| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/webui/shared_resources_data_source.h" | 5 #include "content/browser/webui/shared_resources_data_source.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include "base/containers/hash_tables.h" | |
| 10 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 15 #include "content/public/common/content_client.h" | 14 #include "content/public/common/content_client.h" |
| 16 #include "content/public/common/url_constants.h" | 15 #include "content/public/common/url_constants.h" |
| 17 #include "ui/base/layout.h" | 16 #include "ui/base/layout.h" |
| 18 #include "ui/base/webui/web_ui_util.h" | 17 #include "ui/base/webui/web_ui_util.h" |
| 19 #include "ui/resources/grit/webui_resources.h" | 18 #include "ui/resources/grit/webui_resources.h" |
| 20 #include "ui/resources/grit/webui_resources_map.h" | 19 #include "ui/resources/grit/webui_resources_map.h" |
| 21 | 20 |
| 22 #if defined(OS_WIN) | 21 #if defined(OS_WIN) |
| 23 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 24 #endif | 23 #endif |
| 25 | 24 |
| 26 namespace content { | 25 namespace content { |
| 27 | 26 |
| 28 namespace { | 27 namespace { |
| 29 | 28 |
| 30 using ResourcesMap = base::hash_map<std::string, int>; | |
| 31 | |
| 32 // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' aliases. | 29 // TODO(rkc): Once we have a separate source for apps, remove '*/apps/' aliases. |
| 33 const char* const kPathAliases[][2] = { | 30 const char* const kPathAliases[][2] = { |
| 34 {"../../../third_party/polymer/v1_0/components-chromium/", "polymer/v1_0/"}, | 31 {"../../../third_party/polymer/v1_0/components-chromium/", "polymer/v1_0/"}, |
| 35 {"../../../third_party/web-animations-js/sources/", | 32 {"../../../third_party/web-animations-js/sources/", |
| 36 "polymer/v1_0/web-animations-js/"}, | 33 "polymer/v1_0/web-animations-js/"}, |
| 37 {"../../views/resources/default_100_percent/common/", "images/apps/"}, | 34 {"../../views/resources/default_100_percent/common/", "images/apps/"}, |
| 38 {"../../views/resources/default_200_percent/common/", "images/2x/apps/"}, | 35 {"../../views/resources/default_200_percent/common/", "images/2x/apps/"}, |
| 39 {"../../webui/resources/cr_elements/", "cr_elements/"}}; | 36 {"../../webui/resources/cr_elements/", "cr_elements/"}}; |
| 40 | 37 |
| 41 void AddResource(const std::string& path, | 38 void AddResource(const std::string& path, |
| 42 int resource_id, | 39 int resource_id, |
| 43 ResourcesMap* resources_map) { | 40 SharedResourcesDataSource::ResourcesMap* resources_map) { |
| 44 if (!resources_map->insert(std::make_pair(path, resource_id)).second) | 41 if (!resources_map->insert(std::make_pair(path, resource_id)).second) |
| 45 NOTREACHED() << "Redefinition of '" << path << "'"; | 42 NOTREACHED() << "Redefinition of '" << path << "'"; |
| 46 } | 43 } |
| 47 | 44 |
| 48 const ResourcesMap* CreateResourcesMap() { | 45 const SharedResourcesDataSource::ResourcesMap* CreateResourcesMap() { |
| 49 ResourcesMap* result = new ResourcesMap(); | 46 SharedResourcesDataSource::ResourcesMap* result = |
| 47 new SharedResourcesDataSource::ResourcesMap(); |
| 50 for (size_t i = 0; i < kWebuiResourcesSize; ++i) { | 48 for (size_t i = 0; i < kWebuiResourcesSize; ++i) { |
| 51 const std::string resource_name = kWebuiResources[i].name; | 49 const std::string resource_name = kWebuiResources[i].name; |
| 52 const int resource_id = kWebuiResources[i].value; | 50 const int resource_id = kWebuiResources[i].value; |
| 53 AddResource(resource_name, resource_id, result); | 51 AddResource(resource_name, resource_id, result); |
| 54 for (const char* const (&alias)[2] : kPathAliases) { | 52 for (const char* const (&alias)[2] : kPathAliases) { |
| 55 if (base::StartsWith(resource_name, alias[0], | 53 if (base::StartsWith(resource_name, alias[0], |
| 56 base::CompareCase::SENSITIVE)) { | 54 base::CompareCase::SENSITIVE)) { |
| 57 AddResource(alias[1] + resource_name.substr(strlen(alias[0])), | 55 AddResource(alias[1] + resource_name.substr(strlen(alias[0])), |
| 58 resource_id, result); | 56 resource_id, result); |
| 59 } | 57 } |
| 60 } | 58 } |
| 61 } | 59 } |
| 62 | 60 |
| 63 return result; | 61 return result; |
| 64 } | 62 } |
| 65 | 63 |
| 66 const ResourcesMap& GetResourcesMap() { | |
| 67 // This pointer will be intentionally leaked on shutdown. | |
| 68 static const ResourcesMap* resources_map = CreateResourcesMap(); | |
| 69 return *resources_map; | |
| 70 } | |
| 71 | |
| 72 } // namespace | 64 } // namespace |
| 73 | 65 |
| 66 // static |
| 67 const SharedResourcesDataSource::ResourcesMap& SharedResourcesDataSource::GetRes
ourcesMap() { |
| 68 // This pointer will be intentionally leaked on shutdown. |
| 69 static const SharedResourcesDataSource::ResourcesMap* resources_map = CreateRe
sourcesMap(); |
| 70 return *resources_map; |
| 71 } |
| 72 |
| 74 SharedResourcesDataSource::SharedResourcesDataSource() { | 73 SharedResourcesDataSource::SharedResourcesDataSource() { |
| 75 } | 74 } |
| 76 | 75 |
| 77 SharedResourcesDataSource::~SharedResourcesDataSource() { | 76 SharedResourcesDataSource::~SharedResourcesDataSource() { |
| 78 } | 77 } |
| 79 | 78 |
| 80 std::string SharedResourcesDataSource::GetSource() const { | 79 std::string SharedResourcesDataSource::GetSource() const { |
| 81 return kChromeUIResourcesHost; | 80 return kChromeUIResourcesHost; |
| 82 } | 81 } |
| 83 | 82 |
| 84 void SharedResourcesDataSource::StartDataRequest( | 83 void SharedResourcesDataSource::StartDataRequest( |
| 85 const std::string& path, | 84 const std::string& path, |
| 86 int render_process_id, | 85 int render_process_id, |
| 87 int render_frame_id, | 86 int render_frame_id, |
| 88 const URLDataSource::GotDataCallback& callback) { | 87 const URLDataSource::GotDataCallback& callback) { |
| 89 const ResourcesMap& resources_map = GetResourcesMap(); | 88 const SharedResourcesDataSource::ResourcesMap& resources_map = GetResourcesMap
(); |
| 90 auto it = resources_map.find(path); | 89 auto it = resources_map.find(path); |
| 91 int idr = (it != resources_map.end()) ? it->second : -1; | 90 int idr = (it != resources_map.end()) ? it->second : -1; |
| 92 DCHECK_NE(-1, idr) << " path: " << path; | 91 DCHECK_NE(-1, idr) << " path: " << path; |
| 93 scoped_refptr<base::RefCountedMemory> bytes; | 92 scoped_refptr<base::RefCountedMemory> bytes; |
| 94 | 93 |
| 95 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { | 94 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { |
| 96 std::string css = webui::GetWebUiCssTextDefaults(); | 95 std::string css = webui::GetWebUiCssTextDefaults(); |
| 97 bytes = base::RefCountedString::TakeString(&css); | 96 bytes = base::RefCountedString::TakeString(&css); |
| 98 } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) { | 97 } else if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS_MD) { |
| 99 std::string css = webui::GetWebUiCssTextDefaultsMd(); | 98 std::string css = webui::GetWebUiCssTextDefaultsMd(); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 std::string allowed_origin_prefix = kChromeUIScheme; | 160 std::string allowed_origin_prefix = kChromeUIScheme; |
| 162 allowed_origin_prefix += "://"; | 161 allowed_origin_prefix += "://"; |
| 163 if (!base::StartsWith(origin, allowed_origin_prefix, | 162 if (!base::StartsWith(origin, allowed_origin_prefix, |
| 164 base::CompareCase::SENSITIVE)) { | 163 base::CompareCase::SENSITIVE)) { |
| 165 return "null"; | 164 return "null"; |
| 166 } | 165 } |
| 167 return origin; | 166 return origin; |
| 168 } | 167 } |
| 169 | 168 |
| 170 } // namespace content | 169 } // namespace content |
| OLD | NEW |