| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/renderer/extensions/request_sender.h" | 5 #include "chrome/renderer/extensions/request_sender.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "chrome/common/extensions/extension_messages.h" | 8 #include "chrome/common/extensions/extension_messages.h" |
| 9 #include "chrome/renderer/extensions/chrome_v8_context.h" | 9 #include "chrome/renderer/extensions/chrome_v8_context.h" |
| 10 #include "chrome/renderer/extensions/dispatcher.h" | 10 #include "chrome/renderer/extensions/dispatcher.h" |
| 11 #include "chrome/renderer/extensions/scoped_persistent.h" | |
| 12 #include "content/public/renderer/render_view.h" | 11 #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" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserGestureIndicat
or.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebUserGestureIndicat
or.h" |
| 18 | 16 |
| 19 using content::V8ValueConverter; | |
| 20 | |
| 21 namespace extensions { | 17 namespace extensions { |
| 22 | 18 |
| 23 // Contains info relevant to a pending API request. | 19 // Contains info relevant to a pending API request. |
| 24 struct PendingRequest { | 20 struct PendingRequest { |
| 25 public : | 21 public : |
| 26 PendingRequest(const std::string& name, ChromeV8Context* context) | 22 PendingRequest(const std::string& name, RequestSender::Source* source) |
| 27 : name(name), context(context) { | 23 : name(name), source(source) { |
| 28 } | 24 } |
| 29 | 25 |
| 30 std::string name; | 26 std::string name; |
| 31 ChromeV8Context* context; | 27 RequestSender::Source* source; |
| 32 }; | 28 }; |
| 33 | 29 |
| 34 RequestSender::RequestSender(Dispatcher* dispatcher) : dispatcher_(dispatcher) { | 30 RequestSender::RequestSender(Dispatcher* dispatcher) : dispatcher_(dispatcher) { |
| 35 } | 31 } |
| 36 | 32 |
| 37 RequestSender::~RequestSender() { | 33 RequestSender::~RequestSender() { |
| 38 } | 34 } |
| 39 | 35 |
| 40 void RequestSender::InsertRequest(int request_id, | 36 void RequestSender::InsertRequest(int request_id, |
| 41 PendingRequest* pending_request) { | 37 PendingRequest* pending_request) { |
| 42 DCHECK_EQ(0u, pending_requests_.count(request_id)); | 38 DCHECK_EQ(0u, pending_requests_.count(request_id)); |
| 43 pending_requests_[request_id].reset(pending_request); | 39 pending_requests_[request_id].reset(pending_request); |
| 44 } | 40 } |
| 45 | 41 |
| 46 linked_ptr<PendingRequest> RequestSender::RemoveRequest(int request_id) { | 42 linked_ptr<PendingRequest> RequestSender::RemoveRequest(int request_id) { |
| 47 PendingRequestMap::iterator i = pending_requests_.find(request_id); | 43 PendingRequestMap::iterator i = pending_requests_.find(request_id); |
| 48 if (i == pending_requests_.end()) | 44 if (i == pending_requests_.end()) |
| 49 return linked_ptr<PendingRequest>(); | 45 return linked_ptr<PendingRequest>(); |
| 50 linked_ptr<PendingRequest> result = i->second; | 46 linked_ptr<PendingRequest> result = i->second; |
| 51 pending_requests_.erase(i); | 47 pending_requests_.erase(i); |
| 52 return result; | 48 return result; |
| 53 } | 49 } |
| 54 | 50 |
| 55 void RequestSender::StartRequest(ChromeV8Context* context, | 51 int RequestSender::GetNextRequestId() const { |
| 52 static int next_request_id = 0; |
| 53 return next_request_id++; |
| 54 } |
| 55 |
| 56 void RequestSender::StartRequest(Source* source, |
| 56 const std::string& name, | 57 const std::string& name, |
| 57 int request_id, | 58 int request_id, |
| 58 bool has_callback, | 59 bool has_callback, |
| 59 bool for_io_thread, | 60 bool for_io_thread, |
| 60 base::ListValue* value_args) { | 61 base::ListValue* value_args) { |
| 62 ChromeV8Context* context = source->GetContext(); |
| 63 if (!context) |
| 64 return; |
| 65 |
| 61 // Get the current RenderView so that we can send a routed IPC message from | 66 // Get the current RenderView so that we can send a routed IPC message from |
| 62 // the correct source. | 67 // the correct source. |
| 63 content::RenderView* renderview = context->GetRenderView(); | 68 content::RenderView* renderview = context->GetRenderView(); |
| 64 if (!renderview) | 69 if (!renderview) |
| 65 return; | 70 return; |
| 66 | 71 |
| 67 const std::set<std::string>& function_names = dispatcher_->function_names(); | 72 const std::set<std::string>& function_names = dispatcher_->function_names(); |
| 68 if (function_names.find(name) == function_names.end()) { | 73 if (function_names.find(name) == function_names.end()) { |
| 69 NOTREACHED() << "Unexpected function " << name << | 74 NOTREACHED() << "Unexpected function " << name << |
| 70 ". Did you remember to register it with ExtensionFunctionRegistry?"; | 75 ". Did you remember to register it with ExtensionFunctionRegistry?"; |
| 71 return; | 76 return; |
| 72 } | 77 } |
| 73 | 78 |
| 74 // TODO(koz): See if we can make this a CHECK. | 79 // TODO(koz): See if we can make this a CHECK. |
| 75 if (!dispatcher_->CheckContextAccessToExtensionAPI(name, context)) | 80 if (!dispatcher_->CheckContextAccessToExtensionAPI(name, context)) |
| 76 return; | 81 return; |
| 77 | 82 |
| 78 GURL source_url; | 83 GURL source_url; |
| 79 WebKit::WebSecurityOrigin source_origin; | 84 WebKit::WebSecurityOrigin source_origin; |
| 80 WebKit::WebFrame* webframe = context->web_frame(); | 85 WebKit::WebFrame* webframe = context->web_frame(); |
| 81 if (webframe) { | 86 if (webframe) { |
| 82 source_url = webframe->document().url(); | 87 source_url = webframe->document().url(); |
| 83 source_origin = webframe->document().securityOrigin(); | 88 source_origin = webframe->document().securityOrigin(); |
| 84 } | 89 } |
| 85 | 90 |
| 86 InsertRequest(request_id, new PendingRequest(name, context)); | 91 InsertRequest(request_id, new PendingRequest(name, source)); |
| 87 | 92 |
| 88 ExtensionHostMsg_Request_Params params; | 93 ExtensionHostMsg_Request_Params params; |
| 89 params.name = name; | 94 params.name = name; |
| 90 params.arguments.Swap(value_args); | 95 params.arguments.Swap(value_args); |
| 91 params.extension_id = context->GetExtensionID(); | 96 params.extension_id = context->GetExtensionID(); |
| 92 params.source_url = source_url; | 97 params.source_url = source_url; |
| 93 params.source_origin = source_origin.toString(); | 98 params.source_origin = source_origin.toString(); |
| 94 params.request_id = request_id; | 99 params.request_id = request_id; |
| 95 params.has_callback = has_callback; | 100 params.has_callback = has_callback; |
| 96 params.user_gesture = | 101 params.user_gesture = |
| 97 WebKit::WebUserGestureIndicator::isProcessingUserGesture(); | 102 WebKit::WebUserGestureIndicator::isProcessingUserGesture(); |
| 98 if (for_io_thread) { | 103 if (for_io_thread) { |
| 99 renderview->Send(new ExtensionHostMsg_RequestForIOThread( | 104 renderview->Send(new ExtensionHostMsg_RequestForIOThread( |
| 100 renderview->GetRoutingID(), params)); | 105 renderview->GetRoutingID(), params)); |
| 101 } else { | 106 } else { |
| 102 renderview->Send(new ExtensionHostMsg_Request( | 107 renderview->Send(new ExtensionHostMsg_Request( |
| 103 renderview->GetRoutingID(), params)); | 108 renderview->GetRoutingID(), params)); |
| 104 } | 109 } |
| 105 } | 110 } |
| 106 | 111 |
| 107 void RequestSender::HandleResponse(int request_id, | 112 void RequestSender::HandleResponse(int request_id, |
| 108 bool success, | 113 bool success, |
| 109 const base::ListValue& responseList, | 114 const base::ListValue& response, |
| 110 const std::string& error) { | 115 const std::string& error) { |
| 111 linked_ptr<PendingRequest> request = RemoveRequest(request_id); | 116 linked_ptr<PendingRequest> request = RemoveRequest(request_id); |
| 112 | 117 |
| 113 if (!request.get()) { | 118 if (!request.get()) { |
| 114 // This can happen if a context is destroyed while a request is in flight. | 119 // This can happen if a context is destroyed while a request is in flight. |
| 115 return; | 120 return; |
| 116 } | 121 } |
| 117 | 122 |
| 118 v8::HandleScope handle_scope; | 123 request->source->OnResponseReceived(request->name, request_id, success, |
| 119 | 124 response, error); |
| 120 scoped_ptr<V8ValueConverter> converter(V8ValueConverter::create()); | |
| 121 v8::Handle<v8::Value> argv[] = { | |
| 122 v8::Integer::New(request_id), | |
| 123 v8::String::New(request->name.c_str()), | |
| 124 v8::Boolean::New(success), | |
| 125 converter->ToV8Value(&responseList, request->context->v8_context()), | |
| 126 v8::String::New(error.c_str()) | |
| 127 }; | |
| 128 | |
| 129 v8::Handle<v8::Value> retval; | |
| 130 CHECK(request->context->CallChromeHiddenMethod("handleResponse", | |
| 131 arraysize(argv), | |
| 132 argv, | |
| 133 &retval)); | |
| 134 // In debug, the js will validate the callback parameters and return a | |
| 135 // string if a validation error has occured. | |
| 136 if (DCHECK_IS_ON()) { | |
| 137 if (!retval.IsEmpty() && !retval->IsUndefined()) { | |
| 138 std::string error = *v8::String::AsciiValue(retval); | |
| 139 DCHECK(false) << error; | |
| 140 } | |
| 141 } | |
| 142 } | 125 } |
| 143 | 126 |
| 144 void RequestSender::InvalidateContext(ChromeV8Context* context) { | 127 void RequestSender::InvalidateSource(Source* source) { |
| 145 for (PendingRequestMap::iterator it = pending_requests_.begin(); | 128 for (PendingRequestMap::iterator it = pending_requests_.begin(); |
| 146 it != pending_requests_.end();) { | 129 it != pending_requests_.end();) { |
| 147 if (it->second->context == context) | 130 if (it->second->source == source) |
| 148 pending_requests_.erase(it++); | 131 pending_requests_.erase(it++); |
| 149 else | 132 else |
| 150 ++it; | 133 ++it; |
| 151 } | 134 } |
| 152 } | 135 } |
| 153 | 136 |
| 154 } // namespace extensions | 137 } // namespace extensions |
| OLD | NEW |