• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/bindings/js/handle.h"
6 
7 #include "mojo/bindings/js/handle_close_observer.h"
8 
9 namespace gin {
10 
11 gin::WrapperInfo HandleWrapper::kWrapperInfo = { gin::kEmbedderNativeGin };
12 
HandleWrapper(MojoHandle handle)13 HandleWrapper::HandleWrapper(MojoHandle handle)
14     : handle_(mojo::Handle(handle)) {
15 }
16 
~HandleWrapper()17 HandleWrapper::~HandleWrapper() {
18   NotifyCloseObservers();
19 }
20 
Close()21 void HandleWrapper::Close() {
22   NotifyCloseObservers();
23   handle_.reset();
24 }
25 
AddCloseObserver(HandleCloseObserver * observer)26 void HandleWrapper::AddCloseObserver(HandleCloseObserver* observer) {
27   close_observers_.AddObserver(observer);
28 }
29 
RemoveCloseObserver(HandleCloseObserver * observer)30 void HandleWrapper::RemoveCloseObserver(HandleCloseObserver* observer) {
31   close_observers_.RemoveObserver(observer);
32 }
33 
NotifyCloseObservers()34 void HandleWrapper::NotifyCloseObservers() {
35   if (!handle_.is_valid())
36     return;
37 
38   FOR_EACH_OBSERVER(HandleCloseObserver, close_observers_, OnWillCloseHandle());
39 }
40 
ToV8(v8::Isolate * isolate,const mojo::Handle & val)41 v8::Handle<v8::Value> Converter<mojo::Handle>::ToV8(v8::Isolate* isolate,
42                                                     const mojo::Handle& val) {
43   if (!val.is_valid())
44     return v8::Null(isolate);
45   return HandleWrapper::Create(isolate, val.value()).ToV8();
46 }
47 
FromV8(v8::Isolate * isolate,v8::Handle<v8::Value> val,mojo::Handle * out)48 bool Converter<mojo::Handle>::FromV8(v8::Isolate* isolate,
49                                      v8::Handle<v8::Value> val,
50                                      mojo::Handle* out) {
51   if (val->IsNull()) {
52     *out = mojo::Handle();
53     return true;
54   }
55 
56   gin::Handle<HandleWrapper> handle;
57   if (!Converter<gin::Handle<HandleWrapper> >::FromV8(isolate, val, &handle))
58     return false;
59 
60   *out = handle->get();
61   return true;
62 }
63 
64 }  // namespace gin
65