blob: c4d1567b21583fea40fa296c473cdca6e04c50ca [file] [log] [blame]
[email protected]3fd3cf72012-05-14 05:51:561// 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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_
6#define CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_
[email protected]3fd3cf72012-05-14 05:51:567
8#include <string>
9
10#include "base/callback_forward.h"
[email protected]af78a802012-07-10 23:47:0211#include "base/observer_list.h"
[email protected]09ae70d42012-11-07 00:42:0912#include "chrome/browser/extensions/tab_helper.h"
[email protected]49d9b142013-07-19 08:50:2713#include "extensions/common/user_script.h"
[email protected]3fd3cf72012-05-14 05:51:5614
[email protected]7f3b91e2012-08-07 08:05:0315class GURL;
16
[email protected]cab8cd982012-07-20 20:57:0317namespace base {
18class ListValue;
19} // namespace base
20
[email protected]3fd3cf72012-05-14 05:51:5621namespace content {
22class WebContents;
23}
24
25namespace extensions {
26
27// Interface for executing extension content scripts (e.g. executeScript) as
28// described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the
29// caller when responded with ExtensionHostMsg_ExecuteCodeFinished.
30class ScriptExecutor {
31 public:
[email protected]09ae70d42012-11-07 00:42:0932 ScriptExecutor(
33 content::WebContents* web_contents,
34 // |script_observers| is assumed to be owned by |this|'s owner, and in
35 // such a way that |this| is destroyed first.
36 ObserverList<TabHelper::ScriptExecutionObserver>* script_observers);
[email protected]af78a802012-07-10 23:47:0237
38 ~ScriptExecutor();
[email protected]3fd3cf72012-05-14 05:51:5639
40 // The type of script being injected.
41 enum ScriptType {
42 JAVASCRIPT,
43 CSS,
44 };
45
46 // The scope of the script injection across the frames.
47 enum FrameScope {
48 TOP_FRAME,
49 ALL_FRAMES,
50 };
51
52 // The type of world to inject into (main world, or its own isolated world).
53 enum WorldType {
54 MAIN_WORLD,
55 ISOLATED_WORLD,
56 };
57
[email protected]88c6f5c2013-08-28 04:08:4158 // The type of process the target is.
59 enum ProcessType {
60 DEFAULT_PROCESS,
61 WEB_VIEW_PROCESS,
62 };
63
64 // The type of result the caller is interested in.
65 enum ResultType {
66 NO_RESULT,
67 JSON_SERIALIZED_RESULT,
68 };
69
[email protected]7f3b91e2012-08-07 08:05:0370 // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url,
71 // result). Success is implied by an empty error.
72 typedef base::Callback<void(const std::string&, int32, const GURL&,
[email protected]cab8cd982012-07-20 20:57:0373 const base::ListValue&)>
[email protected]28a69d32012-05-30 07:58:1874 ExecuteScriptCallback;
[email protected]3fd3cf72012-05-14 05:51:5675
[email protected]3fd3cf72012-05-14 05:51:5676 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in
77 // extension_messages.h (request_id is populated automatically).
78 //
79 // |callback| will always be called even if the IPC'd renderer is destroyed
80 // before a response is received (in this case the callback will be with a
81 // failure and appropriate error message).
[email protected]af78a802012-07-10 23:47:0282 void ExecuteScript(const std::string& extension_id,
83 ScriptType script_type,
84 const std::string& code,
85 FrameScope frame_scope,
86 UserScript::RunLocation run_at,
87 WorldType world_type,
[email protected]88c6f5c2013-08-28 04:08:4188 ProcessType process_type,
[email protected]6f451a42014-04-10 17:12:4789 const GURL& webview_src,
[email protected]a7074d12013-10-03 23:09:3090 const GURL& file_url,
[email protected]0df49432014-03-04 01:02:5091 bool user_gesture,
[email protected]88c6f5c2013-08-28 04:08:4192 ResultType result_type,
[email protected]af78a802012-07-10 23:47:0293 const ExecuteScriptCallback& callback);
94
[email protected]af78a802012-07-10 23:47:0295 private:
96 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params.
97 int next_request_id_;
98
[email protected]af78a802012-07-10 23:47:0299 content::WebContents* web_contents_;
100
[email protected]09ae70d42012-11-07 00:42:09101 ObserverList<TabHelper::ScriptExecutionObserver>* script_observers_;
[email protected]3fd3cf72012-05-14 05:51:56102};
103
104} // namespace extensions
105
106#endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_