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

Side by Side Diff: chrome/renderer/extensions/extension_request_sender.cc

Issue 9903010: Extract ExtensionRequestSender from SchemaGeneratedBindings. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: respond to comments Created 8 years, 8 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/renderer/extensions/extension_request_sender.h"
6
7 #include "base/values.h"
8 #include "chrome/common/extensions/extension_messages.h"
9 #include "chrome/renderer/extensions/chrome_v8_context.h"
10 #include "chrome/renderer/extensions/chrome_v8_context_set.h"
11 #include "chrome/renderer/extensions/extension_dispatcher.h"
12 #include "content/public/renderer/render_view.h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h"
16
17 // Contains info relevant to a pending API request.
18 struct PendingRequest {
19 public :
20 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name,
21 const std::string& extension_id)
22 : context(context), name(name), extension_id(extension_id) {
23 }
24
25 ~PendingRequest() {
26 context.Dispose();
27 }
28
29 v8::Persistent<v8::Context> context;
30 std::string name;
31 std::string extension_id;
32 };
33
34 ExtensionRequestSender::ExtensionRequestSender(
35 ExtensionDispatcher* extension_dispatcher,
36 ChromeV8ContextSet* context_set)
37 : extension_dispatcher_(extension_dispatcher),
38 context_set_(context_set) {
39 }
40
41 ExtensionRequestSender::~ExtensionRequestSender() {
42 }
43
44 void ExtensionRequestSender::InsertRequest(int request_id,
45 PendingRequest* pending_request) {
46 DCHECK(pending_requests.count(request_id) == 0);
not at google - send to devlin 2012/03/30 03:14:31 DCHECK_EQ
koz (OOO until 15th September) 2012/04/03 00:15:17 Done.
47 pending_requests_[request_id].reset(pending_request);
not at google - send to devlin 2012/03/30 03:14:31 so much overkill having this in a separate method
koz (OOO until 15th September) 2012/04/03 00:15:17 Eh, it makes the code a bit more readable, I think
48 }
49
50 PendingRequest* ExtensionRequestSender::GetPendingRequest(int request_id) {
51 PendingRequestMap::iterator i = pending_requests_.find(request_id);
52 if (i == pending_requests_.end()) {
53 return NULL;
54 }
55 return i->second.get();
56 }
57
58 void ExtensionRequestSender::RemoveRequest(int request_id) {
59 PendingRequest* request = GetPendingRequest(request_id);
60 if (!request)
61 return;
62 pending_requests_.erase(request_id);
63 }
64
65 void ExtensionRequestSender::StartRequest(
66 const std::string& name,
67 int request_id,
68 bool has_callback,
69 bool for_io_thread,
70 base::ListValue* value_args) {
71 ChromeV8Context* current_context = context_set_->GetCurrent();
72 if (!current_context)
73 return;
74
75 // Get the current RenderView so that we can send a routed IPC message from
76 // the correct source.
77 content::RenderView* renderview = current_context->GetRenderView();
78 if (!renderview)
79 return;
80
81 const std::set<std::string>& function_names =
82 extension_dispatcher_->function_names();
83 if (function_names.find(name) == function_names.end()) {
84 NOTREACHED() << "Unexpected function " << name <<
85 ". Did you remember to register it with ExtensionFunctionRegistry?";
86 return;
87 }
88
89 if (!extension_dispatcher_->CheckCurrentContextAccessToExtensionAPI(name))
90 return;
not at google - send to devlin 2012/03/30 03:14:31 this one is a bit funny, the JS side of things sho
koz (OOO until 15th September) 2012/04/03 00:15:17 Done.
91
92 GURL source_url;
93 WebKit::WebSecurityOrigin source_origin;
94 WebKit::WebFrame* webframe = current_context->web_frame();
95 if (webframe) {
96 source_url = webframe->document().url();
97 source_origin = webframe->document().securityOrigin();
98 }
99
100 v8::Persistent<v8::Context> v8_context =
101 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent());
102 DCHECK(!v8_context.IsEmpty());
103 InsertRequest(request_id, new PendingRequest(
104 v8_context, name, current_context->extension_id()));
105
106 ExtensionHostMsg_Request_Params params;
107 params.name = name;
108 params.arguments.Swap(value_args);
109 params.extension_id = current_context->extension_id();
110 params.source_url = source_url;
111 params.source_origin = source_origin.toString();
112 params.request_id = request_id;
113 params.has_callback = has_callback;
114 params.user_gesture =
115 webframe ? webframe->isProcessingUserGesture() : false;
116 if (for_io_thread) {
117 renderview->Send(new ExtensionHostMsg_RequestForIOThread(
118 renderview->GetRoutingID(), params));
119 } else {
120 renderview->Send(new ExtensionHostMsg_Request(
121 renderview->GetRoutingID(), params));
122 }
123 }
124
125 void ExtensionRequestSender::HandleResponse(int request_id,
126 bool success,
127 const std::string& response,
128 const std::string& error) {
129 PendingRequest* request = GetPendingRequest(request_id);
not at google - send to devlin 2012/03/30 03:14:31 Could this be RemovePendingRequest and not have Ge
koz (OOO until 15th September) 2012/04/03 00:15:17 Done.
130 if (!request) {
131 // This should not be able to happen since we only remove requests when they
132 // are handled.
133 LOG(ERROR) << "Could not find specified request id: " << request_id;
134 return;
135 }
136
137 ChromeV8Context* v8_context = context_set_->GetByV8Context(request->context);
138 if (!v8_context)
139 return; // The frame went away.
140
141 v8::HandleScope handle_scope;
142 v8::Handle<v8::Value> argv[5];
143 argv[0] = v8::Integer::New(request_id);
144 argv[1] = v8::String::New(request->name.c_str());
145 argv[2] = v8::Boolean::New(success);
146 argv[3] = v8::String::New(response.c_str());
147 argv[4] = v8::String::New(error.c_str());
148
149 v8::Handle<v8::Value> retval;
150 CHECK(v8_context->CallChromeHiddenMethod("handleResponse",
151 arraysize(argv),
152 argv,
153 &retval));
154 // In debug, the js will validate the callback parameters and return a
155 // string if a validation error has occured.
156 #ifndef NDEBUG
157 if (!retval.IsEmpty() && !retval->IsUndefined()) {
158 std::string error = *v8::String::AsciiValue(retval);
159 DCHECK(false) << error;
160 }
161 #endif
162
163 RemoveRequest(request_id);
164 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698