Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(505)

Side by Side Diff: chrome/browser/extensions/extension.cc

Issue 40042: Add trivial theming support in extensions.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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 "chrome/browser/extensions/extension.h" 5 #include "chrome/browser/extensions/extension.h"
6 6
7 #include "base/file_path.h" 7 #include "base/file_path.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/string_util.h" 9 #include "base/string_util.h"
10 #include "net/base/net_util.h" 10 #include "net/base/net_util.h"
11 #include "chrome/common/extensions/user_script.h" 11 #include "chrome/common/extensions/user_script.h"
12 #include "chrome/common/resource_bundle.h"
12 #include "chrome/common/url_constants.h" 13 #include "chrome/common/url_constants.h"
13 14
14 const char Extension::kManifestFilename[] = "manifest.json"; 15 const char Extension::kManifestFilename[] = "manifest.json";
15 16
16 const wchar_t* Extension::kContentScriptsKey = L"content_scripts"; 17 const wchar_t* Extension::kContentScriptsKey = L"content_scripts";
17 const wchar_t* Extension::kDescriptionKey = L"description"; 18 const wchar_t* Extension::kDescriptionKey = L"description";
18 const wchar_t* Extension::kFormatVersionKey = L"format_version"; 19 const wchar_t* Extension::kFormatVersionKey = L"format_version";
19 const wchar_t* Extension::kIdKey = L"id"; 20 const wchar_t* Extension::kIdKey = L"id";
20 const wchar_t* Extension::kJsKey = L"js"; 21 const wchar_t* Extension::kJsKey = L"js";
21 const wchar_t* Extension::kMatchesKey = L"matches"; 22 const wchar_t* Extension::kMatchesKey = L"matches";
22 const wchar_t* Extension::kNameKey = L"name"; 23 const wchar_t* Extension::kNameKey = L"name";
23 const wchar_t* Extension::kRunAtKey = L"run_at"; 24 const wchar_t* Extension::kRunAtKey = L"run_at";
24 const wchar_t* Extension::kVersionKey = L"version"; 25 const wchar_t* Extension::kVersionKey = L"version";
25 const wchar_t* Extension::kZipHashKey = L"zip_hash"; 26 const wchar_t* Extension::kZipHashKey = L"zip_hash";
26 const wchar_t* Extension::kPluginsDirKey = L"plugins_dir"; 27 const wchar_t* Extension::kPluginsDirKey = L"plugins_dir";
28 const wchar_t* Extension::kThemeKey = L"theme";
27 29
28 const char* Extension::kRunAtDocumentStartValue = "document_start"; 30 const char* Extension::kRunAtDocumentStartValue = "document_start";
29 const char* Extension::kRunAtDocumentEndValue = "document_end"; 31 const char* Extension::kRunAtDocumentEndValue = "document_end";
30 32
31 // Extension-related error messages. Some of these are simple patterns, where a 33 // Extension-related error messages. Some of these are simple patterns, where a
32 // '*' is replaced at runtime with a specific value. This is used instead of 34 // '*' is replaced at runtime with a specific value. This is used instead of
33 // printf because we want to unit test them and scanf is hard to make 35 // printf because we want to unit test them and scanf is hard to make
34 // cross-platform. 36 // cross-platform.
35 const char* Extension::kInvalidContentScriptError = 37 const char* Extension::kInvalidContentScriptError =
36 "Invalid value for 'content_scripts[*]'."; 38 "Invalid value for 'content_scripts[*]'.";
(...skipping 27 matching lines...) Expand all
64 "Invalid value for 'content_scripts[*].run_at'."; 66 "Invalid value for 'content_scripts[*].run_at'.";
65 const char* Extension::kInvalidVersionError = 67 const char* Extension::kInvalidVersionError =
66 "Required value 'version' is missing or invalid."; 68 "Required value 'version' is missing or invalid.";
67 const char* Extension::kInvalidZipHashError = 69 const char* Extension::kInvalidZipHashError =
68 "Required key 'zip_hash' is missing or invalid."; 70 "Required key 'zip_hash' is missing or invalid.";
69 const char* Extension::kInvalidPluginsDirError = 71 const char* Extension::kInvalidPluginsDirError =
70 "Invalid value for 'plugins_dir'."; 72 "Invalid value for 'plugins_dir'.";
71 73
72 const size_t Extension::kIdSize = 20; // SHA1 (160 bits) == 20 bytes 74 const size_t Extension::kIdSize = 20; // SHA1 (160 bits) == 20 bytes
73 75
76 Extension::Extension(const Extension& rhs) :
77 path_(rhs.path_),
78 extension_url_(rhs.extension_url_),
79 id_(rhs.id_),
80 version_(new Version(*rhs.version_)),
81 name_(rhs.name_),
82 description_(rhs.description_),
83 content_scripts_(rhs.content_scripts_),
84 plugins_dir_(rhs.plugins_dir_),
85 zip_hash_(rhs.zip_hash_),
86 theme_paths_(rhs.theme_paths_) {
87 }
88
74 const std::string Extension::VersionString() const { 89 const std::string Extension::VersionString() const {
75 return version_->GetString(); 90 return version_->GetString();
76 } 91 }
77 92
78 // static 93 // static
79 GURL Extension::GetResourceURL(const GURL& extension_url, 94 GURL Extension::GetResourceURL(const GURL& extension_url,
80 const std::string& relative_path) { 95 const std::string& relative_path) {
81 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme)); 96 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme));
82 DCHECK(extension_url.path() == "/"); 97 DCHECK(extension_url.path() == "/");
83 98
84 GURL ret_val = GURL(extension_url.spec() + relative_path); 99 GURL ret_val = GURL(extension_url.spec() + relative_path);
85 DCHECK(StartsWithASCII(ret_val.spec(), extension_url.spec(), false)); 100 DCHECK(StartsWithASCII(ret_val.spec(), extension_url.spec(), false));
86 101
87 return ret_val; 102 return ret_val;
88 } 103 }
89 104
105 FilePath Extension::GetThemeResourcePath(const int resource_id) {
106 std::wstring id = IntToWString(resource_id);
107 std::string path = theme_paths_[id];
108 if (path.size())
109 return path_.AppendASCII(path.c_str());
110 return FilePath();
111 }
112
90 // static 113 // static
91 FilePath Extension::GetResourcePath(const FilePath& extension_path, 114 FilePath Extension::GetResourcePath(const FilePath& extension_path,
92 const std::string& relative_path) { 115 const std::string& relative_path) {
93 // Build up a file:// URL and convert that back to a FilePath. This avoids 116 // Build up a file:// URL and convert that back to a FilePath. This avoids
94 // URL encoding and path separator issues. 117 // URL encoding and path separator issues.
95 118
96 // Convert the extension's root to a file:// URL. 119 // Convert the extension's root to a file:// URL.
97 GURL extension_url = net::FilePathToFileURL(extension_path); 120 GURL extension_url = net::FilePathToFileURL(extension_path);
98 if (!extension_url.is_valid()) 121 if (!extension_url.is_valid())
99 return FilePath(); 122 return FilePath();
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
230 // Initialize plugins dir (optional). 253 // Initialize plugins dir (optional).
231 if (source.HasKey(kPluginsDirKey)) { 254 if (source.HasKey(kPluginsDirKey)) {
232 std::string plugins_dir; 255 std::string plugins_dir;
233 if (!source.GetString(kPluginsDirKey, &plugins_dir)) { 256 if (!source.GetString(kPluginsDirKey, &plugins_dir)) {
234 *error = kInvalidPluginsDirError; 257 *error = kInvalidPluginsDirError;
235 return false; 258 return false;
236 } 259 }
237 plugins_dir_ = path_.AppendASCII(plugins_dir); 260 plugins_dir_ = path_.AppendASCII(plugins_dir);
238 } 261 }
239 262
263 if (source.HasKey(kThemeKey)) {
264 DictionaryValue* dict_value;
265 if (source.GetDictionary(kThemeKey, &dict_value)) {
266 DictionaryValue::key_iterator iter = dict_value->begin_keys();
267 while (iter != dict_value->end_keys()) {
268 std::string val;
269 if (dict_value->GetString(*iter, &val)) {
270 std::wstring id = *iter;
271 theme_paths_[id] = val;
272 }
273 ++iter;
274 }
275 ResourceBundle::GetSharedInstance().SetThemeExtension(*this);
276 }
277 }
278
240 // Initialize content scripts (optional). 279 // Initialize content scripts (optional).
241 if (source.HasKey(kContentScriptsKey)) { 280 if (source.HasKey(kContentScriptsKey)) {
242 ListValue* list_value; 281 ListValue* list_value;
243 if (!source.GetList(kContentScriptsKey, &list_value)) { 282 if (!source.GetList(kContentScriptsKey, &list_value)) {
244 *error = kInvalidContentScriptsListError; 283 *error = kInvalidContentScriptsListError;
245 return false; 284 return false;
246 } 285 }
247 286
248 for (size_t i = 0; i < list_value->GetSize(); ++i) { 287 for (size_t i = 0; i < list_value->GetSize(); ++i) {
249 DictionaryValue* content_script; 288 DictionaryValue* content_script;
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
322 script.set_path(Extension::GetResourcePath(path(), file)); 361 script.set_path(Extension::GetResourcePath(path(), file));
323 script.set_url(Extension::GetResourceURL(url(), file)); 362 script.set_url(Extension::GetResourceURL(url(), file));
324 363
325 content_scripts_.push_back(script); 364 content_scripts_.push_back(script);
326 } 365 }
327 } 366 }
328 367
329 return true; 368 return true;
330 } 369 }
331 370
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698