OLD | NEW |
---|---|
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/common/extensions/extension.h" | 5 #include "chrome/common/extensions/extension.h" |
6 | 6 |
7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/stl_util-inl.h" | |
13 #include "base/third_party/nss/blapi.h" | 14 #include "base/third_party/nss/blapi.h" |
14 #include "base/third_party/nss/sha256.h" | 15 #include "base/third_party/nss/sha256.h" |
15 #include "chrome/common/chrome_constants.h" | 16 #include "chrome/common/chrome_constants.h" |
16 #include "chrome/common/extensions/extension_constants.h" | 17 #include "chrome/common/extensions/extension_constants.h" |
17 #include "chrome/common/extensions/extension_error_reporter.h" | 18 #include "chrome/common/extensions/extension_error_reporter.h" |
18 #include "chrome/common/extensions/extension_error_utils.h" | 19 #include "chrome/common/extensions/extension_error_utils.h" |
19 #include "chrome/common/extensions/user_script.h" | 20 #include "chrome/common/extensions/user_script.h" |
20 #include "chrome/common/notification_service.h" | 21 #include "chrome/common/notification_service.h" |
21 #include "chrome/common/url_constants.h" | 22 #include "chrome/common/url_constants.h" |
22 #include "net/base/base64.h" | 23 #include "net/base/base64.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
99 }; | 100 }; |
100 | 101 |
101 const char* Extension::kPermissionNames[] = { | 102 const char* Extension::kPermissionNames[] = { |
102 "tabs", | 103 "tabs", |
103 "bookmarks", | 104 "bookmarks", |
104 }; | 105 }; |
105 const size_t Extension::kNumPermissions = | 106 const size_t Extension::kNumPermissions = |
106 arraysize(Extension::kPermissionNames); | 107 arraysize(Extension::kPermissionNames); |
107 | 108 |
108 Extension::~Extension() { | 109 Extension::~Extension() { |
109 for (PageActionMap::iterator i = page_actions_.begin(); | 110 STLDeleteValues(&page_actions_); |
110 i != page_actions_.end(); ++i) | 111 delete browser_action_; |
Erik does not do reviews
2009/09/25 22:56:11
just change this to a scoped_ptr
| |
111 delete i->second; | |
112 } | 112 } |
113 | 113 |
114 const std::string Extension::VersionString() const { | 114 const std::string Extension::VersionString() const { |
115 return version_->GetString(); | 115 return version_->GetString(); |
116 } | 116 } |
117 | 117 |
118 // static | 118 // static |
119 bool Extension::IsExtension(const FilePath& file_name) { | 119 bool Extension::IsExtension(const FilePath& file_name) { |
120 return file_name.MatchesExtension( | 120 return file_name.MatchesExtension( |
121 FilePath::StringType(FILE_PATH_LITERAL(".")) + | 121 FilePath::StringType(FILE_PATH_LITERAL(".")) + |
(...skipping 21 matching lines...) Expand all Loading... | |
143 const std::string& relative_path) { | 143 const std::string& relative_path) { |
144 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme)); | 144 DCHECK(extension_url.SchemeIs(chrome::kExtensionScheme)); |
145 DCHECK(extension_url.path() == "/"); | 145 DCHECK(extension_url.path() == "/"); |
146 | 146 |
147 GURL ret_val = GURL(extension_url.spec() + relative_path); | 147 GURL ret_val = GURL(extension_url.spec() + relative_path); |
148 DCHECK(StartsWithASCII(ret_val.spec(), extension_url.spec(), false)); | 148 DCHECK(StartsWithASCII(ret_val.spec(), extension_url.spec(), false)); |
149 | 149 |
150 return ret_val; | 150 return ret_val; |
151 } | 151 } |
152 | 152 |
153 const PageAction* Extension::GetPageAction(std::string id) const { | 153 const ContextualAction* Extension::GetContextualAction( |
154 PageActionMap::const_iterator it = page_actions_.find(id); | 154 std::string id, ContextualAction::ContextualActionType action_type) const { |
155 if (it == page_actions_.end()) | 155 if (action_type == ContextualAction::BROWSER_ACTION) { |
156 return NULL; | 156 return browser_action_; |
Erik does not do reviews
2009/09/25 22:56:11
DCHECK(id.empty())
| |
157 } else { | |
158 ContextualActionMap::const_iterator it = page_actions_.find(id); | |
159 if (it == page_actions_.end()) | |
160 return NULL; | |
157 | 161 |
158 return it->second; | 162 return it->second; |
163 } | |
159 } | 164 } |
160 | 165 |
161 Extension::Location Extension::ExternalExtensionInstallType( | 166 Extension::Location Extension::ExternalExtensionInstallType( |
162 std::string registry_path) { | 167 std::string registry_path) { |
163 #if defined(OS_WIN) | 168 #if defined(OS_WIN) |
164 HKEY reg_root = HKEY_LOCAL_MACHINE; | 169 HKEY reg_root = HKEY_LOCAL_MACHINE; |
165 RegKey key; | 170 RegKey key; |
166 registry_path.append("\\"); | 171 registry_path.append("\\"); |
167 registry_path.append(id_); | 172 registry_path.append(id_); |
168 if (key.Open(reg_root, ASCIIToWide(registry_path).c_str())) | 173 if (key.Open(reg_root, ASCIIToWide(registry_path).c_str())) |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
299 // TODO(georged): Make GetResourceURL accept wstring too | 304 // TODO(georged): Make GetResourceURL accept wstring too |
300 GURL url = GetResourceURL(WideToUTF8(relative)); | 305 GURL url = GetResourceURL(WideToUTF8(relative)); |
301 FilePath path = GetResourcePath(WideToUTF8(relative)); | 306 FilePath path = GetResourcePath(WideToUTF8(relative)); |
302 result->css_scripts().push_back(UserScript::File(path, url)); | 307 result->css_scripts().push_back(UserScript::File(path, url)); |
303 } | 308 } |
304 } | 309 } |
305 | 310 |
306 return true; | 311 return true; |
307 } | 312 } |
308 | 313 |
309 // Helper method that loads a PageAction object from a dictionary in the | 314 // Helper method that loads a PageAction or BrowserAction object from a |
310 // page_action list of the manifest. | 315 // dictionary in the page_actions list or browser_action key of the manifest. |
311 PageAction* Extension::LoadPageActionHelper( | 316 ContextualAction* Extension::LoadContextualActionHelper( |
312 const DictionaryValue* page_action, int definition_index, | 317 const DictionaryValue* page_action, int definition_index, |
313 std::string* error) { | 318 std::string* error, ContextualAction::ContextualActionType action_type) { |
314 scoped_ptr<PageAction> result(new PageAction()); | 319 scoped_ptr<ContextualAction> result(new ContextualAction()); |
315 result->set_extension_id(id()); | 320 result->set_extension_id(id()); |
321 result->set_type(action_type); | |
316 | 322 |
317 ListValue* icons; | 323 ListValue* icons; |
318 // Read the page action |icons|. | 324 // Read the page action |icons|. |
319 if (!page_action->HasKey(keys::kPageActionIcons) || | 325 if (!page_action->HasKey(keys::kPageActionIcons) || |
320 !page_action->GetList(keys::kPageActionIcons, &icons) || | 326 !page_action->GetList(keys::kPageActionIcons, &icons) || |
321 icons->GetSize() == 0) { | 327 icons->GetSize() == 0) { |
322 *error = ExtensionErrorUtils::FormatErrorMessage( | 328 *error = ExtensionErrorUtils::FormatErrorMessage( |
323 errors::kInvalidPageActionIconPaths, IntToString(definition_index)); | 329 errors::kInvalidPageActionIconPaths, IntToString(definition_index)); |
324 return NULL; | 330 return NULL; |
325 } | 331 } |
326 | 332 |
327 int icon_count = 0; | 333 int icon_count = 0; |
328 for (ListValue::const_iterator iter = icons->begin(); | 334 for (ListValue::const_iterator iter = icons->begin(); |
329 iter != icons->end(); ++iter) { | 335 iter != icons->end(); ++iter) { |
330 std::string path; | 336 std::string path; |
331 if (!(*iter)->GetAsString(&path) || path.empty()) { | 337 if (!(*iter)->GetAsString(&path) || path.empty()) { |
332 *error = ExtensionErrorUtils::FormatErrorMessage( | 338 *error = ExtensionErrorUtils::FormatErrorMessage( |
333 errors::kInvalidPageActionIconPath, | 339 errors::kInvalidPageActionIconPath, |
334 IntToString(definition_index), IntToString(icon_count)); | 340 IntToString(definition_index), IntToString(icon_count)); |
335 return NULL; | 341 return NULL; |
336 } | 342 } |
337 | 343 |
338 result->AddIconPath(path); | 344 result->AddIconPath(path); |
339 ++icon_count; | 345 ++icon_count; |
340 } | 346 } |
341 | 347 |
342 // Read the page action |id|. | 348 if (action_type == ContextualAction::BROWSER_ACTION) { |
343 std::string id; | 349 result->set_id(""); // Not needed (only 1 browser action per extension). |
344 if (!page_action->GetString(keys::kPageActionId, &id)) { | 350 } else { |
345 *error = ExtensionErrorUtils::FormatErrorMessage( | 351 // Read the page action |id|. |
346 errors::kInvalidPageActionId, IntToString(definition_index)); | 352 std::string id; |
347 return NULL; | 353 if (!page_action->GetString(keys::kPageActionId, &id)) { |
354 *error = ExtensionErrorUtils::FormatErrorMessage( | |
355 errors::kInvalidPageActionId, IntToString(definition_index)); | |
356 return NULL; | |
357 } | |
358 result->set_id(id); | |
348 } | 359 } |
349 result->set_id(id); | |
350 | 360 |
351 // Read the page action |name|. | 361 // Read the page action |name|. |
352 std::string name; | 362 std::string name; |
353 if (!page_action->GetString(keys::kName, &name)) { | 363 if (!page_action->GetString(keys::kName, &name)) { |
354 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidName, | 364 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidName, |
355 IntToString(definition_index)); | 365 IntToString(definition_index)); |
356 return NULL; | 366 return NULL; |
357 } | 367 } |
358 result->set_name(name); | 368 result->set_name(name); |
359 | 369 |
360 // Read the page action |type|. It is optional and set to permanent if | |
361 // missing. | |
362 std::string type; | |
363 if (!page_action->GetString(keys::kType, &type)) { | |
364 result->set_type(PageAction::PERMANENT); | |
365 } else if (!LowerCaseEqualsASCII(type, values::kPageActionTypeTab) && | |
366 !LowerCaseEqualsASCII(type, values::kPageActionTypePermanent)) { | |
367 *error = ExtensionErrorUtils::FormatErrorMessage( | |
368 errors::kInvalidPageActionTypeValue, IntToString(definition_index)); | |
369 return NULL; | |
370 } else { | |
371 if (LowerCaseEqualsASCII(type, values::kPageActionTypeTab)) | |
372 result->set_type(PageAction::TAB); | |
373 else | |
374 result->set_type(PageAction::PERMANENT); | |
375 } | |
376 | |
377 return result.release(); | 370 return result.release(); |
378 } | 371 } |
379 | 372 |
380 bool Extension::ContainsNonThemeKeys(const DictionaryValue& source) { | 373 bool Extension::ContainsNonThemeKeys(const DictionaryValue& source) { |
381 // Generate a map of allowable keys | 374 // Generate a map of allowable keys |
382 static std::map<std::wstring, bool> theme_keys; | 375 static std::map<std::wstring, bool> theme_keys; |
383 static bool theme_key_mapped = false; | 376 static bool theme_key_mapped = false; |
384 if (!theme_key_mapped) { | 377 if (!theme_key_mapped) { |
385 for (size_t i = 0; i < arraysize(kValidThemeKeys); ++i) { | 378 for (size_t i = 0; i < arraysize(kValidThemeKeys); ++i) { |
386 theme_keys[kValidThemeKeys[i]] = true; | 379 theme_keys[kValidThemeKeys[i]] = true; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
429 | 422 |
430 // Double-check that the path we ended up with is actually inside the | 423 // Double-check that the path we ended up with is actually inside the |
431 // extension root. | 424 // extension root. |
432 if (!extension_path.IsParent(ret_val)) | 425 if (!extension_path.IsParent(ret_val)) |
433 return FilePath(); | 426 return FilePath(); |
434 | 427 |
435 return ret_val; | 428 return ret_val; |
436 } | 429 } |
437 | 430 |
438 Extension::Extension(const FilePath& path) | 431 Extension::Extension(const FilePath& path) |
439 : is_theme_(false), background_page_ready_(false) { | 432 : browser_action_(NULL), is_theme_(false), background_page_ready_(false) { |
440 DCHECK(path.IsAbsolute()); | 433 DCHECK(path.IsAbsolute()); |
441 location_ = INVALID; | 434 location_ = INVALID; |
442 | 435 |
443 #if defined(OS_WIN) | 436 #if defined(OS_WIN) |
444 // Normalize any drive letter to upper-case. We do this for consistency with | 437 // Normalize any drive letter to upper-case. We do this for consistency with |
445 // net_utils::FilePathToFileURL(), which does the same thing, to make string | 438 // net_utils::FilePathToFileURL(), which does the same thing, to make string |
446 // comparisons simpler. | 439 // comparisons simpler. |
447 std::wstring path_str = path.value(); | 440 std::wstring path_str = path.value(); |
448 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && | 441 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && |
449 path_str[1] == ':') | 442 path_str[1] == ':') |
(...skipping 483 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
933 } | 926 } |
934 | 927 |
935 for (size_t i = 0; i < list_value->GetSize(); ++i) { | 928 for (size_t i = 0; i < list_value->GetSize(); ++i) { |
936 DictionaryValue* page_action_value; | 929 DictionaryValue* page_action_value; |
937 if (!list_value->GetDictionary(i, &page_action_value)) { | 930 if (!list_value->GetDictionary(i, &page_action_value)) { |
938 *error = ExtensionErrorUtils::FormatErrorMessage( | 931 *error = ExtensionErrorUtils::FormatErrorMessage( |
939 errors::kInvalidPageAction, IntToString(i)); | 932 errors::kInvalidPageAction, IntToString(i)); |
940 return false; | 933 return false; |
941 } | 934 } |
942 | 935 |
943 PageAction* page_action = | 936 ContextualAction* contextual_action = |
944 LoadPageActionHelper(page_action_value, i, error); | 937 LoadContextualActionHelper(page_action_value, i, error, |
945 if (!page_action) | 938 ContextualAction::PAGE_ACTION); |
939 if (!contextual_action) | |
946 return false; // Failed to parse page action definition. | 940 return false; // Failed to parse page action definition. |
947 page_actions_[page_action->id()] = page_action; | 941 page_actions_[contextual_action->id()] = contextual_action; |
948 } | 942 } |
949 } | 943 } |
950 | 944 |
945 if (source.HasKey(keys::kBrowserAction)) { | |
946 DictionaryValue* browser_action_value; | |
947 if (!source.GetDictionary(keys::kBrowserAction, &browser_action_value)) { | |
948 *error = ExtensionErrorUtils::FormatErrorMessage( | |
949 errors::kInvalidBrowserAction, ""); | |
950 return false; | |
951 } | |
952 | |
953 browser_action_ = | |
954 LoadContextualActionHelper(browser_action_value, 0, error, | |
955 ContextualAction::BROWSER_ACTION); | |
956 if (!browser_action_) | |
957 return false; // Failed to parse browser action definition. | |
958 } | |
959 | |
951 // Initialize the permissions (optional). | 960 // Initialize the permissions (optional). |
952 if (source.HasKey(keys::kPermissions)) { | 961 if (source.HasKey(keys::kPermissions)) { |
953 ListValue* permissions = NULL; | 962 ListValue* permissions = NULL; |
954 if (!source.GetList(keys::kPermissions, &permissions)) { | 963 if (!source.GetList(keys::kPermissions, &permissions)) { |
955 *error = ExtensionErrorUtils::FormatErrorMessage( | 964 *error = ExtensionErrorUtils::FormatErrorMessage( |
956 errors::kInvalidPermissions, ""); | 965 errors::kInvalidPermissions, ""); |
957 return false; | 966 return false; |
958 } | 967 } |
959 | 968 |
960 for (size_t i = 0; i < permissions->GetSize(); ++i) { | 969 for (size_t i = 0; i < permissions->GetSize(); ++i) { |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1044 for (DictionaryValue::key_iterator it = theme_images->begin_keys(); | 1053 for (DictionaryValue::key_iterator it = theme_images->begin_keys(); |
1045 it != theme_images->end_keys(); ++it) { | 1054 it != theme_images->end_keys(); ++it) { |
1046 std::wstring val; | 1055 std::wstring val; |
1047 if (theme_images->GetString(*it, &val)) { | 1056 if (theme_images->GetString(*it, &val)) { |
1048 image_paths.insert(FilePath::FromWStringHack(val)); | 1057 image_paths.insert(FilePath::FromWStringHack(val)); |
1049 } | 1058 } |
1050 } | 1059 } |
1051 } | 1060 } |
1052 | 1061 |
1053 // page action icons | 1062 // page action icons |
1054 for (PageActionMap::const_iterator it = page_actions().begin(); | 1063 for (ContextualActionMap::const_iterator it = page_actions().begin(); |
1055 it != page_actions().end(); ++it) { | 1064 it != page_actions().end(); ++it) { |
1056 const std::vector<std::string>& icon_paths = it->second->icon_paths(); | 1065 const std::vector<std::string>& icon_paths = it->second->icon_paths(); |
1057 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); | 1066 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); |
1058 iter != icon_paths.end(); ++iter) { | 1067 iter != icon_paths.end(); ++iter) { |
1059 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); | 1068 image_paths.insert(FilePath::FromWStringHack(UTF8ToWide(*iter))); |
1060 } | 1069 } |
1061 } | 1070 } |
1062 | 1071 |
1063 return image_paths; | 1072 return image_paths; |
1064 } | 1073 } |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1124 UserScript::PatternList::const_iterator pattern = | 1133 UserScript::PatternList::const_iterator pattern = |
1125 content_script->url_patterns().begin(); | 1134 content_script->url_patterns().begin(); |
1126 for (; pattern != content_script->url_patterns().end(); ++pattern) { | 1135 for (; pattern != content_script->url_patterns().end(); ++pattern) { |
1127 if (pattern->match_subdomains() && pattern->host().empty()) | 1136 if (pattern->match_subdomains() && pattern->host().empty()) |
1128 return true; | 1137 return true; |
1129 } | 1138 } |
1130 } | 1139 } |
1131 | 1140 |
1132 return false; | 1141 return false; |
1133 } | 1142 } |
OLD | NEW |