Chromium Code Reviews| 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 #include "mojo/bindings/js/handle.h" | 5 #include "mojo/bindings/js/handle.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | |
| 8 | |
| 7 namespace gin { | 9 namespace gin { |
| 8 | 10 |
| 9 gin::WrapperInfo HandleWrapper::kWrapperInfo = { gin::kEmbedderNativeGin }; | 11 gin::WrapperInfo HandleWrapper::kWrapperInfo = { gin::kEmbedderNativeGin }; |
| 10 | 12 |
| 11 HandleWrapper::HandleWrapper(MojoHandle handle) | 13 HandleWrapper::HandleWrapper(MojoHandle handle) |
| 12 : handle_(mojo::Handle(handle)) { | 14 : handle_(mojo::Handle(handle)) { |
| 13 } | 15 } |
| 14 | 16 |
| 15 HandleWrapper::~HandleWrapper() { | 17 HandleWrapper::~HandleWrapper() { |
| 18 NotifyCloseObservers(); | |
| 19 } | |
| 20 | |
| 21 void HandleWrapper::Close() { | |
| 22 NotifyCloseObservers(); | |
| 23 handle_.reset(); | |
| 24 } | |
| 25 | |
| 26 void HandleWrapper::AddCloseObserver(CloseObserver* observer) { | |
| 27 bool inserted = observers_.insert(observer).second; | |
| 28 DCHECK(inserted); | |
| 29 } | |
| 30 | |
| 31 void HandleWrapper::RemoveCloseObserver(CloseObserver* observer) { | |
| 32 size_t removed = observers_.erase(observer); | |
| 33 DCHECK_EQ(removed, 1u); | |
| 34 } | |
| 35 | |
| 36 void HandleWrapper::NotifyCloseObservers() { | |
| 37 for (std::set<CloseObserver*>::iterator it = observers_.begin(); | |
| 38 it != observers_.end();) { | |
| 39 (*it++)->OnHandleClosed(); | |
|
abarth-chromium
2014/09/03 05:21:55
What if observers_ is modified during this iterati
Sam McNally
2014/09/03 05:37:27
Done.
| |
| 40 } | |
| 41 observers_.clear(); | |
| 16 } | 42 } |
| 17 | 43 |
| 18 v8::Handle<v8::Value> Converter<mojo::Handle>::ToV8(v8::Isolate* isolate, | 44 v8::Handle<v8::Value> Converter<mojo::Handle>::ToV8(v8::Isolate* isolate, |
| 19 const mojo::Handle& val) { | 45 const mojo::Handle& val) { |
| 20 if (!val.is_valid()) | 46 if (!val.is_valid()) |
| 21 return v8::Null(isolate); | 47 return v8::Null(isolate); |
| 22 return HandleWrapper::Create(isolate, val.value()).ToV8(); | 48 return HandleWrapper::Create(isolate, val.value()).ToV8(); |
| 23 } | 49 } |
| 24 | 50 |
| 25 bool Converter<mojo::Handle>::FromV8(v8::Isolate* isolate, | 51 bool Converter<mojo::Handle>::FromV8(v8::Isolate* isolate, |
| 26 v8::Handle<v8::Value> val, | 52 v8::Handle<v8::Value> val, |
| 27 mojo::Handle* out) { | 53 mojo::Handle* out) { |
| 28 if (val->IsNull()) { | 54 if (val->IsNull()) { |
| 29 *out = mojo::Handle(); | 55 *out = mojo::Handle(); |
| 30 return true; | 56 return true; |
| 31 } | 57 } |
| 32 | 58 |
| 33 gin::Handle<HandleWrapper> handle; | 59 gin::Handle<HandleWrapper> handle; |
| 34 if (!Converter<gin::Handle<HandleWrapper> >::FromV8(isolate, val, &handle)) | 60 if (!Converter<gin::Handle<HandleWrapper> >::FromV8(isolate, val, &handle)) |
| 35 return false; | 61 return false; |
| 36 | 62 |
| 37 *out = handle->get(); | 63 *out = handle->get(); |
| 38 return true; | 64 return true; |
| 39 } | 65 } |
| 40 | 66 |
| 41 } // namespace gin | 67 } // namespace gin |
| OLD | NEW |