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 "content/public/renderer/v8_value_converter.h" | |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | |
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | |
17 | |
18 using content::V8ValueConverter; | |
19 | |
20 // Contains info relevant to a pending API request. | |
21 struct PendingRequest { | |
22 public : | |
23 PendingRequest(v8::Persistent<v8::Context> context, const std::string& name, | |
24 const std::string& extension_id) | |
25 : context(context), name(name), extension_id(extension_id) { | |
26 } | |
27 | |
28 ~PendingRequest() { | |
29 context.Dispose(); | |
30 } | |
31 | |
32 v8::Persistent<v8::Context> context; | |
33 std::string name; | |
34 std::string extension_id; | |
35 }; | |
36 | |
37 ExtensionRequestSender::ExtensionRequestSender( | |
38 ExtensionDispatcher* extension_dispatcher, | |
39 ChromeV8ContextSet* context_set) | |
40 : extension_dispatcher_(extension_dispatcher), | |
41 context_set_(context_set) { | |
42 } | |
43 | |
44 ExtensionRequestSender::~ExtensionRequestSender() { | |
45 } | |
46 | |
47 void ExtensionRequestSender::InsertRequest(int request_id, | |
48 PendingRequest* pending_request) { | |
49 DCHECK_EQ(0u, pending_requests_.count(request_id)); | |
50 pending_requests_[request_id].reset(pending_request); | |
51 } | |
52 | |
53 linked_ptr<PendingRequest> ExtensionRequestSender::RemoveRequest( | |
54 int request_id) { | |
55 PendingRequestMap::iterator i = pending_requests_.find(request_id); | |
56 if (i == pending_requests_.end()) | |
57 return linked_ptr<PendingRequest>(); | |
58 linked_ptr<PendingRequest> result = i->second; | |
59 pending_requests_.erase(i); | |
60 return result; | |
61 } | |
62 | |
63 void ExtensionRequestSender::StartRequest( | |
64 const std::string& name, | |
65 int request_id, | |
66 bool has_callback, | |
67 bool for_io_thread, | |
68 base::ListValue* value_args) { | |
69 ChromeV8Context* current_context = context_set_->GetCurrent(); | |
70 if (!current_context) | |
71 return; | |
72 | |
73 // Get the current RenderView so that we can send a routed IPC message from | |
74 // the correct source. | |
75 content::RenderView* renderview = current_context->GetRenderView(); | |
76 if (!renderview) | |
77 return; | |
78 | |
79 const std::set<std::string>& function_names = | |
80 extension_dispatcher_->function_names(); | |
81 if (function_names.find(name) == function_names.end()) { | |
82 NOTREACHED() << "Unexpected function " << name << | |
83 ". Did you remember to register it with ExtensionFunctionRegistry?"; | |
84 return; | |
85 } | |
86 | |
87 // TODO(koz): See if we can make this a CHECK. | |
88 if (!extension_dispatcher_->CheckCurrentContextAccessToExtensionAPI(name)) | |
89 return; | |
90 | |
91 GURL source_url; | |
92 WebKit::WebSecurityOrigin source_origin; | |
93 WebKit::WebFrame* webframe = current_context->web_frame(); | |
94 if (webframe) { | |
95 source_url = webframe->document().url(); | |
96 source_origin = webframe->document().securityOrigin(); | |
97 } | |
98 | |
99 v8::Persistent<v8::Context> v8_context = | |
100 v8::Persistent<v8::Context>::New(v8::Context::GetCurrent()); | |
101 DCHECK(!v8_context.IsEmpty()); | |
102 | |
103 std::string extension_id = current_context->GetExtensionID(); | |
104 InsertRequest(request_id, new PendingRequest( | |
105 v8_context, name, extension_id)); | |
106 | |
107 ExtensionHostMsg_Request_Params params; | |
108 params.name = name; | |
109 params.arguments.Swap(value_args); | |
110 params.extension_id = extension_id; | |
111 params.source_url = source_url; | |
112 params.source_origin = source_origin.toString(); | |
113 params.request_id = request_id; | |
114 params.has_callback = has_callback; | |
115 params.user_gesture = | |
116 webframe ? webframe->isProcessingUserGesture() : false; | |
117 if (for_io_thread) { | |
118 renderview->Send(new ExtensionHostMsg_RequestForIOThread( | |
119 renderview->GetRoutingID(), params)); | |
120 } else { | |
121 renderview->Send(new ExtensionHostMsg_Request( | |
122 renderview->GetRoutingID(), params)); | |
123 } | |
124 } | |
125 | |
126 void ExtensionRequestSender::HandleResponse(int request_id, | |
127 bool success, | |
128 const base::ListValue& responseList, | |
129 const std::string& error) { | |
130 linked_ptr<PendingRequest> request = RemoveRequest(request_id); | |
131 | |
132 if (!request.get()) { | |
133 // This should not be able to happen since we only remove requests when | |
134 // they are handled. | |
135 LOG(ERROR) << "Could not find specified request id: " << request_id; | |
136 return; | |
137 } | |
138 | |
139 ChromeV8Context* v8_context = context_set_->GetByV8Context(request->context); | |
140 if (!v8_context) | |
141 return; // The frame went away. | |
142 | |
143 v8::HandleScope handle_scope; | |
144 | |
145 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
146 v8::Handle<v8::Value> argv[] = { | |
147 v8::Integer::New(request_id), | |
148 v8::String::New(request->name.c_str()), | |
149 v8::Boolean::New(success), | |
150 converter->ToV8Value(&responseList, v8_context->v8_context()), | |
151 v8::String::New(error.c_str()) | |
152 }; | |
153 | |
154 v8::Handle<v8::Value> retval; | |
155 CHECK(v8_context->CallChromeHiddenMethod("handleResponse", | |
156 arraysize(argv), | |
157 argv, | |
158 &retval)); | |
159 // In debug, the js will validate the callback parameters and return a | |
160 // string if a validation error has occured. | |
161 #ifndef NDEBUG | |
162 if (!retval.IsEmpty() && !retval->IsUndefined()) { | |
163 std::string error = *v8::String::AsciiValue(retval); | |
164 DCHECK(false) << error; | |
165 } | |
166 #endif | |
167 } | |
OLD | NEW |