OLD | NEW |
---|---|
(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, | |
benwells
2012/03/29 23:06:36
If it is invalid to reuse a request id, should we
koz (OOO until 15th September)
2012/03/29 23:55:46
Done.
| |
45 PendingRequest* pending_request) { | |
46 pending_requests_[request_id].reset(pending_request); | |
47 } | |
48 | |
49 PendingRequest* ExtensionRequestSender::GetPendingRequest(int request_id) { | |
50 PendingRequestMap::iterator i = pending_requests_.find(request_id); | |
51 if (i == pending_requests_.end()) { | |
52 return NULL; | |
53 } | |
54 return i->second.get(); | |
55 } | |
56 | |
57 void ExtensionRequestSender::RemoveRequest(int request_id) { | |
58 PendingRequest* request = GetPendingRequest(request_id); | |
59 if (!request) | |
60 return; | |
61 pending_requests_.erase(request_id); | |
62 } | |
63 | |
64 void ExtensionRequestSender::HandleResponse(int request_id, | |
65 bool success, | |
66 const std::string& response, | |
67 const std::string& error) { | |
68 PendingRequest* request = GetPendingRequest(request_id); | |
69 if (!request) { | |
70 // This should not be able to happen since we only remove requests when they | |
71 // are handled. | |
72 LOG(ERROR) << "Could not find specified request id: " << request_id; | |
73 return; | |
74 } | |
75 | |
76 ChromeV8Context* v8_context = context_set_->GetByV8Context(request->context); | |
77 if (!v8_context) | |
78 return; // The frame went away. | |
79 | |
80 v8::HandleScope handle_scope; | |
81 v8::Handle<v8::Value> argv[5]; | |
82 argv[0] = v8::Integer::New(request_id); | |
83 argv[1] = v8::String::New(request->name.c_str()); | |
84 argv[2] = v8::Boolean::New(success); | |
85 argv[3] = v8::String::New(response.c_str()); | |
86 argv[4] = v8::String::New(error.c_str()); | |
87 | |
88 v8::Handle<v8::Value> retval; | |
89 CHECK(v8_context->CallChromeHiddenMethod("handleResponse", | |
90 arraysize(argv), | |
91 argv, | |
92 &retval)); | |
93 // In debug, the js will validate the callback parameters and return a | |
94 // string if a validation error has occured. | |
95 #ifndef NDEBUG | |
96 if (!retval.IsEmpty() && !retval->IsUndefined()) { | |
97 std::string error = *v8::String::AsciiValue(retval); | |
98 DCHECK(false) << error; | |
99 } | |
100 #endif | |
101 | |
102 RemoveRequest(request_id); | |
103 } | |
104 | |
105 void ExtensionRequestSender::StartRequest( | |
106 const std::string& name, | |
107 int request_id, | |
108 bool has_callback, | |
109 bool for_io_thread, | |
110 base::ListValue* value_args) { | |
111 ChromeV8Context* current_context = context_set_->GetCurrent(); | |
112 if (!current_context) | |
113 return; | |
114 | |
115 // Get the current RenderView so that we can send a routed IPC message from | |
116 // the correct source. | |
117 content::RenderView* renderview = current_context->GetRenderView(); | |
118 if (!renderview) | |
119 return; | |
120 | |
121 const std::set<std::string>& function_names = | |
122 extension_dispatcher_->function_names(); | |
123 if (function_names.find(name) == function_names.end()) { | |
124 NOTREACHED() << "Unexpected function " << name << | |
125 ". Did you remember to register it with ExtensionFunctionRegistry?"; | |
126 return; | |
127 } | |
128 | |
129 if (!extension_dispatcher_->CheckCurrentContextAccessToExtensionAPI(name)) | |
130 return; | |
131 | |
132 GURL source_url; | |
133 WebKit::WebSecurityOrigin source_origin; | |
134 WebKit::WebFrame* webframe = current_context->web_frame(); | |
135 if (webframe) { | |
136 source_url = webframe->document().url(); | |
137 source_origin = webframe->document().securityOrigin(); | |
138 } | |
139 | |
140 v8::Persistent<v8::Context> v8_context = | |
141 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); | |
142 DCHECK(!v8_context.IsEmpty()); | |
143 InsertRequest(request_id, new PendingRequest( | |
144 v8_context, name, current_context->extension_id())); | |
145 | |
146 ExtensionHostMsg_Request_Params params; | |
147 params.name = name; | |
148 params.arguments.Swap(value_args); | |
149 params.extension_id = current_context->extension_id(); | |
150 params.source_url = source_url; | |
151 params.source_origin = source_origin.toString(); | |
152 params.request_id = request_id; | |
153 params.has_callback = has_callback; | |
154 params.user_gesture = | |
155 webframe ? webframe->isProcessingUserGesture() : false; | |
156 if (for_io_thread) { | |
157 renderview->Send(new ExtensionHostMsg_RequestForIOThread( | |
158 renderview->GetRoutingID(), params)); | |
159 } else { | |
160 renderview->Send(new ExtensionHostMsg_Request( | |
161 renderview->GetRoutingID(), params)); | |
162 } | |
163 } | |
OLD | NEW |