| 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/edk/js/js_export.h" |
| 14 #include "mojo/public/cpp/system/core.h" | 15 #include "mojo/public/cpp/system/core.h" |
| 15 | 16 |
| 16 namespace mojo { | 17 namespace mojo { |
| 17 namespace edk { | 18 namespace edk { |
| 18 namespace js { | 19 namespace js { |
| 19 | 20 |
| 20 class HandleCloseObserver; | 21 class HandleCloseObserver; |
| 21 | 22 |
| 22 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle | 23 // Wrapper for mojo Handles exposed to JavaScript. This ensures the Handle |
| 23 // is Closed when its JS object is garbage collected. | 24 // is Closed when its JS object is garbage collected. |
| 24 class HandleWrapper : public gin::Wrappable<HandleWrapper> { | 25 class MOJO_JS_EXPORT HandleWrapper : public gin::Wrappable<HandleWrapper> { |
| 25 public: | 26 public: |
| 26 static gin::WrapperInfo kWrapperInfo; | 27 static gin::WrapperInfo kWrapperInfo; |
| 27 | 28 |
| 28 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, | 29 static gin::Handle<HandleWrapper> Create(v8::Isolate* isolate, |
| 29 MojoHandle handle) { | 30 MojoHandle handle) { |
| 30 return gin::CreateHandle(isolate, new HandleWrapper(handle)); | 31 return gin::CreateHandle(isolate, new HandleWrapper(handle)); |
| 31 } | 32 } |
| 32 | 33 |
| 33 mojo::Handle get() const { return handle_.get(); } | 34 mojo::Handle get() const { return handle_.get(); } |
| 34 mojo::Handle release() { return handle_.release(); } | 35 mojo::Handle release() { return handle_.release(); } |
| (...skipping 14 matching lines...) Expand all Loading... |
| 49 } // namespace js | 50 } // namespace js |
| 50 } // namespace edk | 51 } // namespace edk |
| 51 } // namespace mojo | 52 } // namespace mojo |
| 52 | 53 |
| 53 namespace gin { | 54 namespace gin { |
| 54 | 55 |
| 55 // Note: It's important to use this converter rather than the one for | 56 // Note: It's important to use this converter rather than the one for |
| 56 // MojoHandle, since that will do a simple int32_t conversion. It's unfortunate | 57 // MojoHandle, since that will do a simple int32_t conversion. It's unfortunate |
| 57 // there's no way to prevent against accidental use. | 58 // there's no way to prevent against accidental use. |
| 58 // TODO(mpcomplete): define converters for all Handle subtypes. | 59 // TODO(mpcomplete): define converters for all Handle subtypes. |
| 59 template<> | 60 template <> |
| 60 struct Converter<mojo::Handle> { | 61 struct MOJO_JS_EXPORT Converter<mojo::Handle> { |
| 61 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 62 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 62 const mojo::Handle& val); | 63 const mojo::Handle& val); |
| 63 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | 64 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, |
| 64 mojo::Handle* out); | 65 mojo::Handle* out); |
| 65 }; | 66 }; |
| 66 | 67 |
| 67 template<> | 68 template <> |
| 68 struct Converter<mojo::MessagePipeHandle> { | 69 struct MOJO_JS_EXPORT Converter<mojo::MessagePipeHandle> { |
| 69 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, | 70 static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate, |
| 70 mojo::MessagePipeHandle val); | 71 mojo::MessagePipeHandle val); |
| 71 static bool FromV8(v8::Isolate* isolate, | 72 static bool FromV8(v8::Isolate* isolate, |
| 72 v8::Handle<v8::Value> val, | 73 v8::Handle<v8::Value> val, |
| 73 mojo::MessagePipeHandle* out); | 74 mojo::MessagePipeHandle* out); |
| 74 }; | 75 }; |
| 75 | 76 |
| 76 // 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 |
| 77 // converting |null| to a wrapper for an empty mojo::Handle. | 78 // converting |null| to a wrapper for an empty mojo::Handle. |
| 78 template <> | 79 template <> |
| 79 struct Converter<gin::Handle<mojo::edk::js::HandleWrapper>> { | 80 struct MOJO_JS_EXPORT Converter<gin::Handle<mojo::edk::js::HandleWrapper>> { |
| 80 static v8::Handle<v8::Value> ToV8( | 81 static v8::Handle<v8::Value> ToV8( |
| 81 v8::Isolate* isolate, | 82 v8::Isolate* isolate, |
| 82 const gin::Handle<mojo::edk::js::HandleWrapper>& val) { | 83 const gin::Handle<mojo::edk::js::HandleWrapper>& val) { |
| 83 return val.ToV8(); | 84 return val.ToV8(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 static bool FromV8(v8::Isolate* isolate, | 87 static bool FromV8(v8::Isolate* isolate, |
| 87 v8::Handle<v8::Value> val, | 88 v8::Handle<v8::Value> val, |
| 88 gin::Handle<mojo::edk::js::HandleWrapper>* out) { | 89 gin::Handle<mojo::edk::js::HandleWrapper>* out) { |
| 89 if (val->IsNull()) { | 90 if (val->IsNull()) { |
| 90 *out = mojo::edk::js::HandleWrapper::Create(isolate, MOJO_HANDLE_INVALID); | 91 *out = mojo::edk::js::HandleWrapper::Create(isolate, MOJO_HANDLE_INVALID); |
| 91 return true; | 92 return true; |
| 92 } | 93 } |
| 93 | 94 |
| 94 mojo::edk::js::HandleWrapper* object = NULL; | 95 mojo::edk::js::HandleWrapper* object = NULL; |
| 95 if (!Converter<mojo::edk::js::HandleWrapper*>::FromV8(isolate, val, | 96 if (!Converter<mojo::edk::js::HandleWrapper*>::FromV8(isolate, val, |
| 96 &object)) { | 97 &object)) { |
| 97 return false; | 98 return false; |
| 98 } | 99 } |
| 99 *out = gin::Handle<mojo::edk::js::HandleWrapper>(val, object); | 100 *out = gin::Handle<mojo::edk::js::HandleWrapper>(val, object); |
| 100 return true; | 101 return true; |
| 101 } | 102 } |
| 102 }; | 103 }; |
| 103 | 104 |
| 104 } // namespace gin | 105 } // namespace gin |
| 105 | 106 |
| 106 #endif // MOJO_EDK_JS_HANDLE_H_ | 107 #endif // MOJO_EDK_JS_HANDLE_H_ |
| OLD | NEW |