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