| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | // Copyright (c) 2013 GitHub, Inc. All rights reserved.
 | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style license that can be
 | 
					
						
							|  |  |  | // found in the LICENSE file.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 08:30:26 +08:00
										 |  |  | #include "atom/browser/api/atom_api_event.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-16 08:30:26 +08:00
										 |  |  | #include "atom/common/api/api_messages.h"
 | 
					
						
							|  |  |  | #include "atom/common/v8/node_common.h"
 | 
					
						
							|  |  |  | #include "atom/common/v8/native_type_conversions.h"
 | 
					
						
							| 
									
										
										
										
											2014-04-04 22:04:42 +08:00
										 |  |  | #include "content/public/browser/web_contents.h"
 | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace atom { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace api { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | ScopedPersistent<v8::Function> Event::constructor_template_; | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | Event::Event() | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  |     : sender_(NULL), | 
					
						
							|  |  |  |       message_(NULL), | 
					
						
							|  |  |  |       prevent_default_(false) { | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Event::~Event() { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // static
 | 
					
						
							|  |  |  | v8::Handle<v8::Object> Event::CreateV8Object() { | 
					
						
							|  |  |  |   if (constructor_template_.IsEmpty()) { | 
					
						
							|  |  |  |     v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New); | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  |     t->InstanceTemplate()->SetInternalFieldCount(1); | 
					
						
							|  |  |  |     t->SetClassName(v8::String::NewSymbol("Event")); | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     NODE_SET_PROTOTYPE_METHOD(t, "preventDefault", PreventDefault); | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  |     NODE_SET_PROTOTYPE_METHOD(t, "sendReply", SendReply); | 
					
						
							| 
									
										
										
										
											2013-09-26 11:27:09 +08:00
										 |  |  |     NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy); | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  |     constructor_template_.reset(t->GetFunction()); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  |   v8::Handle<v8::Function> t = constructor_template_.NewHandle(node_isolate); | 
					
						
							|  |  |  |   return t->NewInstance(0, NULL); | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-04 22:04:42 +08:00
										 |  |  | void Event::SetSenderAndMessage(content::WebContents* sender, | 
					
						
							|  |  |  |                                 IPC::Message* message) { | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  |   DCHECK(!sender_); | 
					
						
							|  |  |  |   DCHECK(!message_); | 
					
						
							|  |  |  |   sender_ = sender; | 
					
						
							|  |  |  |   message_ = message; | 
					
						
							| 
									
										
										
										
											2013-09-22 10:43:54 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-04 22:04:42 +08:00
										 |  |  |   Observe(sender); | 
					
						
							| 
									
										
										
										
											2013-09-22 10:43:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-04 22:04:42 +08:00
										 |  |  | void Event::WebContentsDestroyed(content::WebContents* web_contents) { | 
					
						
							| 
									
										
										
										
											2013-09-22 10:43:54 +08:00
										 |  |  |   sender_ = NULL; | 
					
						
							|  |  |  |   message_ = NULL; | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | void Event::New(const v8::FunctionCallbackInfo<v8::Value>& args) { | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  |   Event* event = new Event; | 
					
						
							|  |  |  |   event->Wrap(args.This()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | void Event::PreventDefault(const v8::FunctionCallbackInfo<v8::Value>& args) { | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  |   Event* event = Unwrap<Event>(args.This()); | 
					
						
							| 
									
										
										
										
											2013-04-29 19:38:18 +08:00
										 |  |  |   if (event == NULL) | 
					
						
							|  |  |  |     return node::ThrowError("Event is already destroyed"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  |   event->prevent_default_ = true; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | void Event::SendReply(const v8::FunctionCallbackInfo<v8::Value>& args) { | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  |   Event* event = Unwrap<Event>(args.This()); | 
					
						
							|  |  |  |   if (event == NULL) | 
					
						
							|  |  |  |     return node::ThrowError("Event is already destroyed"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-26 11:27:09 +08:00
										 |  |  |   if (event->message_ == NULL || event->sender_ == NULL) | 
					
						
							|  |  |  |     return node::ThrowError("Can only send reply to synchronous events"); | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-23 23:00:58 +08:00
										 |  |  |   string16 json = FromV8Value(args[0]); | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   AtomViewHostMsg_Message_Sync::WriteReplyParams(event->message_, json); | 
					
						
							|  |  |  |   event->sender_->Send(event->message_); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-26 11:28:53 +08:00
										 |  |  |   delete event; | 
					
						
							| 
									
										
										
										
											2013-09-22 09:52:58 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-26 11:27:09 +08:00
										 |  |  | // static
 | 
					
						
							| 
									
										
										
										
											2013-12-11 15:48:19 +08:00
										 |  |  | void Event::Destroy(const v8::FunctionCallbackInfo<v8::Value>& args) { | 
					
						
							| 
									
										
										
										
											2013-09-26 11:27:09 +08:00
										 |  |  |   delete Unwrap<Event>(args.This()); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-18 21:42:20 +08:00
										 |  |  | }  // namespace api
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace atom
 |