Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: chrome/browser/extensions/script_executor_impl.cc

Issue 10383104: Extract executeScript-like functionality into a single ExtensionScriptExecutor class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/extensions/script_executor_impl.h"
6
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/pickle.h"
10 #include "chrome/common/extensions/extension_messages.h"
11 #include "content/public/browser/render_view_host.h"
12 #include "content/public/browser/web_contents.h"
13 #include "content/public/browser/web_contents_observer.h"
14 #include "ipc/ipc_message.h"
15 #include "ipc/ipc_message_macros.h"
16
17 namespace extensions {
18
19 namespace {
20
21 const char* kRendererDestroyed = "Renderer destroyed";
22
23 class Handler : public content::WebContentsObserver {
24 public:
25 Handler(content::WebContents* web_contents,
26 const ExtensionMsg_ExecuteCode_Params params,
27 const ScriptExecutor::ExecuteScriptCallback& callback)
28 : request_id_(params.request_id),
29 callback_(callback) {
30 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
31 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params));
32 Observe(web_contents);
33 }
34
35 virtual ~Handler() {}
36
37 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
38 // Unpack by hand to check the request_id, since there may be multiple
39 // requests in flight but only one is for this.
40 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID)
41 return false;
42
43 int message_request_id;
44 PickleIterator iter(message);
45 if (!message.ReadInt(&iter, &message_request_id)) {
46 NOTREACHED() << "malformed extension message";
47 return true;
48 }
49
50 if (message_request_id != request_id_)
51 return false;
52
53 IPC_BEGIN_MESSAGE_MAP(Handler, message)
54 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished,
55 OnExecuteCodeFinished)
56 IPC_END_MESSAGE_MAP()
57 return true;
58 }
59
60 virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE {
61 Observe(NULL);
62 callback_.Run(false, kRendererDestroyed);
63 delete this;
64 }
65
66 private:
67 void OnExecuteCodeFinished(int request_id,
68 bool success,
69 const std::string& error) {
70 Observe(NULL);
71 callback_.Run(success, error);
72 delete this;
73 }
74
75 int request_id_;
76 ScriptExecutor::ExecuteScriptCallback callback_;
77 };
78
79 } // namespace
80
81 ScriptExecutorImpl::ScriptExecutorImpl(
82 content::WebContents* web_contents)
83 : next_request_id_(0),
84 web_contents_(web_contents) {}
85
86 ScriptExecutorImpl::~ScriptExecutorImpl() {}
87
88 void ScriptExecutorImpl::ExecuteScript(
89 const std::string& extension_id,
90 bool is_javascript,
91 const std::string& code,
92 bool all_frames,
93 UserScript::RunLocation run_at,
94 bool in_main_world,
95 const ExecuteScriptCallback& callback) {
96 ExtensionMsg_ExecuteCode_Params params;
97 params.request_id = next_request_id_++;
98 params.extension_id = extension_id;
99 params.is_javascript = is_javascript;
100 params.code = code;
101 params.all_frames = all_frames;
102 params.run_at = (int) run_at;
103 params.in_main_world = in_main_world;
104
105 // Handler handles IPCs and deletes itself on completion.
106 new Handler(web_contents_, params, callback);
107 }
108
109 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698