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_
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"
25 namespace extensions
{
28 // Base class for all ContentActions of the declarative content API.
29 class ContentAction
: public base::RefCounted
<ContentAction
> {
31 // Type identifiers for concrete ContentActions.
33 ACTION_SHOW_PAGE_ACTION
,
34 ACTION_REQUEST_CONTENT_SCRIPT
,
39 content::BrowserContext
* browser_context
;
40 content::WebContents
* tab
;
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
,
75 // Shared procedure for resetting error state within factories.
76 static void ResetErrorData(std::string
* error
, bool* bad_message
) {
81 // Shared procedure for validating JSON data.
82 static bool Validate(const base::Value
& json_action
,
85 const base::DictionaryValue
** action_dict
,
86 std::string
* instance_type
);
89 friend class base::RefCounted
<ContentAction
>;
90 virtual ~ContentAction();
93 // Action that injects a content script.
94 class RequestContentScript
: public ContentAction
{
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
,
112 static scoped_refptr
<ContentAction
> CreateForTest(
113 DeclarativeUserScriptMaster
* master
,
114 const Extension
* extension
,
115 const base::Value
& json_action
,
119 static bool InitScriptData(const base::DictionaryValue
* dict
,
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
;
140 void InitScript(const Extension
* extension
, const ScriptData
& script_data
);
144 master_
->AddScript(script_
);
147 ~RequestContentScript() override
;
149 void InstructRenderProcessToInject(content::WebContents
* contents
,
150 const std::string
& extension_id
) const;
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_