app_list: Re-enable people search.
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_action.h
blobdc39431aed870bfe65d677064bf23bc30e542a05
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>
9 #include <vector>
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"
15 namespace base {
16 class Time;
17 class Value;
20 namespace content {
21 class BrowserContext;
22 class WebContents;
25 namespace extensions {
26 class Extension;
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,
38 struct ApplyInfo {
39 content::BrowserContext* browser_context;
40 content::WebContents* tab;
41 int priority;
44 ContentAction();
46 virtual Type GetType() const = 0;
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;
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);
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;
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);
88 protected:
89 friend class base::RefCounted<ContentAction>;
90 virtual ~ContentAction();
93 // Action that injects a content script.
94 class RequestContentScript : public ContentAction {
95 public:
96 struct ScriptData;
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);
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);
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);
119 static bool InitScriptData(const base::DictionaryValue* dict,
120 std::string* error,
121 bool* bad_message,
122 ScriptData* script_data);
124 // Implementation of ContentAction:
125 Type GetType() const override;
127 void Apply(const std::string& extension_id,
128 const base::Time& extension_install_time,
129 ApplyInfo* apply_info) const override;
131 void Reapply(const std::string& extension_id,
132 const base::Time& extension_install_time,
133 ApplyInfo* apply_info) const override;
135 void Revert(const std::string& extension_id,
136 const base::Time& extension_install_time,
137 ApplyInfo* apply_info) const override;
139 private:
140 void InitScript(const Extension* extension, const ScriptData& script_data);
142 void AddScript() {
143 DCHECK(master_);
144 master_->AddScript(script_);
147 ~RequestContentScript() override;
149 void InstructRenderProcessToInject(content::WebContents* contents,
150 const std::string& extension_id) const;
152 UserScript script_;
153 DeclarativeUserScriptMaster* master_;
155 DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
158 typedef DeclarativeActionSet<ContentAction> ContentActionSet;
160 } // namespace extensions
162 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_