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";
Matt Perry 2012/05/11 22:47:38 maybe "The tab was closed." would be more human re
not at google - send to devlin 2012/05/14 02:27:16 Done.
22
23 class Handler : public content::WebContentsObserver {
koz (OOO until 15th September) 2012/05/11 01:09:16 Could you please add a comment here explaining the
not at google - send to devlin 2012/05/14 02:27:16 Done.
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);
koz (OOO until 15th September) 2012/05/11 01:09:16 Maybe a comment like "Wait for WebContentsDestroy
not at google - send to devlin 2012/05/14 02:27:16 It would be OnMessageReceived too, but yeah, reali
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 CHECK(message.ReadInt(&iter, &message_request_id));
46
47 if (message_request_id != request_id_)
48 return false;
49
50 IPC_BEGIN_MESSAGE_MAP(Handler, message)
51 IPC_MESSAGE_HANDLER(ExtensionHostMsg_ExecuteCodeFinished,
52 OnExecuteCodeFinished)
53 IPC_END_MESSAGE_MAP()
54 return true;
55 }
56
57 virtual void WebContentsDestroyed(content::WebContents* tab) OVERRIDE {
58 Observe(NULL);
59 callback_.Run(false, kRendererDestroyed);
60 delete this;
61 }
62
63 private:
64 void OnExecuteCodeFinished(int request_id,
65 bool success,
66 const std::string& error) {
67 Observe(NULL);
68 callback_.Run(success, error);
69 delete this;
70 }
71
72 int request_id_;
73 ScriptExecutor::ExecuteScriptCallback callback_;
74 };
75
76 } // namespace
77
78 ScriptExecutorImpl::ScriptExecutorImpl(
79 content::WebContents* web_contents)
80 : next_request_id_(0),
81 web_contents_(web_contents) {}
82
83 ScriptExecutorImpl::~ScriptExecutorImpl() {}
84
85 void ScriptExecutorImpl::ExecuteScript(
86 const std::string& extension_id,
87 bool is_javascript,
88 const std::string& code,
89 bool all_frames,
90 UserScript::RunLocation run_at,
91 bool in_main_world,
92 const ExecuteScriptCallback& callback) {
93 ExtensionMsg_ExecuteCode_Params params;
94 params.request_id = next_request_id_++;
95 params.extension_id = extension_id;
96 params.is_javascript = is_javascript;
97 params.code = code;
98 params.all_frames = all_frames;
99 params.run_at = (int) run_at;
100 params.in_main_world = in_main_world;
101
102 // Handler handles IPCs and deletes itself on completion.
103 new Handler(web_contents_, params, callback);
104 }
105
106 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698