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_SCRIPT_EXECUTOR_H_
6 #define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_
10 #include "base/callback_forward.h"
11 #include "base/observer_list.h"
12 #include "chrome/browser/extensions/tab_helper.h"
13 #include "extensions/common/user_script.h"
16 struct ExtensionMsg_ExecuteCode_Params
;
26 namespace extensions
{
28 // Interface for executing extension content scripts (e.g. executeScript) as
29 // described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the
30 // caller when responded with ExtensionHostMsg_ExecuteCodeFinished.
31 class ScriptExecutor
{
34 content::WebContents
* web_contents
,
35 // |script_observers| is assumed to be owned by |this|'s owner, and in
36 // such a way that |this| is destroyed first.
37 ObserverList
<TabHelper::ScriptExecutionObserver
>* script_observers
);
41 // The type of script being injected.
47 // The scope of the script injection across the frames.
53 // Whether to insert the script in about: frames when its origin matches
54 // the extension's host permissions.
55 enum MatchAboutBlank
{
56 DONT_MATCH_ABOUT_BLANK
,
60 // The type of world to inject into (main world, or its own isolated world).
66 // The type of process the target is.
72 // The type of result the caller is interested in.
75 JSON_SERIALIZED_RESULT
,
78 // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url,
79 // result). Success is implied by an empty error.
80 typedef base::Callback
<void(const std::string
&, int32
, const GURL
&,
81 const base::ListValue
&)>
82 ExecuteScriptCallback
;
84 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in
85 // extension_messages.h (request_id is populated automatically).
87 // |callback| will always be called even if the IPC'd renderer is destroyed
88 // before a response is received (in this case the callback will be with a
89 // failure and appropriate error message).
90 void ExecuteScript(const std::string
& extension_id
,
91 ScriptType script_type
,
92 const std::string
& code
,
93 FrameScope frame_scope
,
94 MatchAboutBlank match_about_blank
,
95 UserScript::RunLocation run_at
,
97 ProcessType process_type
,
98 const GURL
& webview_src
,
101 ResultType result_type
,
102 const ExecuteScriptCallback
& callback
);
105 // Called upon a request being given to execute the script.
106 void ExecuteScriptHelper(scoped_ptr
<ExtensionMsg_ExecuteCode_Params
> params
,
107 const ExecuteScriptCallback
& callback
);
109 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params.
110 int next_request_id_
;
112 content::WebContents
* web_contents_
;
114 ObserverList
<TabHelper::ScriptExecutionObserver
>* script_observers_
;
117 } // namespace extensions
119 #endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_