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