| OLD | NEW |
| (Empty) |
| 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 <sstream> | |
| 8 #include "mojo/bindings/js/handle_close_observer.h" | |
| 9 | |
| 10 namespace mojo { | |
| 11 namespace js { | |
| 12 | |
| 13 gin::WrapperInfo HandleWrapper::kWrapperInfo = { gin::kEmbedderNativeGin }; | |
| 14 | |
| 15 HandleWrapper::HandleWrapper(MojoHandle handle) | |
| 16 : handle_(mojo::Handle(handle)) { | |
| 17 } | |
| 18 | |
| 19 HandleWrapper::~HandleWrapper() { | |
| 20 NotifyCloseObservers(); | |
| 21 } | |
| 22 | |
| 23 std::string HandleWrapper::ToString() { | |
| 24 std::ostringstream oss; | |
| 25 oss << "[mojo::Handle "; | |
| 26 if (handle_.is_valid()) | |
| 27 oss << handle_.get().value(); | |
| 28 else | |
| 29 oss << "null"; | |
| 30 oss << "]"; | |
| 31 return oss.str(); | |
| 32 } | |
| 33 | |
| 34 gin::ObjectTemplateBuilder HandleWrapper::GetObjectTemplateBuilder( | |
| 35 v8::Isolate* isolate) { | |
| 36 return Wrappable<HandleWrapper>::GetObjectTemplateBuilder(isolate) | |
| 37 .SetMethod("toString", &HandleWrapper::ToString); | |
| 38 } | |
| 39 | |
| 40 void HandleWrapper::Close() { | |
| 41 NotifyCloseObservers(); | |
| 42 handle_.reset(); | |
| 43 } | |
| 44 | |
| 45 void HandleWrapper::AddCloseObserver(HandleCloseObserver* observer) { | |
| 46 close_observers_.AddObserver(observer); | |
| 47 } | |
| 48 | |
| 49 void HandleWrapper::RemoveCloseObserver(HandleCloseObserver* observer) { | |
| 50 close_observers_.RemoveObserver(observer); | |
| 51 } | |
| 52 | |
| 53 void HandleWrapper::NotifyCloseObservers() { | |
| 54 if (!handle_.is_valid()) | |
| 55 return; | |
| 56 | |
| 57 FOR_EACH_OBSERVER(HandleCloseObserver, close_observers_, OnWillCloseHandle()); | |
| 58 } | |
| 59 | |
| 60 } // namespace js | |
| 61 } // namespace mojo | |
| 62 | |
| 63 namespace gin { | |
| 64 | |
| 65 v8::Handle<v8::Value> Converter<mojo::Handle>::ToV8(v8::Isolate* isolate, | |
| 66 const mojo::Handle& val) { | |
| 67 if (!val.is_valid()) | |
| 68 return v8::Null(isolate); | |
| 69 return mojo::js::HandleWrapper::Create(isolate, val.value()).ToV8(); | |
| 70 } | |
| 71 | |
| 72 bool Converter<mojo::Handle>::FromV8(v8::Isolate* isolate, | |
| 73 v8::Handle<v8::Value> val, | |
| 74 mojo::Handle* out) { | |
| 75 if (val->IsNull()) { | |
| 76 *out = mojo::Handle(); | |
| 77 return true; | |
| 78 } | |
| 79 | |
| 80 gin::Handle<mojo::js::HandleWrapper> handle; | |
| 81 if (!Converter<gin::Handle<mojo::js::HandleWrapper> >::FromV8( | |
| 82 isolate, val, &handle)) | |
| 83 return false; | |
| 84 | |
| 85 *out = handle->get(); | |
| 86 return true; | |
| 87 } | |
| 88 | |
| 89 v8::Handle<v8::Value> Converter<mojo::MessagePipeHandle>::ToV8( | |
| 90 v8::Isolate* isolate, mojo::MessagePipeHandle val) { | |
| 91 return Converter<mojo::Handle>::ToV8(isolate, val); | |
| 92 } | |
| 93 | |
| 94 bool Converter<mojo::MessagePipeHandle>::FromV8(v8::Isolate* isolate, | |
| 95 v8::Handle<v8::Value> val, | |
| 96 mojo::MessagePipeHandle* out) { | |
| 97 return Converter<mojo::Handle>::FromV8(isolate, val, out); | |
| 98 } | |
| 99 | |
| 100 | |
| 101 } // namespace gin | |
| OLD | NEW |