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
3 // found in the LICENSE file.
4
5 #include "mojo/edk/js/handle.h"
6
7 #include "mojo/edk/js/handle_close_observer.h"
8
9 namespace mojo {
10 namespace edk {
11 namespace js {
12
13 gin::WrapperInfo HandleWrapper::kWrapperInfo = { gin::kEmbedderNativeGin };
14
HandleWrapper(MojoHandle handle)15 HandleWrapper::HandleWrapper(MojoHandle handle)
16 : handle_(mojo::Handle(handle)) {
17 }
18
~HandleWrapper()19 HandleWrapper::~HandleWrapper() {
20 NotifyCloseObservers();
21 }
22
Close()23 void HandleWrapper::Close() {
24 NotifyCloseObservers();
25 handle_.reset();
26 }
27
AddCloseObserver(HandleCloseObserver * observer)28 void HandleWrapper::AddCloseObserver(HandleCloseObserver* observer) {
29 close_observers_.AddObserver(observer);
30 }
31
RemoveCloseObserver(HandleCloseObserver * observer)32 void HandleWrapper::RemoveCloseObserver(HandleCloseObserver* observer) {
33 close_observers_.RemoveObserver(observer);
34 }
35
NotifyCloseObservers()36 void HandleWrapper::NotifyCloseObservers() {
37 if (!handle_.is_valid())
38 return;
39
40 FOR_EACH_OBSERVER(HandleCloseObserver, close_observers_, OnWillCloseHandle());
41 }
42
43 } // namespace js
44 } // namespace edk
45 } // namespace mojo
46
47 namespace gin {
48
ToV8(v8::Isolate * isolate,const mojo::Handle & val)49 v8::Handle<v8::Value> Converter<mojo::Handle>::ToV8(v8::Isolate* isolate,
50 const mojo::Handle& val) {
51 if (!val.is_valid())
52 return v8::Null(isolate);
53 return mojo::edk::js::HandleWrapper::Create(isolate, val.value()).ToV8();
54 }
55
FromV8(v8::Isolate * isolate,v8::Handle<v8::Value> val,mojo::Handle * out)56 bool Converter<mojo::Handle>::FromV8(v8::Isolate* isolate,
57 v8::Handle<v8::Value> val,
58 mojo::Handle* out) {
59 if (val->IsNull()) {
60 *out = mojo::Handle();
61 return true;
62 }
63
64 gin::Handle<mojo::edk::js::HandleWrapper> handle;
65 if (!Converter<gin::Handle<mojo::edk::js::HandleWrapper>>::FromV8(
66 isolate, val, &handle))
67 return false;
68
69 *out = handle->get();
70 return true;
71 }
72
ToV8(v8::Isolate * isolate,mojo::MessagePipeHandle val)73 v8::Handle<v8::Value> Converter<mojo::MessagePipeHandle>::ToV8(
74 v8::Isolate* isolate, mojo::MessagePipeHandle val) {
75 return Converter<mojo::Handle>::ToV8(isolate, val);
76 }
77
FromV8(v8::Isolate * isolate,v8::Handle<v8::Value> val,mojo::MessagePipeHandle * out)78 bool Converter<mojo::MessagePipeHandle>::FromV8(v8::Isolate* isolate,
79 v8::Handle<v8::Value> val,
80 mojo::MessagePipeHandle* out) {
81 return Converter<mojo::Handle>::FromV8(isolate, val, out);
82 }
83
84 } // namespace gin
85