| Index: chrome/browser/extensions/api/declarative_content/content_action.cc
|
| diff --git a/chrome/browser/extensions/api/declarative_content/content_action.cc b/chrome/browser/extensions/api/declarative_content/content_action.cc
|
| index 298f504fd257a7c23c8f6f59c904b9382ccac3c7..47deb9ea12cbb5d03d14ff15032bf23af74e9ac2 100644
|
| --- a/chrome/browser/extensions/api/declarative_content/content_action.cc
|
| +++ b/chrome/browser/extensions/api/declarative_content/content_action.cc
|
| @@ -11,14 +11,21 @@
|
| #include "base/values.h"
|
| #include "chrome/browser/extensions/api/declarative_content/content_constants.h"
|
| #include "chrome/browser/extensions/api/extension_action/extension_action_api.h"
|
| +#include "chrome/browser/extensions/declarative_user_script_master.h"
|
| #include "chrome/browser/extensions/extension_action.h"
|
| #include "chrome/browser/extensions/extension_action_manager.h"
|
| #include "chrome/browser/extensions/extension_tab_util.h"
|
| #include "chrome/browser/profiles/profile.h"
|
| +#include "chrome/browser/sessions/session_tab_helper.h"
|
| +#include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h"
|
| #include "content/public/browser/invalidate_type.h"
|
| +#include "content/public/browser/render_process_host.h"
|
| +#include "content/public/browser/render_view_host.h"
|
| #include "content/public/browser/web_contents.h"
|
| #include "extensions/browser/extension_registry.h"
|
| +#include "extensions/browser/extension_system.h"
|
| #include "extensions/common/extension.h"
|
| +#include "extensions/common/extension_messages.h"
|
|
|
| namespace extensions {
|
|
|
| @@ -48,7 +55,8 @@ class ShowPageAction : public ContentAction {
|
| public:
|
| ShowPageAction() {}
|
|
|
| - static scoped_refptr<ContentAction> Create(const Extension* extension,
|
| + static scoped_refptr<ContentAction> Create(Profile* profile,
|
| + const Extension* extension,
|
| const base::DictionaryValue* dict,
|
| std::string* error,
|
| bool* bad_message) {
|
| @@ -80,6 +88,10 @@ class ShowPageAction : public ContentAction {
|
| action, apply_info->tab);
|
| }
|
| }
|
| + virtual void Reapply(const std::string& extension_id,
|
| + const base::Time& extension_install_time,
|
| + ApplyInfo* apply_info) const OVERRIDE {
|
| + }
|
|
|
| private:
|
| static ExtensionAction* GetPageAction(Profile* profile,
|
| @@ -99,20 +111,19 @@ class ShowPageAction : public ContentAction {
|
| // Action that injects a content script.
|
| class RequestContentScript : public ContentAction {
|
| public:
|
| - RequestContentScript(const std::vector<std::string>& css_file_names,
|
| - const std::vector<std::string>& js_file_names,
|
| - bool all_frames,
|
| - bool match_about_blank)
|
| - : css_file_names_(css_file_names),
|
| - js_file_names_(js_file_names),
|
| - all_frames_(all_frames),
|
| - match_about_blank_(match_about_blank) {}
|
| -
|
| - static scoped_refptr<ContentAction> Create(const Extension* extension,
|
| + static scoped_refptr<ContentAction> Create(Profile* profile,
|
| + const Extension* extension,
|
| const base::DictionaryValue* dict,
|
| std::string* error,
|
| bool* bad_message);
|
|
|
| + RequestContentScript(Profile* profile,
|
| + const Extension* extension,
|
| + const std::vector<std::string>& css_file_names,
|
| + const std::vector<std::string>& js_file_names,
|
| + bool all_frames,
|
| + bool match_about_blank);
|
| +
|
| // Implementation of ContentAction:
|
| virtual Type GetType() const OVERRIDE {
|
| return ACTION_REQUEST_CONTENT_SCRIPT;
|
| @@ -121,24 +132,30 @@ class RequestContentScript : public ContentAction {
|
| virtual void Apply(const std::string& extension_id,
|
| const base::Time& extension_install_time,
|
| ApplyInfo* apply_info) const OVERRIDE {
|
| - // TODO(markdittmer): Invoke UserScriptMaster declarative script loader:
|
| - // load new user script.
|
| + InstructRenderProcessToInject(apply_info->tab, extension_id);
|
| }
|
|
|
| virtual void Revert(const std::string& extension_id,
|
| const base::Time& extension_install_time,
|
| ApplyInfo* apply_info) const OVERRIDE {
|
| - // TODO(markdittmer): Invoke UserScriptMaster declarative script loader:
|
| - // do not load user script if Apply() runs again on the same page.
|
| + }
|
| + virtual void Reapply(const std::string& extension_id,
|
| + const base::Time& extension_install_time,
|
| + ApplyInfo* apply_info) const OVERRIDE {
|
| + InstructRenderProcessToInject(apply_info->tab, extension_id);
|
| }
|
|
|
| private:
|
| - virtual ~RequestContentScript() {}
|
| + virtual ~RequestContentScript() {
|
| + DCHECK(master_);
|
| + master_->RemoveScript(script_);
|
| + }
|
|
|
| - std::vector<std::string> css_file_names_;
|
| - std::vector<std::string> js_file_names_;
|
| - bool all_frames_;
|
| - bool match_about_blank_;
|
| + void InstructRenderProcessToInject(content::WebContents* contents,
|
| + const std::string& extension_id) const;
|
| +
|
| + UserScript script_;
|
| + DeclarativeUserScriptMaster* master_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
|
| };
|
| @@ -162,6 +179,7 @@ static bool AppendJSStringsToCPPStrings(const base::ListValue& append_strings,
|
|
|
| // static
|
| scoped_refptr<ContentAction> RequestContentScript::Create(
|
| + Profile* profile,
|
| const Extension* extension,
|
| const base::DictionaryValue* dict,
|
| std::string* error,
|
| @@ -195,7 +213,52 @@ scoped_refptr<ContentAction> RequestContentScript::Create(
|
| }
|
|
|
| return scoped_refptr<ContentAction>(new RequestContentScript(
|
| - css_file_names, js_file_names, all_frames, match_about_blank));
|
| + profile,
|
| + extension,
|
| + css_file_names,
|
| + js_file_names,
|
| + all_frames,
|
| + match_about_blank));
|
| +}
|
| +
|
| +RequestContentScript::RequestContentScript(
|
| + Profile* profile,
|
| + const Extension* extension,
|
| + const std::vector<std::string>& css_file_names,
|
| + const std::vector<std::string>& js_file_names,
|
| + bool all_frames,
|
| + bool match_about_blank) {
|
| + script_.set_id(ContentScriptsHandler::GetNextUserScriptID());
|
| + script_.set_extension_id(extension->id());
|
| + script_.set_run_location(UserScript::BROWSER_DRIVEN);
|
| + script_.set_match_all_frames(all_frames);
|
| + script_.set_match_about_blank(match_about_blank);
|
| + for (std::vector<std::string>::const_iterator it = css_file_names.begin();
|
| + it != css_file_names.end(); ++it) {
|
| + GURL url = extension->GetResourceURL(*it);
|
| + ExtensionResource resource = extension->GetResource(*it);
|
| + script_.css_scripts().push_back(UserScript::File(
|
| + resource.extension_root(), resource.relative_path(), url));
|
| + }
|
| + for (std::vector<std::string>::const_iterator it = js_file_names.begin();
|
| + it != js_file_names.end(); ++it) {
|
| + GURL url = extension->GetResourceURL(*it);
|
| + ExtensionResource resource = extension->GetResource(*it);
|
| + script_.js_scripts().push_back(UserScript::File(
|
| + resource.extension_root(), resource.relative_path(), url));
|
| + }
|
| +
|
| + master_ =
|
| + ExtensionSystem::Get(profile)->GetDeclarativeUserScriptMasterByExtension(
|
| + extension->id());
|
| + DCHECK(master_);
|
| + master_->AddScript(script_);
|
| +}
|
| +
|
| +void RequestContentScript::InstructRenderProcessToInject(
|
| + content::WebContents* contents,
|
| + const std::string& extension_id) const {
|
| + // TODO(markdittmer): Send ExtensionMsg to renderer.
|
| }
|
|
|
| struct ContentActionFactory {
|
| @@ -206,6 +269,7 @@ struct ContentActionFactory {
|
| // semantically incorrect. |bad_message| is set to true in case |dict| does
|
| // not confirm to the validated JSON specification.
|
| typedef scoped_refptr<ContentAction>(*FactoryMethod)(
|
| + Profile* /* profile */,
|
| const Extension* /* extension */,
|
| const base::DictionaryValue* /* dict */,
|
| std::string* /* error */,
|
| @@ -237,6 +301,7 @@ ContentAction::~ContentAction() {}
|
|
|
| // static
|
| scoped_refptr<ContentAction> ContentAction::Create(
|
| + Profile* profile,
|
| const Extension* extension,
|
| const base::Value& json_action,
|
| std::string* error,
|
| @@ -256,7 +321,7 @@ scoped_refptr<ContentAction> ContentAction::Create(
|
| factory_method_iter = factory.factory_methods.find(instance_type);
|
| if (factory_method_iter != factory.factory_methods.end())
|
| return (*factory_method_iter->second)(
|
| - extension, action_dict, error, bad_message);
|
| + profile, extension, action_dict, error, bad_message);
|
|
|
| *error = base::StringPrintf(kInvalidInstanceTypeError, instance_type.c_str());
|
| return scoped_refptr<ContentAction>();
|
|
|