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