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