1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_ 7 8 #include <string> 9 #include <vector> 10 11 #include "base/memory/ref_counted.h" 12 #include "chrome/browser/extensions/declarative_user_script_master.h" 13 #include "extensions/browser/api/declarative/declarative_rule.h" 14 15 namespace base { 16 class Time; 17 class Value; 18 } 19 20 namespace content { 21 class BrowserContext; 22 class WebContents; 23 } 24 25 namespace extensions { 26 class Extension; 27 28 // Base class for all ContentActions of the declarative content API. 29 class ContentAction : public base::RefCounted<ContentAction> { 30 public: 31 // Type identifiers for concrete ContentActions. 32 enum Type { 33 ACTION_SHOW_PAGE_ACTION, 34 ACTION_REQUEST_CONTENT_SCRIPT, 35 ACTION_SET_ICON, 36 }; 37 38 struct ApplyInfo { 39 content::BrowserContext* browser_context; 40 content::WebContents* tab; 41 int priority; 42 }; 43 44 ContentAction(); 45 46 virtual Type GetType() const = 0; 47 48 // Applies or reverts this ContentAction on a particular tab for a particular 49 // extension. Revert exists to keep the actions up to date as the page 50 // changes. Reapply exists to reapply changes to a new page, even if the 51 // previous page also matched relevant conditions. 52 virtual void Apply(const std::string& extension_id, 53 const base::Time& extension_install_time, 54 ApplyInfo* apply_info) const = 0; 55 virtual void Reapply(const std::string& extension_id, 56 const base::Time& extension_install_time, 57 ApplyInfo* apply_info) const = 0; 58 virtual void Revert(const std::string& extension_id, 59 const base::Time& extension_install_time, 60 ApplyInfo* apply_info) const = 0; 61 62 // Factory method that instantiates a concrete ContentAction 63 // implementation according to |json_action|, the representation of the 64 // ContentAction as received from the extension API. 65 // Sets |error| and returns NULL in case of a semantic error that cannot 66 // be caught by schema validation. Sets |bad_message| and returns NULL 67 // in case the input is syntactically unexpected. 68 static scoped_refptr<ContentAction> Create( 69 content::BrowserContext* browser_context, 70 const Extension* extension, 71 const base::Value& json_action, 72 std::string* error, 73 bool* bad_message); 74 75 // Shared procedure for resetting error state within factories. 76 static void ResetErrorData(std::string* error, bool* bad_message) { 77 *error = ""; 78 *bad_message = false; 79 } 80 81 // Shared procedure for validating JSON data. 82 static bool Validate(const base::Value& json_action, 83 std::string* error, 84 bool* bad_message, 85 const base::DictionaryValue** action_dict, 86 std::string* instance_type); 87 88 protected: 89 friend class base::RefCounted<ContentAction>; 90 virtual ~ContentAction(); 91 }; 92 93 // Action that injects a content script. 94 class RequestContentScript : public ContentAction { 95 public: 96 struct ScriptData; 97 98 RequestContentScript(content::BrowserContext* browser_context, 99 const Extension* extension, 100 const ScriptData& script_data); 101 RequestContentScript(DeclarativeUserScriptMaster* master, 102 const Extension* extension, 103 const ScriptData& script_data); 104 105 static scoped_refptr<ContentAction> Create( 106 content::BrowserContext* browser_context, 107 const Extension* extension, 108 const base::DictionaryValue* dict, 109 std::string* error, 110 bool* bad_message); 111 112 static scoped_refptr<ContentAction> CreateForTest( 113 DeclarativeUserScriptMaster* master, 114 const Extension* extension, 115 const base::Value& json_action, 116 std::string* error, 117 bool* bad_message); 118 119 static bool InitScriptData(const base::DictionaryValue* dict, 120 std::string* error, 121 bool* bad_message, 122 ScriptData* script_data); 123 124 // Implementation of ContentAction: 125 virtual Type GetType() const OVERRIDE; 126 127 virtual void Apply(const std::string& extension_id, 128 const base::Time& extension_install_time, 129 ApplyInfo* apply_info) const OVERRIDE; 130 131 virtual void Reapply(const std::string& extension_id, 132 const base::Time& extension_install_time, 133 ApplyInfo* apply_info) const OVERRIDE; 134 135 virtual void Revert(const std::string& extension_id, 136 const base::Time& extension_install_time, 137 ApplyInfo* apply_info) const OVERRIDE; 138 139 private: 140 void InitScript(const Extension* extension, const ScriptData& script_data); 141 142 void AddScript() { 143 DCHECK(master_); 144 master_->AddScript(script_); 145 } 146 147 virtual ~RequestContentScript(); 148 149 void InstructRenderProcessToInject(content::WebContents* contents, 150 const std::string& extension_id) const; 151 152 UserScript script_; 153 DeclarativeUserScriptMaster* master_; 154 155 DISALLOW_COPY_AND_ASSIGN(RequestContentScript); 156 }; 157 158 typedef DeclarativeActionSet<ContentAction> ContentActionSet; 159 160 } // namespace extensions 161 162 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_ 163