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_
10 #include "base/memory/scoped_ptr.h"
11 #include "extensions/browser/declarative_user_script_master.h"
12 #include "extensions/common/user_script.h"
16 class DictionaryValue
;
24 namespace extensions
{
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.
36 const Extension
* extension
;
37 content::BrowserContext
* browser_context
;
38 content::WebContents
* tab
;
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
56 static scoped_ptr
<ContentAction
> Create(
57 content::BrowserContext
* browser_context
,
58 const Extension
* extension
,
59 const base::Value
& json_action
,
66 // Action that injects a content script.
67 class RequestContentScript
: public ContentAction
{
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
,
86 static scoped_ptr
<ContentAction
> CreateForTest(
87 DeclarativeUserScriptMaster
* master
,
88 const Extension
* extension
,
89 const base::Value
& json_action
,
92 static bool InitScriptData(const base::DictionaryValue
* dict
,
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
;
102 void InitScript(const HostID
& host_id
,
103 const Extension
* extension
,
104 const ScriptData
& script_data
);
108 master_
->AddScript(script_
);
111 void InstructRenderProcessToInject(content::WebContents
* contents
,
112 const Extension
* extension
) const;
115 DeclarativeUserScriptMaster
* master_
;
117 DISALLOW_COPY_AND_ASSIGN(RequestContentScript
);
120 } // namespace extensions
122 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_