1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/webui/shared_resources_data_source.h"
7 #include "base/containers/hash_tables.h"
8 #include "base/logging.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "base/strings/string_piece.h"
11 #include "base/strings/string_util.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "content/public/common/content_client.h"
14 #include "content/public/common/url_constants.h"
15 #include "net/base/mime_util.h"
16 #include "ui/base/layout.h"
17 #include "ui/base/webui/web_ui_util.h"
18 #include "ui/resources/grit/webui_resources.h"
19 #include "ui/resources/grit/webui_resources_map.h"
25 using ResourcesMap
= base::hash_map
<std::string
, int>;
27 // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' aliases.
28 const char* kPathAliases
[][2] = {
29 {"../../../third_party/polymer/components-chromium/", "polymer/"},
30 {"../../../third_party/polymer/v1_0/components-chromium/", "polymer/v1_0/"},
31 {"../../../third_party/web-animations-js/sources/",
32 "polymer/web-animations-js/"},
33 {"../../resources/default_100_percent/common/", "images/apps/"},
34 {"../../resources/default_200_percent/common/", "images/2x/apps/"},
35 {"../../webui/resources/cr_elements/", "cr_elements/"}};
37 void AddResource(const std::string
& path
,
39 ResourcesMap
* resources_map
) {
40 if (!resources_map
->insert(std::make_pair(path
, resource_id
)).second
)
41 NOTREACHED() << "Redefinition of '" << path
<< "'";
44 const ResourcesMap
* CreateResourcesMap() {
45 ResourcesMap
* result
= new ResourcesMap();
46 for (size_t i
= 0; i
< kWebuiResourcesSize
; ++i
) {
47 const std::string resource_name
= kWebuiResources
[i
].name
;
48 const int resource_id
= kWebuiResources
[i
].value
;
49 AddResource(resource_name
, resource_id
, result
);
50 for (const char* (&alias
)[2]: kPathAliases
) {
51 if (base::StartsWithASCII(resource_name
, alias
[0], true)) {
52 AddResource(alias
[1] + resource_name
.substr(strlen(alias
[0])),
61 const ResourcesMap
& GetResourcesMap() {
62 // This pointer will be intentionally leaked on shutdown.
63 static const ResourcesMap
* resources_map
= CreateResourcesMap();
64 return *resources_map
;
69 SharedResourcesDataSource::SharedResourcesDataSource() {
72 SharedResourcesDataSource::~SharedResourcesDataSource() {
75 std::string
SharedResourcesDataSource::GetSource() const {
76 return kChromeUIResourcesHost
;
79 void SharedResourcesDataSource::StartDataRequest(
80 const std::string
& path
,
81 int render_process_id
,
83 const URLDataSource::GotDataCallback
& callback
) {
84 const ResourcesMap
& resources_map
= GetResourcesMap();
85 auto it
= resources_map
.find(path
);
86 int idr
= (it
!= resources_map
.end()) ? it
->second
: -1;
87 DCHECK_NE(-1, idr
) << " path: " << path
;
88 scoped_refptr
<base::RefCountedMemory
> bytes
;
90 if (idr
== IDR_WEBUI_CSS_TEXT_DEFAULTS
) {
91 std::string css
= webui::GetWebUiCssTextDefaults();
92 bytes
= base::RefCountedString::TakeString(&css
);
94 bytes
= GetContentClient()->GetDataResourceBytes(idr
);
97 callback
.Run(bytes
.get());
100 std::string
SharedResourcesDataSource::GetMimeType(
101 const std::string
& path
) const {
102 // Requests should not block on the disk! On POSIX this goes to disk.
103 // http://code.google.com/p/chromium/issues/detail?id=59849
105 base::ThreadRestrictions::ScopedAllowIO allow_io
;
106 std::string mime_type
;
107 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path
), &mime_type
);
112 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
113 const std::string
& origin
) const {
114 // For now we give access only for "chrome://*" origins.
115 // According to CORS spec, Access-Control-Allow-Origin header doesn't support
116 // wildcards, so we need to set its value explicitly by passing the |origin|
118 std::string allowed_origin_prefix
= kChromeUIScheme
;
119 allowed_origin_prefix
+= "://";
120 if (origin
.find(allowed_origin_prefix
) != 0)
125 } // namespace content