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