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