Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / declarative_content / content_action.h
bloba4b91876d417f82d30e48b454cad262f159b9599
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.
29 class ContentAction {
30 public:
31 struct ApplyInfo {
32 const Extension* extension;
33 content::BrowserContext* browser_context;
34 content::WebContents* tab;
35 int priority;
38 virtual ~ContentAction();
40 // Applies or reverts this ContentAction on a particular tab for a particular
41 // extension. Revert exists to keep the actions up to date as the page
42 // changes. Reapply exists to reapply changes to a new page, even if the
43 // previous page also matched relevant conditions.
44 virtual void Apply(const ApplyInfo& apply_info) const = 0;
45 virtual void Reapply(const ApplyInfo& apply_info) const = 0;
46 virtual void Revert(const ApplyInfo& apply_info) const = 0;
48 // Factory method that instantiates a concrete ContentAction implementation
49 // according to |json_action|, the representation of the ContentAction as
50 // received from the extension API. Sets |error| and returns NULL in case of
51 // an error.
52 static scoped_ptr<ContentAction> Create(
53 content::BrowserContext* browser_context,
54 const Extension* extension,
55 const base::Value& json_action,
56 std::string* error);
58 protected:
59 ContentAction();
62 // Action that injects a content script.
63 class RequestContentScript : public ContentAction {
64 public:
65 struct ScriptData;
67 RequestContentScript(content::BrowserContext* browser_context,
68 const Extension* extension,
69 const ScriptData& script_data);
70 RequestContentScript(DeclarativeUserScriptMaster* master,
71 const Extension* extension,
72 const ScriptData& script_data);
74 ~RequestContentScript() override;
76 static scoped_ptr<ContentAction> Create(
77 content::BrowserContext* browser_context,
78 const Extension* extension,
79 const base::DictionaryValue* dict,
80 std::string* error);
82 static scoped_ptr<ContentAction> CreateForTest(
83 DeclarativeUserScriptMaster* master,
84 const Extension* extension,
85 const base::Value& json_action,
86 std::string* error);
88 static bool InitScriptData(const base::DictionaryValue* dict,
89 std::string* error,
90 ScriptData* script_data);
92 // Implementation of ContentAction:
93 void Apply(const ApplyInfo& apply_info) const override;
94 void Reapply(const ApplyInfo& apply_info) const override;
95 void Revert(const ApplyInfo& apply_info) const override;
97 private:
98 void InitScript(const HostID& host_id,
99 const Extension* extension,
100 const ScriptData& script_data);
102 void AddScript() {
103 DCHECK(master_);
104 master_->AddScript(script_);
107 void InstructRenderProcessToInject(content::WebContents* contents,
108 const Extension* extension) const;
110 UserScript script_;
111 DeclarativeUserScriptMaster* master_;
113 DISALLOW_COPY_AND_ASSIGN(RequestContentScript);
116 } // namespace extensions
118 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_