diff options
author | hanxi <hanxi@chromium.org> | 2015-01-27 15:38:46 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-01-27 23:40:18 +0000 |
commit | b88fe3dc1072501bdd105fd95a8b3bc453fd2aa7 (patch) | |
tree | e4f8aecf3319c52d1b5e32d56ffe28191b45faf9 /extensions | |
parent | 86b7486d902f7e1389f8a3dad9ff42ee68134076 (diff) | |
download | chromium_src-b88fe3dc1072501bdd105fd95a8b3bc453fd2aa7.zip chromium_src-b88fe3dc1072501bdd105fd95a8b3bc453fd2aa7.tar.gz chromium_src-b88fe3dc1072501bdd105fd95a8b3bc453fd2aa7.tar.bz2 |
Introduce HostID and de-couple Extensions from "script injection System" [browser side]
The major refactor includes:
- Introduce HostID (a pair of |id, type|) to replace extension_id in browser
side.
- Abstract UserScriptLoader to be a base class, and introduces
a derived class ExtensionUserScriptLoader which is
responsible for loading user scripts for extensions.
- In DeclarativeUserScriptManager, a master is created per
extension/webUI.
- DeclarativeUserScriptManager becomes an
ExtensionRegistryObserver and is responsible for clearing scripts
for master objects when receive OnExensionUnloaded event.
BUG=437566
Review URL: https://codereview.chromium.org/822453002
Cr-Commit-Position: refs/heads/master@{#313402}
Diffstat (limited to 'extensions')
-rw-r--r-- | extensions/browser/api/declarative/declarative_rule.h | 12 | ||||
-rw-r--r-- | extensions/browser/api/declarative/declarative_rule_unittest.cc | 41 | ||||
-rw-r--r-- | extensions/browser/api/declarative_webrequest/webrequest_action.cc | 1 | ||||
-rw-r--r-- | extensions/browser/api/declarative_webrequest/webrequest_action.h | 1 | ||||
-rw-r--r-- | extensions/browser/api/declarative_webrequest/webrequest_rules_registry.cc | 3 | ||||
-rw-r--r-- | extensions/common/BUILD.gn | 1 | ||||
-rw-r--r-- | extensions/common/host_id.h | 39 | ||||
-rw-r--r-- | extensions/common/user_script.cc | 19 | ||||
-rw-r--r-- | extensions/common/user_script.h | 19 | ||||
-rw-r--r-- | extensions/common/user_script_unittest.cc | 3 | ||||
-rw-r--r-- | extensions/extensions.gyp | 1 |
11 files changed, 110 insertions, 30 deletions
diff --git a/extensions/browser/api/declarative/declarative_rule.h b/extensions/browser/api/declarative/declarative_rule.h index b612f27..8dd3837 100644 --- a/extensions/browser/api/declarative/declarative_rule.h +++ b/extensions/browser/api/declarative/declarative_rule.h @@ -23,6 +23,7 @@ #include "components/url_matcher/url_matcher.h" #include "extensions/common/api/events.h" #include "extensions/common/extension.h" +#include "extensions/common/host_id.h" namespace base { class Time; @@ -152,6 +153,7 @@ class DeclarativeActionSet { // the extension API. static scoped_ptr<DeclarativeActionSet> Create( content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, const AnyVector& actions, std::string* error, @@ -230,6 +232,7 @@ class DeclarativeRule { static scoped_ptr<DeclarativeRule> Create( url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, base::Time extension_installation_time, linked_ptr<JsonRule> rule, @@ -366,6 +369,7 @@ template<typename ActionT> scoped_ptr<DeclarativeActionSet<ActionT> > DeclarativeActionSet<ActionT>::Create( content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, const AnyVector& actions, std::string* error, @@ -378,7 +382,8 @@ DeclarativeActionSet<ActionT>::Create( i != actions.end(); ++i) { CHECK(i->get()); scoped_refptr<const ActionT> action = - ActionT::Create(browser_context, extension, **i, error, bad_message); + ActionT::Create( + browser_context, host_id, extension, **i, error, bad_message); if (!error->empty() || *bad_message) return scoped_ptr<DeclarativeActionSet>(); result.push_back(action); @@ -455,6 +460,7 @@ scoped_ptr<DeclarativeRule<ConditionT, ActionT> > DeclarativeRule<ConditionT, ActionT>::Create( url_matcher::URLMatcherConditionFactory* url_matcher_condition_factory, content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, base::Time extension_installation_time, linked_ptr<JsonRule> rule, @@ -470,8 +476,8 @@ DeclarativeRule<ConditionT, ActionT>::Create( bool bad_message = false; scoped_ptr<ActionSet> actions = - ActionSet::Create( - browser_context, extension, rule->actions, error, &bad_message); + ActionSet::Create(browser_context, host_id, extension, + rule->actions, error, &bad_message); if (bad_message) { // TODO(battre) Export concept of bad_message to caller, the extension // should be killed in case it is true. diff --git a/extensions/browser/api/declarative/declarative_rule_unittest.cc b/extensions/browser/api/declarative/declarative_rule_unittest.cc index 552145d..3c4163b 100644 --- a/extensions/browser/api/declarative/declarative_rule_unittest.cc +++ b/extensions/browser/api/declarative/declarative_rule_unittest.cc @@ -219,6 +219,7 @@ class SummingAction : public base::RefCounted<SummingAction> { static scoped_refptr<const SummingAction> Create( content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, const base::Value& action, std::string* error, @@ -270,7 +271,7 @@ TEST(DeclarativeActionTest, ErrorActionSet) { std::string error; bool bad = false; scoped_ptr<SummingActionSet> result = - SummingActionSet::Create(NULL, NULL, actions, &error, &bad); + SummingActionSet::Create(NULL, HostID(), NULL, actions, &error, &bad); EXPECT_EQ("the error", error); EXPECT_FALSE(bad); EXPECT_FALSE(result); @@ -278,7 +279,8 @@ TEST(DeclarativeActionTest, ErrorActionSet) { actions.clear(); actions.push_back(ScopedToLinkedPtr(ParseJson("{\"value\": 1}"))); actions.push_back(ScopedToLinkedPtr(ParseJson("{\"bad\": 3}"))); - result = SummingActionSet::Create(NULL, NULL, actions, &error, &bad); + result = SummingActionSet::Create( + NULL, HostID(), NULL, actions, &error, &bad); EXPECT_EQ("", error); EXPECT_TRUE(bad); EXPECT_FALSE(result); @@ -295,7 +297,7 @@ TEST(DeclarativeActionTest, ApplyActionSet) { std::string error; bool bad = false; scoped_ptr<SummingActionSet> result = - SummingActionSet::Create(NULL, NULL, actions, &error, &bad); + SummingActionSet::Create(NULL, HostID(), NULL, actions, &error, &bad); EXPECT_EQ("", error); EXPECT_FALSE(bad); ASSERT_TRUE(result); @@ -336,13 +338,15 @@ TEST(DeclarativeRuleTest, Create) { URLMatcher matcher; std::string error; - scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), - NULL, - extension.get(), - install_time, - json_rule, - Rule::ConsistencyChecker(), - &error)); + scoped_ptr<Rule> rule( + Rule::Create(matcher.condition_factory(), + NULL, + HostID(HostID::EXTENSIONS, extension.get()->id()), + extension.get(), + install_time, + json_rule, + Rule::ConsistencyChecker(), + &error)); EXPECT_EQ("", error); ASSERT_TRUE(rule.get()); @@ -405,13 +409,15 @@ TEST(DeclarativeRuleTest, CheckConsistency) { " \"priority\": 200 \n" "}"), json_rule.get())); - scoped_ptr<Rule> rule(Rule::Create(matcher.condition_factory(), - NULL, - extension.get(), - base::Time(), - json_rule, - base::Bind(AtLeastOneCondition), - &error)); + scoped_ptr<Rule> rule( + Rule::Create(matcher.condition_factory(), + NULL, + HostID(HostID::EXTENSIONS, extension.get()->id()), + extension.get(), + base::Time(), + json_rule, + base::Bind(AtLeastOneCondition), + &error)); EXPECT_TRUE(rule); EXPECT_EQ("", error); @@ -430,6 +436,7 @@ TEST(DeclarativeRuleTest, CheckConsistency) { json_rule.get())); rule = Rule::Create(matcher.condition_factory(), NULL, + HostID(HostID::EXTENSIONS, extension.get()->id()), extension.get(), base::Time(), json_rule, diff --git a/extensions/browser/api/declarative_webrequest/webrequest_action.cc b/extensions/browser/api/declarative_webrequest/webrequest_action.cc index 1a777ce..6d79227 100644 --- a/extensions/browser/api/declarative_webrequest/webrequest_action.cc +++ b/extensions/browser/api/declarative_webrequest/webrequest_action.cc @@ -514,6 +514,7 @@ bool WebRequestAction::HasPermission(const InfoMap* extension_info_map, // static scoped_refptr<const WebRequestAction> WebRequestAction::Create( content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, const base::Value& json_action, std::string* error, diff --git a/extensions/browser/api/declarative_webrequest/webrequest_action.h b/extensions/browser/api/declarative_webrequest/webrequest_action.h index 03fc796..fa01fd3 100644 --- a/extensions/browser/api/declarative_webrequest/webrequest_action.h +++ b/extensions/browser/api/declarative_webrequest/webrequest_action.h @@ -135,6 +135,7 @@ class WebRequestAction : public base::RefCounted<WebRequestAction> { // in case the input is syntactically unexpected. static scoped_refptr<const WebRequestAction> Create( content::BrowserContext* browser_context, + const HostID& host_id, const Extension* extension, const base::Value& json_action, std::string* error, diff --git a/extensions/browser/api/declarative_webrequest/webrequest_rules_registry.cc b/extensions/browser/api/declarative_webrequest/webrequest_rules_registry.cc index ae6f531..dcb0e6e 100644 --- a/extensions/browser/api/declarative_webrequest/webrequest_rules_registry.cc +++ b/extensions/browser/api/declarative_webrequest/webrequest_rules_registry.cc @@ -180,7 +180,8 @@ std::string WebRequestRulesRegistry::AddRulesImpl( DCHECK(registered_rules.find(rule_id) == registered_rules.end()); scoped_ptr<WebRequestRule> webrequest_rule(WebRequestRule::Create( - url_matcher_.condition_factory(), browser_context(), extension, + url_matcher_.condition_factory(), browser_context(), + HostID(HostID::EXTENSIONS, extension->id()), extension, extension_installation_time, *rule, base::Bind(&Checker, base::Unretained(extension)), &error)); if (!error.empty()) { diff --git a/extensions/common/BUILD.gn b/extensions/common/BUILD.gn index c23df44..ebfd09d 100644 --- a/extensions/common/BUILD.gn +++ b/extensions/common/BUILD.gn @@ -109,6 +109,7 @@ if (enable_extensions) { "file_util.h", "guest_view/guest_view_constants.cc", "guest_view/guest_view_constants.h", + "host_id.h", "image_util.cc", "image_util.h", "install_warning.cc", diff --git a/extensions/common/host_id.h b/extensions/common/host_id.h new file mode 100644 index 0000000..0044807 --- /dev/null +++ b/extensions/common/host_id.h @@ -0,0 +1,39 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef EXTENSIONS_COMMON_HOST_ID_H_ +#define EXTENSIONS_COMMON_HOST_ID_H_ + +#include <string> + +// IDs of hosts who own user scripts. +// A HostID is immutable after creation. +struct HostID { + enum HostType { EXTENSIONS, WEBUI }; + + HostID() {} + HostID(HostType type, const std::string& id) + : type_(type), id_(id) {} + + bool operator<(const HostID& host_id) const { + if (type_ != host_id.type()) + return type_ < host_id.type(); + else if (id_ != host_id.id()) + return id_ < host_id.id(); + return false; + } + + HostType type() const { return type_; } + const std::string& id() const { return id_; } + + private: + // The type of the host. + HostType type_; + + // Similar to extension_id, host_id is a unique indentifier for a host, + // e.g., an Extension or WebUI. + std::string id_; +}; + +#endif // EXTENSIONS_COMMON_HOST_ID_H_ diff --git a/extensions/common/user_script.cc b/extensions/common/user_script.cc index 4410290..b8ba22e 100644 --- a/extensions/common/user_script.cc +++ b/extensions/common/user_script.cc @@ -138,13 +138,13 @@ void UserScript::File::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { void UserScript::Pickle(::Pickle* pickle) const { // Write the simple types to the pickle. pickle->WriteInt(run_location()); - pickle->WriteString(extension_id()); pickle->WriteInt(user_script_id_); pickle->WriteBool(emulate_greasemonkey()); pickle->WriteBool(match_all_frames()); pickle->WriteBool(match_about_blank()); pickle->WriteBool(is_incognito_enabled()); + PickleHostID(pickle, host_id_); PickleGlobs(pickle, globs_); PickleGlobs(pickle, exclude_globs_); PickleURLPatternSet(pickle, url_set_); @@ -162,6 +162,11 @@ void UserScript::PickleGlobs(::Pickle* pickle, } } +void UserScript::PickleHostID(::Pickle* pickle, const HostID& host_id) const { + pickle->WriteInt(host_id.type()); + pickle->WriteString(host_id.id()); +} + void UserScript::PickleURLPatternSet(::Pickle* pickle, const URLPatternSet& pattern_list) const { pickle->WriteSizeT(pattern_list.patterns().size()); @@ -188,13 +193,13 @@ void UserScript::Unpickle(const ::Pickle& pickle, PickleIterator* iter) { CHECK(run_location >= 0 && run_location < RUN_LOCATION_LAST); run_location_ = static_cast<RunLocation>(run_location); - CHECK(iter->ReadString(&extension_id_)); CHECK(iter->ReadInt(&user_script_id_)); CHECK(iter->ReadBool(&emulate_greasemonkey_)); CHECK(iter->ReadBool(&match_all_frames_)); CHECK(iter->ReadBool(&match_about_blank_)); CHECK(iter->ReadBool(&incognito_enabled_)); + UnpickleHostID(pickle, iter, &host_id_); UnpickleGlobs(pickle, iter, &globs_); UnpickleGlobs(pickle, iter, &exclude_globs_); UnpickleURLPatternSet(pickle, iter, &url_set_); @@ -215,6 +220,16 @@ void UserScript::UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, } } +void UserScript::UnpickleHostID(const ::Pickle& pickle, + PickleIterator* iter, + HostID* host_id) { + int type = 0; + std::string id; + CHECK(iter->ReadInt(&type)); + CHECK(iter->ReadString(&id)); + *host_id = HostID(static_cast<HostID::HostType>(type), id); +} + void UserScript::UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter, URLPatternSet* pattern_list) { diff --git a/extensions/common/user_script.h b/extensions/common/user_script.h index 5a27315..6069161 100644 --- a/extensions/common/user_script.h +++ b/extensions/common/user_script.h @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/files/file_path.h" #include "base/strings/string_piece.h" +#include "extensions/common/host_id.h" #include "extensions/common/url_pattern.h" #include "extensions/common/url_pattern_set.h" #include "url/gurl.h" @@ -194,8 +195,10 @@ class UserScript { FileList& css_scripts() { return css_scripts_; } const FileList& css_scripts() const { return css_scripts_; } - const std::string& extension_id() const { return extension_id_; } - void set_extension_id(const std::string& id) { extension_id_ = id; } + const std::string& extension_id() const { return host_id_.id(); } + + const HostID& host_id() const { return host_id_; } + void set_host_id(const HostID& host_id) { host_id_ = host_id; } int id() const { return user_script_id_; } void set_id(int id) { user_script_id_ = id; } @@ -203,7 +206,7 @@ class UserScript { bool is_incognito_enabled() const { return incognito_enabled_; } void set_incognito_enabled(bool enabled) { incognito_enabled_ = enabled; } - bool is_standalone() const { return extension_id_.empty(); } + bool is_standalone() const { return extension_id().empty(); } // Returns true if the script should be applied to the specified URL, false // otherwise. @@ -222,6 +225,7 @@ class UserScript { // Pickle helper functions used to pickle the individual types of components. void PickleGlobs(::Pickle* pickle, const std::vector<std::string>& globs) const; + void PickleHostID(::Pickle* pickle, const HostID& host_id) const; void PickleURLPatternSet(::Pickle* pickle, const URLPatternSet& pattern_list) const; void PickleScripts(::Pickle* pickle, const FileList& scripts) const; @@ -229,6 +233,9 @@ class UserScript { // Unpickle helper functions used to unpickle individual types of components. void UnpickleGlobs(const ::Pickle& pickle, PickleIterator* iter, std::vector<std::string>* globs); + void UnpickleHostID(const ::Pickle& pickle, + PickleIterator* iter, + HostID* host_id); void UnpickleURLPatternSet(const ::Pickle& pickle, PickleIterator* iter, URLPatternSet* pattern_list); void UnpickleScripts(const ::Pickle& pickle, PickleIterator* iter, @@ -267,9 +274,9 @@ class UserScript { // List of css scripts defined in content_scripts FileList css_scripts_; - // The ID of the extension this script is a part of, if any. Can be empty if - // the script is a "standlone" user script. - std::string extension_id_; + // The ID of the host this script is a part of. The |ID| of the + // |host_id| can be empty if the script is a "standlone" user script. + HostID host_id_; // The globally-unique id associated with this user script. Defaults to // -1 for invalid. diff --git a/extensions/common/user_script_unittest.cc b/extensions/common/user_script_unittest.cc index 589b555..4a964f7 100644 --- a/extensions/common/user_script_unittest.cc +++ b/extensions/common/user_script_unittest.cc @@ -193,7 +193,8 @@ TEST(ExtensionUserScriptTest, Pickle) { const int64 kId = 12; script1.set_id(kId); const std::string kExtensionId = "foo"; - script1.set_extension_id(kExtensionId); + HostID id(HostID::EXTENSIONS, kExtensionId); + script1.set_host_id(id); Pickle pickle; script1.Pickle(&pickle); diff --git a/extensions/extensions.gyp b/extensions/extensions.gyp index 1197c5c..0977c5d 100644 --- a/extensions/extensions.gyp +++ b/extensions/extensions.gyp @@ -88,6 +88,7 @@ 'common/cast/cast_cert_validator.h', 'common/common_manifest_handlers.cc', 'common/common_manifest_handlers.h', + 'common/host_id.h', 'common/csp_validator.cc', 'common/csp_validator.h', 'common/dom_action_types.h', |