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

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: rebase 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 = "The tab was closed.";
22
23 // A handler for a single injection request. On creation this will send the
24 // injection request to the renderer, and it will be destroyed after either the
25 // corresponding response comes from the renderer, or the renderer is destroyed.
26 class Handler : public content::WebContentsObserver {
27 public:
28 Handler(content::WebContents* web_contents,
29 const ExtensionMsg_ExecuteCode_Params params,
30 const ScriptExecutor::ExecuteScriptCallback& callback)
31 : content::WebContentsObserver(web_contents),
32 request_id_(params.request_id),
33 callback_(callback) {
34 content::RenderViewHost* rvh = web_contents->GetRenderViewHost();
35 rvh->Send(new ExtensionMsg_ExecuteCode(rvh->GetRoutingID(), params));
36 }
37
38 virtual ~Handler() {}
39
40 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
41 // Unpack by hand to check the request_id, since there may be multiple
42 // requests in flight but only one is for this.
43 if (message.type() != ExtensionHostMsg_ExecuteCodeFinished::ID)
44 return false;
45
46 int message_request_id;
47 PickleIterator iter(message);
48 CHECK(message.ReadInt(&iter, &message_request_id));
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 callback_.Run(false, kRendererDestroyed);
62 delete this;
63 }
64
65 private:
66 void OnExecuteCodeFinished(int request_id,
67 bool success,
68 const std::string& error) {
69 callback_.Run(success, error);
70 delete this;
71 }
72
73 int request_id_;
74 ScriptExecutor::ExecuteScriptCallback callback_;
75 };
76
77 } // namespace
78
79 ScriptExecutorImpl::ScriptExecutorImpl(
80 content::WebContents* web_contents)
81 : next_request_id_(0),
82 web_contents_(web_contents) {}
83
84 ScriptExecutorImpl::~ScriptExecutorImpl() {}
85
86 void ScriptExecutorImpl::ExecuteScript(
87 const std::string& extension_id,
88 ScriptExecutor::ScriptType script_type,
89 const std::string& code,
90 ScriptExecutor::FrameScope frame_scope,
91 UserScript::RunLocation run_at,
92 ScriptExecutor::WorldType world_type,
93 const ExecuteScriptCallback& callback) {
94 ExtensionMsg_ExecuteCode_Params params;
95 params.request_id = next_request_id_++;
96 params.extension_id = extension_id;
97 params.is_javascript = (script_type == JAVASCRIPT);
98 params.code = code;
99 params.all_frames = (frame_scope == ALL_FRAMES);
100 params.run_at = (int) run_at;
101 params.in_main_world = (world_type == MAIN_WORLD);
102
103 // Handler handles IPCs and deletes itself on completion.
104 new Handler(web_contents_, params, callback);
105 }
106
107 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/script_executor_impl.h ('k') | chrome/browser/ui/tab_contents/tab_contents_wrapper.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698