| 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 #ifndef MOJO_BINDINGS_JS_HANDLE_H_ | |
| 6 #define MOJO_BINDINGS_JS_HANDLE_H_ | |
| 7 | |
| 8 #include "base/observer_list.h" | |
| 9 #include "gin/converter.h" | |
| 10 #include "gin/handle.h" | |
| 11 #include "gin/object_template_builder.h" | |
| 12 #include "gin/wrappable.h" | |
| 13 #include "mojo/public/cpp/system/core.h" | |
| 14 | |
| 15 namespace mojo { | |
| 16 namespace js { | |
| 17 class HandleCloseObserver; | |
| 18 | |
| 19 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle | |
| 20 // is Closed when its JS object is garbage collected. | |
| 21 class HandleWrapper : public gin::Wrappable<HandleWrapper> { | |
| 22 public: | |
| 23 static gin::WrapperInfo kWrapperInfo; | |
| 24 | |
| 25 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, | |
| 26 MojoHandle handle) { | |
| 27 return gin::CreateHandle(isolate, new HandleWrapper(handle)); | |
| 28 } | |
| 29 | |
| 30 std::string ToString(); | |
| 31 | |
| 32 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) | |
| 33 override; | |
| 34 | |
| 35 mojo::Handle get() const { return handle_.get(); } | |
| 36 mojo::Handle release() { return handle_.release(); } | |
| 37 void Close(); | |
| 38 | |
| 39 void AddCloseObserver(HandleCloseObserver* observer); | |
| 40 void RemoveCloseObserver(HandleCloseObserver* observer); | |
| 41 | |
| 42 protected: | |
| 43 HandleWrapper(MojoHandle handle); | |
| 44 ~HandleWrapper() override; | |
| 45 void NotifyCloseObservers(); | |
| 46 | |
| 47 mojo::ScopedHandle handle_; | |
| 48 ObserverList<HandleCloseObserver> close_observers_; | |
| 49 }; | |
| 50 | |
| 51 } // namespace js | |
| 52 } // namespace mojo | |
| 53 | |
| 54 namespace gin { | |
| 55 | |
| 56 // Note: It's important to use this converter rather than the one for | |
| 57 // MojoHandle, since that will do a simple int32 conversion. It's unfortunate | |
| 58 // there's no way to prevent against accidental use. | |
| 59 // TODO(mpcomplete): define converters for all Handle subtypes. | |
| 60 template<> | |
| 61 struct Converter<mojo::Handle> { | |
| 62 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
| 63 const mojo::Handle& val); | |
| 64 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
| 65 mojo::Handle* out); | |
| 66 }; | |
| 67 | |
| 68 template<> | |
| 69 struct Converter<mojo::MessagePipeHandle> { | |
| 70 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | |
| 71 mojo::MessagePipeHandle val); | |
| 72 static bool FromV8(v8::Isolate* isolate, | |
| 73 v8::Handle<v8::Value> val, | |
| 74 mojo::MessagePipeHandle* out); | |
| 75 }; | |
| 76 | |
| 77 // We need to specialize the normal gin::Handle converter in order to handle | |
| 78 // converting |null| to a wrapper for an empty mojo::Handle. | |
| 79 template<> | |
| 80 struct Converter<gin::Handle<mojo::js::HandleWrapper> > { | |
| 81 static v8::Handle<v8::Value> ToV8( | |
| 82 v8::Isolate* isolate, const gin::Handle<mojo::js::HandleWrapper>& val) { | |
| 83 return val.ToV8(); | |
| 84 } | |
| 85 | |
| 86 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
| 87 gin::Handle<mojo::js::HandleWrapper>* out) { | |
| 88 if (val->IsNull()) { | |
| 89 *out = mojo::js::HandleWrapper::Create(isolate, MOJO_HANDLE_INVALID); | |
| 90 return true; | |
| 91 } | |
| 92 | |
| 93 mojo::js::HandleWrapper* object = NULL; | |
| 94 if (!Converter<mojo::js::HandleWrapper*>::FromV8(isolate, val, &object)) { | |
| 95 return false; | |
| 96 } | |
| 97 *out = gin::Handle<mojo::js::HandleWrapper>(val, object); | |
| 98 return true; | |
| 99 } | |
| 100 }; | |
| 101 | |
| 102 } // namespace gin | |
| 103 | |
| 104 #endif // MOJO_BINDINGS_JS_HANDLE_H_ | |
| OLD | NEW |