Chromium Code Reviews| 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 4cc5fbe412d5ade4068e9ac083482bba66cb5fb8..8598de7395031e7d2ff624f7d6ad797d9d0263d8 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" |
|
Jeffrey Yasskin
2014/08/22 01:12:24
Double-check which headers you still need.
Mark Dittmer
2014/08/22 19:59:19
Done.
|
| +#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) { |
| @@ -84,6 +92,10 @@ class ShowPageAction : public ContentAction { |
| action, apply_info->tab, apply_info->profile); |
| } |
| } |
| + 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, |
| @@ -103,20 +115,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, |
|
Jeffrey Yasskin
2014/08/22 01:12:24
Try not to reorder blocks unnecessarily.
Mark Dittmer
2014/08/22 19:59:18
Done.
|
| + 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; |
| @@ -125,8 +136,7 @@ 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 Reapply(const std::string& extension_id, |
| @@ -139,17 +149,24 @@ class RequestContentScript : public ContentAction { |
| 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); |
| }; |
| @@ -173,6 +190,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, |
| @@ -206,7 +224,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( |
|
Jeffrey Yasskin
2014/08/22 01:12:24
Can you point to the code that shows that master_
Devlin
2014/08/22 19:25:45
Jumping in, since Mark was chatting with me about
|
| + 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 { |
| @@ -217,6 +280,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 */, |
| @@ -248,6 +312,7 @@ ContentAction::~ContentAction() {} |
| // static |
| scoped_refptr<ContentAction> ContentAction::Create( |
| + Profile* profile, |
| const Extension* extension, |
| const base::Value& json_action, |
| std::string* error, |
| @@ -267,7 +332,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>(); |