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

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

Issue 340057: Add first-class support for user scripts (Closed)
Patch Set: newness Created 11 years, 1 month 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
OLDNEW
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/command_line.h" 9 #include "base/command_line.h"
10 #include "base/file_path.h" 10 #include "base/file_path.h"
(...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after
239 URLPattern pattern; 239 URLPattern pattern;
240 if (!pattern.Parse(match_str)) { 240 if (!pattern.Parse(match_str)) {
241 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch, 241 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidMatch,
242 IntToString(definition_index), IntToString(j)); 242 IntToString(definition_index), IntToString(j));
243 return false; 243 return false;
244 } 244 }
245 245
246 result->add_url_pattern(pattern); 246 result->add_url_pattern(pattern);
247 } 247 }
248 248
249 // include/exclude globs (mostly for Greasemonkey compat)
250 if (!LoadGlobsHelper(content_script, definition_index, keys::kIncludeGlobs,
251 error, &UserScript::add_glob, result)) {
252 return false;
253 }
254
255 if (!LoadGlobsHelper(content_script, definition_index, keys::kExcludeGlobs,
256 error, &UserScript::add_exclude_glob, result)) {
257 return false;
258 }
259
249 // js and css keys 260 // js and css keys
250 ListValue* js = NULL; 261 ListValue* js = NULL;
251 if (content_script->HasKey(keys::kJs) && 262 if (content_script->HasKey(keys::kJs) &&
252 !content_script->GetList(keys::kJs, &js)) { 263 !content_script->GetList(keys::kJs, &js)) {
253 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJsList, 264 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidJsList,
254 IntToString(definition_index)); 265 IntToString(definition_index));
255 return false; 266 return false;
256 } 267 }
257 268
258 ListValue* css = NULL; 269 ListValue* css = NULL;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 GURL url = GetResourceURL(WideToUTF8(relative)); 313 GURL url = GetResourceURL(WideToUTF8(relative));
303 ExtensionResource resource = GetResource(WideToUTF8(relative)); 314 ExtensionResource resource = GetResource(WideToUTF8(relative));
304 result->css_scripts().push_back(UserScript::File( 315 result->css_scripts().push_back(UserScript::File(
305 resource.extension_root(), resource.relative_path(), url)); 316 resource.extension_root(), resource.relative_path(), url));
306 } 317 }
307 } 318 }
308 319
309 return true; 320 return true;
310 } 321 }
311 322
323 bool Extension::LoadGlobsHelper(
324 const DictionaryValue* content_script,
325 int content_script_index,
326 const wchar_t* globs_property_name,
327 std::string* error,
328 void (UserScript::*add_method) (const std::string& glob),
329 UserScript *instance) {
330 if (!content_script->HasKey(globs_property_name))
331 return true; // they are optional
332
333 ListValue* list = NULL;
334 if (!content_script->GetList(globs_property_name, &list)) {
335 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidGlobList,
336 IntToString(content_script_index), WideToASCII(globs_property_name));
337 return false;
338 }
339
340 for (size_t i = 0; i < list->GetSize(); ++i) {
341 std::string glob;
342 if (!list->GetString(i, &glob)) {
343 *error = ExtensionErrorUtils::FormatErrorMessage(errors::kInvalidGlob,
344 IntToString(content_script_index), WideToASCII(globs_property_name),
345 IntToString(i));
346 return false;
347 }
348
349 (instance->*add_method)(glob);
350 }
351
352 return true;
353 }
354
312 ExtensionAction* Extension::LoadExtensionActionHelper( 355 ExtensionAction* Extension::LoadExtensionActionHelper(
313 const DictionaryValue* extension_action, std::string* error) { 356 const DictionaryValue* extension_action, std::string* error) {
314 scoped_ptr<ExtensionAction> result(new ExtensionAction()); 357 scoped_ptr<ExtensionAction> result(new ExtensionAction());
315 result->set_extension_id(id()); 358 result->set_extension_id(id());
316 359
317 // Page actions are hidden by default, and browser actions ignore 360 // Page actions are hidden by default, and browser actions ignore
318 // visibility. 361 // visibility.
319 result->SetIsVisible(ExtensionAction::kDefaultTabId, false); 362 result->SetIsVisible(ExtensionAction::kDefaultTabId, false);
320 363
321 // TODO(EXTENSIONS_DEPRECATED): icons list is obsolete. 364 // TODO(EXTENSIONS_DEPRECATED): icons list is obsolete.
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 if (start_pos == std::string::npos) 484 if (start_pos == std::string::npos)
442 return ExtensionResource(); 485 return ExtensionResource();
443 std::string trimmed_path = relative_path.substr(start_pos); 486 std::string trimmed_path = relative_path.substr(start_pos);
444 487
445 FilePath relative_resource_path; 488 FilePath relative_resource_path;
446 return ExtensionResource(extension_path, 489 return ExtensionResource(extension_path,
447 relative_resource_path.AppendASCII(trimmed_path)); 490 relative_resource_path.AppendASCII(trimmed_path));
448 } 491 }
449 492
450 Extension::Extension(const FilePath& path) 493 Extension::Extension(const FilePath& path)
451 : is_theme_(false), background_page_ready_(false) { 494 : is_theme_(false), background_page_ready_(false),
495 converted_from_user_script_(false) {
452 DCHECK(path.IsAbsolute()); 496 DCHECK(path.IsAbsolute());
453 location_ = INVALID; 497 location_ = INVALID;
454 498
455 #if defined(OS_WIN) 499 #if defined(OS_WIN)
456 // Normalize any drive letter to upper-case. We do this for consistency with 500 // Normalize any drive letter to upper-case. We do this for consistency with
457 // net_utils::FilePathToFileURL(), which does the same thing, to make string 501 // net_utils::FilePathToFileURL(), which does the same thing, to make string
458 // comparisons simpler. 502 // comparisons simpler.
459 std::wstring path_str = path.value(); 503 std::wstring path_str = path.value();
460 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' && 504 if (path_str.size() >= 2 && path_str[0] >= L'a' && path_str[0] <= L'z' &&
461 path_str[1] == ':') 505 path_str[1] == ':')
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
659 return false; 703 return false;
660 } 704 }
661 update_url_ = GURL(tmp); 705 update_url_ = GURL(tmp);
662 if (!update_url_.is_valid() || update_url_.has_ref()) { 706 if (!update_url_.is_valid() || update_url_.has_ref()) {
663 *error = ExtensionErrorUtils::FormatErrorMessage( 707 *error = ExtensionErrorUtils::FormatErrorMessage(
664 errors::kInvalidUpdateURL, tmp); 708 errors::kInvalidUpdateURL, tmp);
665 return false; 709 return false;
666 } 710 }
667 } 711 }
668 712
713 // Initialize converted_from_user_script (if present)
714 source.GetBoolean(keys::kConvertedFromUserScript,
715 &converted_from_user_script_);
716
669 // Initialize icons (if present). 717 // Initialize icons (if present).
670 if (source.HasKey(keys::kIcons)) { 718 if (source.HasKey(keys::kIcons)) {
671 DictionaryValue* icons_value = NULL; 719 DictionaryValue* icons_value = NULL;
672 if (!source.GetDictionary(keys::kIcons, &icons_value)) { 720 if (!source.GetDictionary(keys::kIcons, &icons_value)) {
673 *error = errors::kInvalidIcons; 721 *error = errors::kInvalidIcons;
674 return false; 722 return false;
675 } 723 }
676 724
677 for (size_t i = 0; i < arraysize(kIconSizes); ++i) { 725 for (size_t i = 0; i < arraysize(kIconSizes); ++i) {
678 std::wstring key = ASCIIToWide(IntToString(kIconSizes[i])); 726 std::wstring key = ASCIIToWide(IntToString(kIconSizes[i]));
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 if (!list_value->GetDictionary(i, &content_script)) { 982 if (!list_value->GetDictionary(i, &content_script)) {
935 *error = ExtensionErrorUtils::FormatErrorMessage( 983 *error = ExtensionErrorUtils::FormatErrorMessage(
936 errors::kInvalidContentScript, IntToString(i)); 984 errors::kInvalidContentScript, IntToString(i));
937 return false; 985 return false;
938 } 986 }
939 987
940 UserScript script; 988 UserScript script;
941 if (!LoadUserScriptHelper(content_script, i, error, &script)) 989 if (!LoadUserScriptHelper(content_script, i, error, &script))
942 return false; // Failed to parse script context definition 990 return false; // Failed to parse script context definition
943 script.set_extension_id(id()); 991 script.set_extension_id(id());
992 if (converted_from_user_script_)
993 script.set_emulate_greasemonkey(true);
944 content_scripts_.push_back(script); 994 content_scripts_.push_back(script);
945 } 995 }
946 } 996 }
947 997
948 // Initialize page action (optional). 998 // Initialize page action (optional).
949 if (source.HasKey(keys::kPageActions)) { 999 if (source.HasKey(keys::kPageActions)) {
950 ListValue* list_value; 1000 ListValue* list_value;
951 if (!source.GetList(keys::kPageActions, &list_value)) { 1001 if (!source.GetList(keys::kPageActions, &list_value)) {
952 *error = errors::kInvalidPageActionsList; 1002 *error = errors::kInvalidPageActionsList;
953 return false; 1003 return false;
(...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after
1190 UserScript::PatternList::const_iterator pattern = 1240 UserScript::PatternList::const_iterator pattern =
1191 content_script->url_patterns().begin(); 1241 content_script->url_patterns().begin();
1192 for (; pattern != content_script->url_patterns().end(); ++pattern) { 1242 for (; pattern != content_script->url_patterns().end(); ++pattern) {
1193 if (pattern->match_subdomains() && pattern->host().empty()) 1243 if (pattern->match_subdomains() && pattern->host().empty())
1194 return true; 1244 return true;
1195 } 1245 }
1196 } 1246 }
1197 1247
1198 return false; 1248 return false;
1199 } 1249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698