blob: c77065cc020c03d32ec387e8307244746ef4f30a [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]3fd3cf72012-05-14 05:51:5612#include "chrome/common/extensions/user_script.h"
13
[email protected]7f3b91e2012-08-07 08:05:0314class GURL;
15
[email protected]cab8cd982012-07-20 20:57:0316namespace base {
17class ListValue;
18} // namespace base
19
[email protected]3fd3cf72012-05-14 05:51:5620namespace content {
21class WebContents;
22}
23
24namespace extensions {
25
26// Interface for executing extension content scripts (e.g. executeScript) as
27// described by the ExtensionMsg_ExecuteCode_Params IPC, and notifying the
28// caller when responded with ExtensionHostMsg_ExecuteCodeFinished.
29class ScriptExecutor {
30 public:
[email protected]af78a802012-07-10 23:47:0231 explicit ScriptExecutor(content::WebContents* web_contents);
32
33 ~ScriptExecutor();
[email protected]3fd3cf72012-05-14 05:51:5634
35 // The type of script being injected.
36 enum ScriptType {
37 JAVASCRIPT,
38 CSS,
39 };
40
41 // The scope of the script injection across the frames.
42 enum FrameScope {
43 TOP_FRAME,
44 ALL_FRAMES,
45 };
46
47 // The type of world to inject into (main world, or its own isolated world).
48 enum WorldType {
49 MAIN_WORLD,
50 ISOLATED_WORLD,
51 };
52
[email protected]7f3b91e2012-08-07 08:05:0353 // Callback from ExecuteScript. The arguments are (error, on_page_id, on_url,
54 // result). Success is implied by an empty error.
55 typedef base::Callback<void(const std::string&, int32, const GURL&,
[email protected]cab8cd982012-07-20 20:57:0356 const base::ListValue&)>
[email protected]28a69d32012-05-30 07:58:1857 ExecuteScriptCallback;
[email protected]3fd3cf72012-05-14 05:51:5658
[email protected]af78a802012-07-10 23:47:0259 class Observer {
60 public:
61 // Automatically observes and unobserves *script_executor on construction
62 // and destruction. *script_executor must outlive *this.
63 explicit Observer(ScriptExecutor* script_executor);
64 virtual ~Observer();
65
66 virtual void OnExecuteScriptFinished(const std::string& extension_id,
[email protected]cab8cd982012-07-20 20:57:0367 const std::string& error,
[email protected]7f3b91e2012-08-07 08:05:0368 int32 on_page_id,
69 const GURL& on_url,
[email protected]cab8cd982012-07-20 20:57:0370 const base::ListValue&) = 0;
[email protected]af78a802012-07-10 23:47:0271 private:
72 ScriptExecutor& script_executor_;
73 };
74
[email protected]3fd3cf72012-05-14 05:51:5675 // Executes a script. The arguments match ExtensionMsg_ExecuteCode_Params in
76 // extension_messages.h (request_id is populated automatically).
77 //
78 // |callback| will always be called even if the IPC'd renderer is destroyed
79 // before a response is received (in this case the callback will be with a
80 // failure and appropriate error message).
[email protected]af78a802012-07-10 23:47:0281 void ExecuteScript(const std::string& extension_id,
82 ScriptType script_type,
83 const std::string& code,
84 FrameScope frame_scope,
85 UserScript::RunLocation run_at,
86 WorldType world_type,
87 const ExecuteScriptCallback& callback);
88
89 void AddObserver(Observer* obs) {
90 observer_list_.AddObserver(obs);
91 }
92
93 void RemoveObserver(Observer* obs) {
94 observer_list_.RemoveObserver(obs);
95 }
96
97 private:
98 // The next value to use for request_id in ExtensionMsg_ExecuteCode_Params.
99 int next_request_id_;
100
101 // The WebContents this is bound to.
102 content::WebContents* web_contents_;
103
104 ObserverList<Observer> observer_list_;
[email protected]3fd3cf72012-05-14 05:51:56105};
106
107} // namespace extensions
108
109#endif // CHROME_BROWSER_EXTENSIONS_SCRIPT_EXECUTOR_H_