Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_action.h
blob625a1caf15fc1e8f93ba6f4a1d5ad17750e021a0
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_
8 #include <string>
10 #include "base/memory/scoped_ptr.h"
11 #include "extensions/browser/declarative_user_script_master.h"
12 #include "extensions/common/user_script.h"
14 namespace base {
15 class Value;
16 class DictionaryValue;
19 namespace content {
20 class BrowserContext;
21 class WebContents;
24 namespace extensions {
26 class Extension;
28 // Base class for all ContentActions of the Declarative Content API.
30 // For example, given the sample code at
31 // https://developer.chrome.com/extensions/declarativeContent#rules, the entity
32 // rule1['actions'][0] is represented by a ContentAction subclass.
33 class ContentAction {
34 public:
35 struct ApplyInfo {
36 const Extension* extension;
37 content::BrowserContext* browser_context;
38 content::WebContents* tab;
39 int priority;
42 virtual ~ContentAction();
44 // Applies or reverts this ContentAction on a particular tab for a particular
45 // extension. Revert exists to keep the actions up to date as the page
46 // changes. Reapply exists to reapply changes to a new page, even if the
47 // previous page also matched relevant conditions.
48 virtual void Apply(const ApplyInfo& apply_info) const = 0;
49 virtual void Reapply(const ApplyInfo& apply_info) const = 0;
50 virtual void Revert(const ApplyInfo& apply_info) const = 0;
52 // Factory method that instantiates a concrete ContentAction implementation
53 // according to |json_action|, the representation of the ContentAction as
54 // received from the extension API. Sets |error| and returns NULL in case of
55 // an error.
56 static scoped_ptr<ContentAction> Create(
57 content::BrowserContext* browser_context,
58 const Extension* extension,
59 const base::Value& json_action,
60 std::string* error);
62 protected:
63 ContentAction();
66 // Action that injects a content script.
67 class RequestContentScript : public ContentAction {
68 public:
69 struct ScriptData;
71 RequestContentScript(content::BrowserContext* browser_context,
72 const Extension* extension,
73 const ScriptData& script_data);
74 RequestContentScript(DeclarativeUserScriptMaster* master,
75 const Extension* extension,
76 const ScriptData& script_data);
78 ~RequestContentScript() override;
80 static scoped_ptr<ContentAction> Create(
81 content::BrowserContext* browser_context,
82 const Extension* extension,
83 const base::DictionaryValue* dict,
84 std::string* error);
86 static scoped_ptr<ContentAction> CreateForTest(
87 DeclarativeUserScriptMaster* master,
88 const Extension* extension,
89 const base::Value& json_action,
90 std::string* error);
92 static bool InitScriptData(const base::DictionaryValue* dict,
93 std::string* error,
94 ScriptData* script_data);
96 // Implementation of ContentAction:
97 void Apply(const ApplyInfo& apply_info) const override;
98 void Reapply(const ApplyInfo& apply_info) const override;
99 void Revert(const ApplyInfo& apply_info) const override;
101 private:
102 void InitScript(const HostID& host_id,
103 const Extension* extension,
104 const ScriptData& script_data);
106 void AddScript() {
107 DCHECK(master_);
108 master_->AddScript(script_);
111 void InstructRenderProcessToInject(content::WebContents* contents,
112 const Extension* extension) const;
114 UserScript script_;
115 DeclarativeUserScriptMaster* master_;
117 DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
120 } // namespace extensions
122 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_