Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / declarative_content / content_action.h
index bb18d02..a8137cb 100644 (file)
@@ -9,9 +9,8 @@
 #include <vector>
 
 #include "base/memory/ref_counted.h"
-#include "chrome/browser/extensions/api/declarative/declarative_rule.h"
-
-class Profile;
+#include "chrome/browser/extensions/declarative_user_script_master.h"
+#include "extensions/browser/api/declarative/declarative_rule.h"
 
 namespace base {
 class Time;
@@ -19,6 +18,7 @@ class Value;
 }
 
 namespace content {
+class BrowserContext;
 class WebContents;
 }
 
@@ -32,11 +32,13 @@ class ContentAction : public base::RefCounted<ContentAction> {
   enum Type {
     ACTION_SHOW_PAGE_ACTION,
     ACTION_REQUEST_CONTENT_SCRIPT,
+    ACTION_SET_ICON,
   };
 
   struct ApplyInfo {
-    Profile* profile;
+    content::BrowserContext* browser_context;
     content::WebContents* tab;
+    int priority;
   };
 
   ContentAction();
@@ -45,10 +47,14 @@ class ContentAction : public base::RefCounted<ContentAction> {
 
   // Applies or reverts this ContentAction on a particular tab for a particular
   // extension.  Revert exists to keep the actions up to date as the page
-  // changes.
+  // changes.  Reapply exists to reapply changes to a new page, even if the
+  // previous page also matched relevant conditions.
   virtual void Apply(const std::string& extension_id,
                      const base::Time& extension_install_time,
                      ApplyInfo* apply_info) const = 0;
+  virtual void Reapply(const std::string& extension_id,
+                       const base::Time& extension_install_time,
+                       ApplyInfo* apply_info) const = 0;
   virtual void Revert(const std::string& extension_id,
                       const base::Time& extension_install_time,
                       ApplyInfo* apply_info) const = 0;
@@ -59,16 +65,96 @@ class ContentAction : public base::RefCounted<ContentAction> {
   // Sets |error| and returns NULL in case of a semantic error that cannot
   // be caught by schema validation. Sets |bad_message| and returns NULL
   // in case the input is syntactically unexpected.
-  static scoped_refptr<ContentAction> Create(const Extension* extension,
-                                             const base::Value& json_action,
-                                             std::string* error,
-                                             bool* bad_message);
+  static scoped_refptr<ContentAction> Create(
+      content::BrowserContext* browser_context,
+      const Extension* extension,
+      const base::Value& json_action,
+      std::string* error,
+      bool* bad_message);
+
+  // Shared procedure for resetting error state within factories.
+  static void ResetErrorData(std::string* error, bool* bad_message) {
+    *error = "";
+    *bad_message = false;
+  }
+
+  // Shared procedure for validating JSON data.
+  static bool Validate(const base::Value& json_action,
+                       std::string* error,
+                       bool* bad_message,
+                       const base::DictionaryValue** action_dict,
+                       std::string* instance_type);
 
  protected:
   friend class base::RefCounted<ContentAction>;
   virtual ~ContentAction();
 };
 
+// Action that injects a content script.
+class RequestContentScript : public ContentAction {
+ public:
+  struct ScriptData;
+
+  RequestContentScript(content::BrowserContext* browser_context,
+                       const Extension* extension,
+                       const ScriptData& script_data);
+  RequestContentScript(DeclarativeUserScriptMaster* master,
+                       const Extension* extension,
+                       const ScriptData& script_data);
+
+  static scoped_refptr<ContentAction> Create(
+      content::BrowserContext* browser_context,
+      const Extension* extension,
+      const base::DictionaryValue* dict,
+      std::string* error,
+      bool* bad_message);
+
+  static scoped_refptr<ContentAction> CreateForTest(
+      DeclarativeUserScriptMaster* master,
+      const Extension* extension,
+      const base::Value& json_action,
+      std::string* error,
+      bool* bad_message);
+
+  static bool InitScriptData(const base::DictionaryValue* dict,
+                             std::string* error,
+                             bool* bad_message,
+                             ScriptData* script_data);
+
+  // Implementation of ContentAction:
+  virtual Type GetType() const OVERRIDE;
+
+  virtual void Apply(const std::string& extension_id,
+                     const base::Time& extension_install_time,
+                     ApplyInfo* apply_info) const OVERRIDE;
+
+  virtual void Reapply(const std::string& extension_id,
+                       const base::Time& extension_install_time,
+                       ApplyInfo* apply_info) const OVERRIDE;
+
+  virtual void Revert(const std::string& extension_id,
+                      const base::Time& extension_install_time,
+                      ApplyInfo* apply_info) const OVERRIDE;
+
+ private:
+  void InitScript(const Extension* extension, const ScriptData& script_data);
+
+  void AddScript() {
+    DCHECK(master_);
+    master_->AddScript(script_);
+  }
+
+  virtual ~RequestContentScript();
+
+  void InstructRenderProcessToInject(content::WebContents* contents,
+                                     const std::string& extension_id) const;
+
+  UserScript script_;
+  DeclarativeUserScriptMaster* master_;
+
+  DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
+};
+
 typedef DeclarativeActionSet<ContentAction> ContentActionSet;
 
 }  // namespace extensions