Add ICU message format support
[chromium-blink-merge.git] / content / browser / webui / shared_resources_data_source.cc
blob32dc6c6388f2df41100619b70709843863397f3c
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"
21 namespace content {
23 namespace {
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/v1_0/components-chromium/", "polymer/v1_0/"},
30 {"../../../third_party/web-animations-js/sources/",
31 "polymer/v1_0/web-animations-js/"},
32 {"../../views/resources/default_100_percent/common/", "images/apps/"},
33 {"../../views/resources/default_200_percent/common/", "images/2x/apps/"},
34 {"../../webui/resources/cr_elements/", "cr_elements/"}};
36 void AddResource(const std::string& path,
37 int resource_id,
38 ResourcesMap* resources_map) {
39 if (!resources_map->insert(std::make_pair(path, resource_id)).second)
40 NOTREACHED() << "Redefinition of '" << path << "'";
43 const ResourcesMap* CreateResourcesMap() {
44 ResourcesMap* result = new ResourcesMap();
45 for (size_t i = 0; i < kWebuiResourcesSize; ++i) {
46 const std::string resource_name = kWebuiResources[i].name;
47 const int resource_id = kWebuiResources[i].value;
48 AddResource(resource_name, resource_id, result);
49 for (const char* (&alias)[2]: kPathAliases) {
50 if (base::StartsWith(resource_name, alias[0],
51 base::CompareCase::SENSITIVE)) {
52 AddResource(alias[1] + resource_name.substr(strlen(alias[0])),
53 resource_id, result);
58 return result;
61 const ResourcesMap& GetResourcesMap() {
62 // This pointer will be intentionally leaked on shutdown.
63 static const ResourcesMap* resources_map = CreateResourcesMap();
64 return *resources_map;
67 } // namespace
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,
82 int render_frame_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);
93 } else {
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);
108 return mime_type;
111 std::string
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|
117 // back.
118 std::string allowed_origin_prefix = kChromeUIScheme;
119 allowed_origin_prefix += "://";
120 if (origin.find(allowed_origin_prefix) != 0)
121 return "null";
122 return origin;
125 } // namespace content