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