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 {"../../resources/default_100_percent/common/", "images/apps/"},
30 {"../../resources/default_200_percent/common/", "images/2x/apps/"},
31 {"../../../third_party/polymer/components-chromium/", "polymer/"}
34 void AddResource(const std::string
& path
,
36 ResourcesMap
* resources_map
) {
37 if (!resources_map
->insert(std::make_pair(path
, resource_id
)).second
)
38 NOTREACHED() << "Redefinition of '" << path
<< "'";
41 const ResourcesMap
* CreateResourcesMap() {
42 ResourcesMap
* result
= new ResourcesMap();
43 for (size_t i
= 0; i
< kWebuiResourcesSize
; ++i
) {
44 const std::string resource_name
= kWebuiResources
[i
].name
;
45 const int resource_id
= kWebuiResources
[i
].value
;
46 AddResource(resource_name
, resource_id
, result
);
47 for (const char* (&alias
)[2]: kPathAliases
) {
48 if (StartsWithASCII(resource_name
, alias
[0], true)) {
49 AddResource(alias
[1] + resource_name
.substr(strlen(alias
[0])),
58 const ResourcesMap
& GetResourcesMap() {
59 // This pointer will be intentionally leaked on shutdown.
60 static const ResourcesMap
* resources_map
= CreateResourcesMap();
61 return *resources_map
;
66 SharedResourcesDataSource::SharedResourcesDataSource() {
69 SharedResourcesDataSource::~SharedResourcesDataSource() {
72 std::string
SharedResourcesDataSource::GetSource() const {
73 return kChromeUIResourcesHost
;
76 void SharedResourcesDataSource::StartDataRequest(
77 const std::string
& path
,
78 int render_process_id
,
80 const URLDataSource::GotDataCallback
& callback
) {
81 const ResourcesMap
& resources_map
= GetResourcesMap();
82 auto it
= resources_map
.find(path
);
83 int idr
= (it
!= resources_map
.end()) ? it
->second
: -1;
84 DCHECK_NE(-1, idr
) << " path: " << path
;
85 scoped_refptr
<base::RefCountedMemory
> bytes
;
87 if (idr
== IDR_WEBUI_CSS_TEXT_DEFAULTS
) {
88 std::vector
<std::string
> placeholders
;
89 placeholders
.push_back(webui::GetTextDirection()); // $1
90 placeholders
.push_back(webui::GetFontFamily()); // $2
91 placeholders
.push_back(webui::GetFontSize()); // $3
93 ContentClient
* content_client
= GetContentClient();
94 const std::string
& chrome_shared
=
95 content_client
->GetDataResource(idr
, ui::SCALE_FACTOR_NONE
).as_string();
96 std::string replaced
=
97 ReplaceStringPlaceholders(chrome_shared
, placeholders
, nullptr);
98 bytes
= base::RefCountedString::TakeString(&replaced
);
100 bytes
= GetContentClient()->GetDataResourceBytes(idr
);
103 callback
.Run(bytes
.get());
106 std::string
SharedResourcesDataSource::GetMimeType(
107 const std::string
& path
) const {
108 // Requests should not block on the disk! On POSIX this goes to disk.
109 // http://code.google.com/p/chromium/issues/detail?id=59849
111 base::ThreadRestrictions::ScopedAllowIO allow_io
;
112 std::string mime_type
;
113 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path
), &mime_type
);
118 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
119 const std::string
& origin
) const {
120 // For now we give access only for "chrome://*" origins.
121 // According to CORS spec, Access-Control-Allow-Origin header doesn't support
122 // wildcards, so we need to set its value explicitly by passing the |origin|
124 std::string allowed_origin_prefix
= kChromeUIScheme
;
125 allowed_origin_prefix
+= "://";
126 if (origin
.find(allowed_origin_prefix
) != 0)
131 } // namespace content