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 "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/memory/ref_counted_memory.h" | 8 #include "base/memory/ref_counted_memory.h" |
| 9 #include "base/strings/string_piece.h" |
9 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
10 #include "base/threading/thread_restrictions.h" | 11 #include "base/threading/thread_restrictions.h" |
11 #include "content/public/common/content_client.h" | 12 #include "content/public/common/content_client.h" |
12 #include "content/public/common/url_constants.h" | 13 #include "content/public/common/url_constants.h" |
13 #include "net/base/mime_util.h" | 14 #include "net/base/mime_util.h" |
| 15 #include "ui/base/layout.h" |
| 16 #include "ui/base/webui/web_ui_util.h" |
| 17 #include "ui/resources/grit/webui_resources.h" |
14 #include "ui/resources/grit/webui_resources_map.h" | 18 #include "ui/resources/grit/webui_resources_map.h" |
15 | 19 |
| 20 namespace content { |
| 21 |
16 namespace { | 22 namespace { |
17 | 23 |
18 const char kAppImagesPath[] = "images/apps/"; | 24 const char kAppImagesPath[] = "images/apps/"; |
19 const char kAppImagesPath2x[] = "images/2x/apps/"; | 25 const char kAppImagesPath2x[] = "images/2x/apps/"; |
20 | 26 |
21 const char kReplacement[] = "../../resources/default_100_percent/common/"; | 27 const char kReplacement[] = "../../resources/default_100_percent/common/"; |
22 const char kReplacement2x[] = "../../resources/default_200_percent/common/"; | 28 const char kReplacement2x[] = "../../resources/default_200_percent/common/"; |
23 | 29 |
24 // This entire method is a hack introduced to be able to handle apps images | 30 // This entire method is a hack introduced to be able to handle apps images |
25 // that exist in the ui/resources directory. From JS/CSS, we still load the | 31 // that exist in the ui/resources directory. From JS/CSS, we still load the |
(...skipping 30 matching lines...) Expand all Loading... |
56 | 62 |
57 } // namespace | 63 } // namespace |
58 | 64 |
59 SharedResourcesDataSource::SharedResourcesDataSource() { | 65 SharedResourcesDataSource::SharedResourcesDataSource() { |
60 } | 66 } |
61 | 67 |
62 SharedResourcesDataSource::~SharedResourcesDataSource() { | 68 SharedResourcesDataSource::~SharedResourcesDataSource() { |
63 } | 69 } |
64 | 70 |
65 std::string SharedResourcesDataSource::GetSource() const { | 71 std::string SharedResourcesDataSource::GetSource() const { |
66 return content::kChromeUIResourcesHost; | 72 return kChromeUIResourcesHost; |
67 } | 73 } |
68 | 74 |
69 void SharedResourcesDataSource::StartDataRequest( | 75 void SharedResourcesDataSource::StartDataRequest( |
70 const std::string& path, | 76 const std::string& path, |
71 int render_process_id, | 77 int render_process_id, |
72 int render_frame_id, | 78 int render_frame_id, |
73 const content::URLDataSource::GotDataCallback& callback) { | 79 const URLDataSource::GotDataCallback& callback) { |
74 int idr = PathToIDR(path); | 80 int idr = PathToIDR(path); |
75 DCHECK_NE(-1, idr) << " path: " << path; | 81 DCHECK_NE(-1, idr) << " path: " << path; |
76 scoped_refptr<base::RefCountedStaticMemory> bytes( | 82 scoped_refptr<base::RefCountedMemory> bytes; |
77 content::GetContentClient()->GetDataResourceBytes(idr)); | 83 |
| 84 ContentClient* content_client = GetContentClient(); |
| 85 |
| 86 // TODO(dbeam): there's some comments in content/DEPS about disallowing |
| 87 // grd-related code. Does using this IDR_* go against that spirit? |
| 88 if (idr == IDR_WEBUI_CSS_TEXT_DEFAULTS) { |
| 89 std::vector<std::string> placeholders; |
| 90 placeholders.push_back(webui::GetTextDirection()); // $1 |
| 91 placeholders.push_back(webui::GetFontFamily()); // $2 |
| 92 placeholders.push_back(webui::GetFontSize()); // $3 |
| 93 |
| 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); |
| 99 } else { |
| 100 bytes = content_client->GetDataResourceBytes(idr); |
| 101 } |
78 | 102 |
79 callback.Run(bytes.get()); | 103 callback.Run(bytes.get()); |
80 } | 104 } |
81 | 105 |
82 std::string SharedResourcesDataSource::GetMimeType( | 106 std::string SharedResourcesDataSource::GetMimeType( |
83 const std::string& path) const { | 107 const std::string& path) const { |
84 // Requests should not block on the disk! On POSIX this goes to disk. | 108 // Requests should not block on the disk! On POSIX this goes to disk. |
85 // http://code.google.com/p/chromium/issues/detail?id=59849 | 109 // http://code.google.com/p/chromium/issues/detail?id=59849 |
86 | 110 |
87 base::ThreadRestrictions::ScopedAllowIO allow_io; | 111 base::ThreadRestrictions::ScopedAllowIO allow_io; |
88 std::string mime_type; | 112 std::string mime_type; |
89 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type); | 113 net::GetMimeTypeFromFile(base::FilePath().AppendASCII(path), &mime_type); |
90 return mime_type; | 114 return mime_type; |
91 } | 115 } |
92 | 116 |
93 std::string | 117 std::string |
94 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( | 118 SharedResourcesDataSource::GetAccessControlAllowOriginForOrigin( |
95 const std::string& origin) const { | 119 const std::string& origin) const { |
96 // For now we give access only for "chrome://*" origins. | 120 // For now we give access only for "chrome://*" origins. |
97 // According to CORS spec, Access-Control-Allow-Origin header doesn't support | 121 // According to CORS spec, Access-Control-Allow-Origin header doesn't support |
98 // wildcards, so we need to set its value explicitly by passing the |origin| | 122 // wildcards, so we need to set its value explicitly by passing the |origin| |
99 // back. | 123 // back. |
100 std::string allowed_origin_prefix = content::kChromeUIScheme; | 124 std::string allowed_origin_prefix = kChromeUIScheme; |
101 allowed_origin_prefix += "://"; | 125 allowed_origin_prefix += "://"; |
102 if (origin.find(allowed_origin_prefix) != 0) | 126 if (origin.find(allowed_origin_prefix) != 0) |
103 return "none"; | 127 return "none"; |
104 return origin; | 128 return origin; |
105 } | 129 } |
| 130 |
| 131 } // namespace content |
OLD | NEW |