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 "content/public/renderer/render_view.h" | 11 #include "content/public/renderer/render_view.h" |
12 #include "content/public/renderer/v8_value_converter.h" | 12 #include "content/public/renderer/v8_value_converter.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
16 | 16 |
17 using content::V8ValueConverter; | 17 using content::V8ValueConverter; |
18 | 18 |
19 namespace extensions { | 19 namespace extensions { |
20 | 20 |
21 // Contains info relevant to a pending API request. | 21 // Contains info relevant to a pending API request. |
22 struct PendingRequest { | 22 struct PendingRequest { |
23 public : | 23 public : |
24 PendingRequest(ChromeV8Context* context, | 24 PendingRequest(const std::string& name, ChromeV8Context* context) |
25 ChromeV8Context* caller_context, | 25 : name(name), context(context) { |
26 const std::string& name) | |
27 : name(name), context(context), caller_context(caller_context) { | |
28 } | 26 } |
29 | 27 |
30 std::string name; | 28 std::string name; |
31 ChromeV8Context* context; | 29 ChromeV8Context* context; |
32 ChromeV8Context* caller_context; | |
33 }; | 30 }; |
34 | 31 |
35 RequestSender::RequestSender(Dispatcher* dispatcher) : dispatcher_(dispatcher) { | 32 RequestSender::RequestSender(Dispatcher* dispatcher) : dispatcher_(dispatcher) { |
36 } | 33 } |
37 | 34 |
38 RequestSender::~RequestSender() { | 35 RequestSender::~RequestSender() { |
39 } | 36 } |
40 | 37 |
41 void RequestSender::InsertRequest(int request_id, | 38 void RequestSender::InsertRequest(int request_id, |
42 PendingRequest* pending_request) { | 39 PendingRequest* pending_request) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 return; | 74 return; |
78 | 75 |
79 GURL source_url; | 76 GURL source_url; |
80 WebKit::WebSecurityOrigin source_origin; | 77 WebKit::WebSecurityOrigin source_origin; |
81 WebKit::WebFrame* webframe = context->web_frame(); | 78 WebKit::WebFrame* webframe = context->web_frame(); |
82 if (webframe) { | 79 if (webframe) { |
83 source_url = webframe->document().url(); | 80 source_url = webframe->document().url(); |
84 source_origin = webframe->document().securityOrigin(); | 81 source_origin = webframe->document().securityOrigin(); |
85 } | 82 } |
86 | 83 |
87 std::string extension_id = context->GetExtensionID(); | 84 InsertRequest(request_id, new PendingRequest(name, context)); |
88 // Insert the current context into the PendingRequest because that's the | |
89 // context that we call back on. | |
90 InsertRequest( | |
91 request_id, | |
92 new PendingRequest(context, | |
93 dispatcher_->v8_context_set().GetCurrent(), | |
94 name)); | |
95 | 85 |
96 ExtensionHostMsg_Request_Params params; | 86 ExtensionHostMsg_Request_Params params; |
97 params.name = name; | 87 params.name = name; |
98 params.arguments.Swap(value_args); | 88 params.arguments.Swap(value_args); |
99 params.extension_id = extension_id; | 89 params.extension_id = context->GetExtensionID(); |
100 params.source_url = source_url; | 90 params.source_url = source_url; |
101 params.source_origin = source_origin.toString(); | 91 params.source_origin = source_origin.toString(); |
102 params.request_id = request_id; | 92 params.request_id = request_id; |
103 params.has_callback = has_callback; | 93 params.has_callback = has_callback; |
104 params.user_gesture = | 94 params.user_gesture = |
105 webframe ? webframe->isProcessingUserGesture() : false; | 95 webframe ? webframe->isProcessingUserGesture() : false; |
106 if (for_io_thread) { | 96 if (for_io_thread) { |
107 renderview->Send(new ExtensionHostMsg_RequestForIOThread( | 97 renderview->Send(new ExtensionHostMsg_RequestForIOThread( |
108 renderview->GetRoutingID(), params)); | 98 renderview->GetRoutingID(), params)); |
109 } else { | 99 } else { |
(...skipping 19 matching lines...) Expand all Loading... |
129 v8::Handle<v8::Value> argv[] = { | 119 v8::Handle<v8::Value> argv[] = { |
130 v8::Integer::New(request_id), | 120 v8::Integer::New(request_id), |
131 v8::String::New(request->name.c_str()), | 121 v8::String::New(request->name.c_str()), |
132 v8::Boolean::New(success), | 122 v8::Boolean::New(success), |
133 converter->ToV8Value(&responseList, request->context->v8_context()), | 123 converter->ToV8Value(&responseList, request->context->v8_context()), |
134 v8::String::New(error.c_str()) | 124 v8::String::New(error.c_str()) |
135 }; | 125 }; |
136 | 126 |
137 v8::Handle<v8::Value> retval; | 127 v8::Handle<v8::Value> retval; |
138 CHECK(request->context->CallChromeHiddenMethod("handleResponse", | 128 CHECK(request->context->CallChromeHiddenMethod("handleResponse", |
139 arraysize(argv), | 129 arraysize(argv), |
140 argv, | 130 argv, |
141 &retval)); | 131 &retval)); |
142 // In debug, the js will validate the callback parameters and return a | 132 // In debug, the js will validate the callback parameters and return a |
143 // string if a validation error has occured. | 133 // string if a validation error has occured. |
144 if (DCHECK_IS_ON()) { | 134 if (DCHECK_IS_ON()) { |
145 if (!retval.IsEmpty() && !retval->IsUndefined()) { | 135 if (!retval.IsEmpty() && !retval->IsUndefined()) { |
146 std::string error = *v8::String::AsciiValue(retval); | 136 std::string error = *v8::String::AsciiValue(retval); |
147 DCHECK(false) << error; | 137 DCHECK(false) << error; |
148 } | 138 } |
149 } | 139 } |
150 } | 140 } |
151 | 141 |
152 void RequestSender::InvalidateContext(ChromeV8Context* context) { | 142 void RequestSender::InvalidateContext(ChromeV8Context* context) { |
153 for (PendingRequestMap::iterator it = pending_requests_.begin(); | 143 for (PendingRequestMap::iterator it = pending_requests_.begin(); |
154 it != pending_requests_.end();) { | 144 it != pending_requests_.end();) { |
155 if (it->second->context == context) | 145 if (it->second->context == context) |
156 pending_requests_.erase(it++); | 146 pending_requests_.erase(it++); |
157 else | 147 else |
158 ++it; | 148 ++it; |
159 } | 149 } |
160 } | 150 } |
161 | 151 |
162 } // namespace extensions | 152 } // namespace extensions |
OLD | NEW |