[Session restore] Rename group name Enabled to Restore.
[chromium-blink-merge.git] / content / browser / webui / shared_resources_data_source.cc
blobd5377a1676fb7e459c80b52c0a809da4e1dba2a3
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/components-chromium/", "polymer/"},
30 {"../../../third_party/web-animations-js/sources/",
31 "polymer/web-animations-js/"},
32 {"../../resources/default_100_percent/common/", "images/apps/"},
33 {"../../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 (StartsWithASCII(resource_name, alias[0], true)) {
51 AddResource(alias[1] + resource_name.substr(strlen(alias[0])),
52 resource_id, result);
57 return result;
60 const ResourcesMap& GetResourcesMap() {
61 // This pointer will be intentionally leaked on shutdown.
62 static const ResourcesMap* resources_map = CreateResourcesMap();
63 return *resources_map;
66 } // namespace
68 SharedResourcesDataSource::SharedResourcesDataSource() {
71 SharedResourcesDataSource::~SharedResourcesDataSource() {
74 std::string SharedResourcesDataSource::GetSource() const {
75 return kChromeUIResourcesHost;
78 void SharedResourcesDataSource::StartDataRequest(
79 const std::string& path,
80 int render_process_id,
81 int render_frame_id,
82 const URLDataSource::GotDataCallback& callback) {
83 const ResourcesMap& resources_map = GetResourcesMap();
84 auto it = resources_map.find(path);
85 int idr = (it != resources_map.end()) ? it->second : -1;
86 DCHECK_NE(-1, idr) << " path: " << path;
87 scoped_refptr<base::RefCountedMemory> bytes;
89 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) {
90 std::string css = webui::GetWebUiCssTextDefaults();
91 bytes = base::RefCountedString::TakeString(&css);
92 } else {
93 bytes = GetContentClient()->GetDataResourceBytes(idr);
96 callback.Run(bytes.get());
99 std::string SharedResourcesDataSource::GetMimeType(
100 const std::string& path) const {
101 // Requests should not block on the disk! On POSIX this goes to disk.
102 // http://code.google.com/p/chromium/issues/detail?id=59849
104 base::ThreadRestrictions::ScopedAllowIO allow_io;
105 std::string mime_type;
106 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type);
107 return mime_type;
110 std::string
111 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin(
112 const std::string& origin) const {
113 // For now we give access only for "chrome://*" origins.
114 // According to CORS spec, Access-Control-Allow-Origin header doesn't support
115 // wildcards, so we need to set its value explicitly by passing the |origin|
116 // back.
117 std::string allowed_origin_prefix = kChromeUIScheme;
118 allowed_origin_prefix += "://";
119 if (origin.find(allowed_origin_prefix) != 0)
120 return "null";
121 return origin;
124 } // namespace content