OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/renderer/messaging_bindings.h" | 5 #include "extensions/renderer/messaging_bindings.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include <map> | 9 #include <map> |
8 #include <string> | 10 #include <string> |
9 | 11 |
10 #include "base/basictypes.h" | |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
13 #include "base/callback.h" | 14 #include "base/callback.h" |
14 #include "base/lazy_instance.h" | 15 #include "base/lazy_instance.h" |
| 16 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
16 #include "base/message_loop/message_loop.h" | 18 #include "base/message_loop/message_loop.h" |
17 #include "base/values.h" | 19 #include "base/values.h" |
18 #include "components/guest_view/common/guest_view_constants.h" | 20 #include "components/guest_view/common/guest_view_constants.h" |
19 #include "content/public/child/v8_value_converter.h" | 21 #include "content/public/child/v8_value_converter.h" |
20 #include "content/public/common/child_process_host.h" | 22 #include "content/public/common/child_process_host.h" |
21 #include "content/public/renderer/render_frame.h" | 23 #include "content/public/renderer/render_frame.h" |
22 #include "content/public/renderer/render_thread.h" | 24 #include "content/public/renderer/render_thread.h" |
23 #include "extensions/common/api/messaging/message.h" | 25 #include "extensions/common/api/messaging/message.h" |
24 #include "extensions/common/extension_messages.h" | 26 #include "extensions/common/extension_messages.h" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 g_port_tracker.Get().DeletePort(port_id); | 172 g_port_tracker.Get().DeletePort(port_id); |
171 dispatcher_->ClearPortData(port_id); | 173 dispatcher_->ClearPortData(port_id); |
172 } | 174 } |
173 | 175 |
174 // Sends a message along the given channel. | 176 // Sends a message along the given channel. |
175 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { | 177 void PostMessage(const v8::FunctionCallbackInfo<v8::Value>& args) { |
176 content::RenderFrame* renderframe = context()->GetRenderFrame(); | 178 content::RenderFrame* renderframe = context()->GetRenderFrame(); |
177 if (!renderframe) | 179 if (!renderframe) |
178 return; | 180 return; |
179 | 181 |
180 // Arguments are (int32 port_id, string message). | 182 // Arguments are (int32_t port_id, string message). |
181 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); | 183 CHECK(args.Length() == 2 && args[0]->IsInt32() && args[1]->IsString()); |
182 | 184 |
183 int port_id = args[0].As<v8::Int32>()->Value(); | 185 int port_id = args[0].As<v8::Int32>()->Value(); |
184 if (!g_port_tracker.Get().HasPort(port_id)) { | 186 if (!g_port_tracker.Get().HasPort(port_id)) { |
185 v8::Local<v8::String> error_message = | 187 v8::Local<v8::String> error_message = |
186 ToV8StringUnsafe(args.GetIsolate(), kPortClosedError); | 188 ToV8StringUnsafe(args.GetIsolate(), kPortClosedError); |
187 args.GetIsolate()->ThrowException(v8::Exception::Error(error_message)); | 189 args.GetIsolate()->ThrowException(v8::Exception::Error(error_message)); |
188 return; | 190 return; |
189 } | 191 } |
190 | 192 |
191 renderframe->Send(new ExtensionHostMsg_PostMessage( | 193 renderframe->Send(new ExtensionHostMsg_PostMessage( |
192 renderframe->GetRoutingID(), port_id, | 194 renderframe->GetRoutingID(), port_id, |
193 Message(*v8::String::Utf8Value(args[1]), | 195 Message(*v8::String::Utf8Value(args[1]), |
194 blink::WebUserGestureIndicator::isProcessingUserGesture()))); | 196 blink::WebUserGestureIndicator::isProcessingUserGesture()))); |
195 } | 197 } |
196 | 198 |
197 // Forcefully disconnects a port. | 199 // Forcefully disconnects a port. |
198 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { | 200 void CloseChannel(const v8::FunctionCallbackInfo<v8::Value>& args) { |
199 // Arguments are (int32 port_id, boolean notify_browser). | 201 // Arguments are (int32_t port_id, boolean notify_browser). |
200 CHECK_EQ(2, args.Length()); | 202 CHECK_EQ(2, args.Length()); |
201 CHECK(args[0]->IsInt32()); | 203 CHECK(args[0]->IsInt32()); |
202 CHECK(args[1]->IsBoolean()); | 204 CHECK(args[1]->IsBoolean()); |
203 | 205 |
204 int port_id = args[0].As<v8::Int32>()->Value(); | 206 int port_id = args[0].As<v8::Int32>()->Value(); |
205 if (!g_port_tracker.Get().HasPort(port_id)) | 207 if (!g_port_tracker.Get().HasPort(port_id)) |
206 return; | 208 return; |
207 | 209 |
208 // Send via the RenderThread because the RenderFrame might be closing. | 210 // Send via the RenderThread because the RenderFrame might be closing. |
209 bool notify_browser = args[1].As<v8::Boolean>()->Value(); | 211 bool notify_browser = args[1].As<v8::Boolean>()->Value(); |
210 if (notify_browser) { | 212 if (notify_browser) { |
211 content::RenderThread::Get()->Send( | 213 content::RenderThread::Get()->Send( |
212 new ExtensionHostMsg_CloseChannel(port_id, std::string())); | 214 new ExtensionHostMsg_CloseChannel(port_id, std::string())); |
213 } | 215 } |
214 | 216 |
215 ClearPortDataAndNotifyDispatcher(port_id); | 217 ClearPortDataAndNotifyDispatcher(port_id); |
216 } | 218 } |
217 | 219 |
218 // A new port has been created for a context. This occurs both when script | 220 // A new port has been created for a context. This occurs both when script |
219 // opens a connection, and when a connection is opened to this script. | 221 // opens a connection, and when a connection is opened to this script. |
220 void PortAddRef(const v8::FunctionCallbackInfo<v8::Value>& args) { | 222 void PortAddRef(const v8::FunctionCallbackInfo<v8::Value>& args) { |
221 // Arguments are (int32 port_id). | 223 // Arguments are (int32_t port_id). |
222 CHECK_EQ(1, args.Length()); | 224 CHECK_EQ(1, args.Length()); |
223 CHECK(args[0]->IsInt32()); | 225 CHECK(args[0]->IsInt32()); |
224 | 226 |
225 int port_id = args[0].As<v8::Int32>()->Value(); | 227 int port_id = args[0].As<v8::Int32>()->Value(); |
226 g_port_tracker.Get().AddReference(context(), port_id); | 228 g_port_tracker.Get().AddReference(context(), port_id); |
227 } | 229 } |
228 | 230 |
229 // The frame a port lived in has been destroyed. When there are no more | 231 // The frame a port lived in has been destroyed. When there are no more |
230 // frames with a reference to a given port, we will disconnect it and notify | 232 // frames with a reference to a given port, we will disconnect it and notify |
231 // the other end of the channel. | 233 // the other end of the channel. |
232 void PortRelease(const v8::FunctionCallbackInfo<v8::Value>& args) { | 234 void PortRelease(const v8::FunctionCallbackInfo<v8::Value>& args) { |
233 // Arguments are (int32 port_id). | 235 // Arguments are (int32_t port_id). |
234 CHECK(args.Length() == 1 && args[0]->IsInt32()); | 236 CHECK(args.Length() == 1 && args[0]->IsInt32()); |
235 ReleasePort(args[0].As<v8::Int32>()->Value()); | 237 ReleasePort(args[0].As<v8::Int32>()->Value()); |
236 } | 238 } |
237 | 239 |
238 // Releases the reference to |port_id| for this context, and clears all port | 240 // Releases the reference to |port_id| for this context, and clears all port |
239 // data if there are no more references. | 241 // data if there are no more references. |
240 void ReleasePort(int port_id) { | 242 void ReleasePort(int port_id) { |
241 if (g_port_tracker.Get().RemoveReference(context(), port_id) && | 243 if (g_port_tracker.Get().RemoveReference(context(), port_id) && |
242 !g_port_tracker.Get().HasPort(port_id)) { | 244 !g_port_tracker.Get().HasPort(port_id)) { |
243 // Send via the RenderThread because the RenderFrame might be closing. | 245 // Send via the RenderThread because the RenderFrame might be closing. |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
494 const ScriptContextSet& context_set, | 496 const ScriptContextSet& context_set, |
495 int port_id, | 497 int port_id, |
496 const std::string& error_message, | 498 const std::string& error_message, |
497 content::RenderFrame* restrict_to_render_frame) { | 499 content::RenderFrame* restrict_to_render_frame) { |
498 context_set.ForEach( | 500 context_set.ForEach( |
499 restrict_to_render_frame, | 501 restrict_to_render_frame, |
500 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); | 502 base::Bind(&DispatchOnDisconnectToScriptContext, port_id, error_message)); |
501 } | 503 } |
502 | 504 |
503 } // namespace extensions | 505 } // namespace extensions |
OLD | NEW |