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

Side by Side Diff: chrome/browser/extensions/api/webview/execute_script_function.cc

Issue 11968054: <webview>: Implement ExecuteScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Renamed misnamed variable Created 7 years, 11 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) 2013 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/api/webview/execute_script_function.h"
6
7 #include "chrome/browser/extensions/extension_host.h"
8 #include "chrome/browser/extensions/extension_process_manager.h"
9 #include "chrome/browser/extensions/extension_system.h"
10 #include "chrome/common/extensions/api/tabs.h"
11 #include "chrome/common/extensions/extension_messages.h"
12 #include "chrome/common/extensions/value_builder.h"
13 #include "content/public/browser/render_view_host.h"
14
15 using extensions::api::tabs::InjectDetails;
16
17 namespace extensions {
18
19 ExecuteScriptFunction::ExecuteScriptFunction() {
20 }
21
22 ExecuteScriptFunction::~ExecuteScriptFunction() {
23 }
24
25 bool ExecuteScriptFunction::RunImpl() {
26 if (args_->empty())
27 return false;
28
29 int process_id = 0;
30 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &process_id));
31
32 int route_id = 0;
33 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &route_id));
34
35 base::DictionaryValue* details_value = NULL;
36 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(2, &details_value));
Matt Perry 2013/01/23 02:50:49 You can clean up some of this parsing logic using
Fady Samuel 2013/01/23 20:12:03 Done.
37
38 scoped_ptr<InjectDetails> details(new InjectDetails());
39 if (!InjectDetails::Populate(*details_value, details.get()))
40 return false;
41
42 content::RenderViewHost* guest_rvh =
43 content::RenderViewHost::FromID(process_id, route_id);
44 // If we haven't loaded a guest yet, then this will be NULL.
45 if (!guest_rvh)
46 return false;
47 content::WebContents* guest_web_contents =
48 content::WebContents::FromRenderViewHost(guest_rvh);
49 CHECK(guest_web_contents);
50
51 ObserverList<extensions::TabHelper::ScriptExecutionObserver>
52 script_observers_;
Matt Perry 2013/01/23 02:50:49 no trailing _
Fady Samuel 2013/01/23 20:12:03 Done.
53 scoped_ptr<extensions::ScriptExecutor> script_executor_(
54 new extensions::ScriptExecutor(guest_web_contents, &script_observers_));
55
56 extensions::ScriptExecutor::FrameScope frame_scope =
57 details->all_frames.get() && *details->all_frames ?
58 extensions::ScriptExecutor::ALL_FRAMES :
59 extensions::ScriptExecutor::TOP_FRAME;
60
61 extensions::UserScript::RunLocation run_at =
62 extensions::UserScript::UNDEFINED;
63 switch (details->run_at) {
64 case InjectDetails::RUN_AT_NONE:
65 case InjectDetails::RUN_AT_DOCUMENT_IDLE:
66 run_at = extensions::UserScript::DOCUMENT_IDLE;
67 break;
68 case InjectDetails::RUN_AT_DOCUMENT_START:
69 run_at = extensions::UserScript::DOCUMENT_START;
70 break;
71 case InjectDetails::RUN_AT_DOCUMENT_END:
72 run_at = extensions::UserScript::DOCUMENT_END;
73 break;
74 }
75 CHECK_NE(extensions::UserScript::UNDEFINED, run_at);
76
77 script_executor_->ExecuteScript(
78 GetExtension()->id(),
79 extensions::ScriptExecutor::JAVASCRIPT,
80 *details->code.get(),
Matt Perry 2013/01/23 02:50:49 There's a version of executeScript that uses a fil
Fady Samuel 2013/01/23 20:12:03 I'll have to chat with creis@ and others and perha
81 frame_scope,
82 run_at,
83 extensions::ScriptExecutor::ISOLATED_WORLD,
84 base::Bind(
85 &ExecuteScriptFunction::OnExecuteCodeFinished,
86 this));
87
88 // Balanced in OnExecuteCodeFinished.
89 AddRef();
90 return true;
91 }
92
93 void ExecuteScriptFunction::OnExecuteCodeFinished(
94 const std::string& error,
95 int32 on_page_id,
96 const GURL& on_url,
97 const ListValue& result) {
98 if (error.empty()) {
99 SetResult(result.DeepCopy());
100 } else {
101 SetError(error);
102 }
103 SendResponse(error.empty());
104 Release(); // Added in RunImpl().
105 }
106
107 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698