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.
32 const Extension
* extension
;
33 content::BrowserContext
* browser_context
;
34 content::WebContents
* tab
;
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
52 static scoped_ptr
<ContentAction
> Create(
53 content::BrowserContext
* browser_context
,
54 const Extension
* extension
,
55 const base::Value
& json_action
,
62 // Action that injects a content script.
63 class RequestContentScript
: public ContentAction
{
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
,
82 static scoped_ptr
<ContentAction
> CreateForTest(
83 DeclarativeUserScriptMaster
* master
,
84 const Extension
* extension
,
85 const base::Value
& json_action
,
88 static bool InitScriptData(const base::DictionaryValue
* dict
,
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
;
98 void InitScript(const HostID
& host_id
,
99 const Extension
* extension
,
100 const ScriptData
& script_data
);
104 master_
->AddScript(script_
);
107 void InstructRenderProcessToInject(content::WebContents
* contents
,
108 const Extension
* extension
) const;
111 DeclarativeUserScriptMaster
* master_
;
113 DISALLOW_COPY_AND_ASSIGN(RequestContentScript
);
116 } // namespace extensions
118 #endif // CHROME_BROWSER_EXTENSIONS_API_DECLARATIVE_CONTENT_CONTENT_ACTION_H_