Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: mojo/edk/js/waiting_callback.cc

Issue 1722543003: Mojo bindings environment: remove usage in mojo/edk/js (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « mojo/edk/js/waiting_callback.h ('k') | mojo/mojo_edk.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/edk/js/waiting_callback.h" 5 #include "mojo/edk/js/waiting_callback.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h" 8 #include "base/message_loop/message_loop.h"
9 #include "gin/per_context_data.h" 9 #include "gin/per_context_data.h"
10 #include "mojo/public/cpp/environment/environment.h"
11 10
12 namespace mojo { 11 namespace mojo {
13 namespace edk { 12 namespace edk {
14 namespace js { 13 namespace js {
15 14
16 namespace { 15 namespace {
17 16
18 v8::Handle<v8::Private> GetHiddenPropertyName(v8::Isolate* isolate) { 17 v8::Handle<v8::Private> GetHiddenPropertyName(v8::Isolate* isolate) {
19 return v8::Private::ForApi( 18 return v8::Private::ForApi(
20 isolate, gin::StringToV8(isolate, "::mojo::js::WaitingCallback")); 19 isolate, gin::StringToV8(isolate, "::mojo::js::WaitingCallback"));
21 } 20 }
22 21
23 } // namespace 22 } // namespace
24 23
25 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin }; 24 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin };
26 25
27 // static 26 // static
28 gin::Handle<WaitingCallback> WaitingCallback::Create( 27 gin::Handle<WaitingCallback> WaitingCallback::Create(
29 v8::Isolate* isolate, 28 v8::Isolate* isolate,
30 v8::Handle<v8::Function> callback, 29 v8::Handle<v8::Function> callback,
31 gin::Handle<HandleWrapper> handle_wrapper, 30 gin::Handle<HandleWrapper> handle_wrapper,
32 MojoHandleSignals signals) { 31 MojoHandleSignals signals) {
33 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle( 32 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle(
34 isolate, new WaitingCallback(isolate, callback, handle_wrapper)); 33 isolate, new WaitingCallback(isolate, callback, handle_wrapper));
35 waiting_callback->wait_id_ = Environment::GetDefaultAsyncWaiter()->AsyncWait( 34
36 handle_wrapper->get().value(), 35 waiting_callback->started_handle_watcher_ = true;
jam 2016/02/23 16:54:13 i'm curious why keep this extra state around, whic
yzshen1 2016/02/23 21:22:07 Done.
37 signals, 36 waiting_callback->handle_watcher_.Start(
38 MOJO_DEADLINE_INDEFINITE, 37 handle_wrapper->get(), signals, MOJO_DEADLINE_INDEFINITE,
39 &WaitingCallback::CallOnHandleReady, 38 base::Bind(&WaitingCallback::OnHandleReady,
40 waiting_callback.get()); 39 base::Unretained(waiting_callback.get())));
41 return waiting_callback; 40 return waiting_callback;
42 } 41 }
43 42
44 void WaitingCallback::Cancel() { 43 void WaitingCallback::Cancel() {
45 if (!wait_id_) 44 if (!started_handle_watcher_)
46 return; 45 return;
47 46
48 handle_wrapper_->RemoveCloseObserver(this); 47 handle_wrapper_->RemoveCloseObserver(this);
49 handle_wrapper_ = NULL; 48 handle_wrapper_ = NULL;
50 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); 49 handle_watcher_.Stop();
51 wait_id_ = 0; 50 started_handle_watcher_ = false;
52 } 51 }
53 52
54 WaitingCallback::WaitingCallback(v8::Isolate* isolate, 53 WaitingCallback::WaitingCallback(v8::Isolate* isolate,
55 v8::Handle<v8::Function> callback, 54 v8::Handle<v8::Function> callback,
56 gin::Handle<HandleWrapper> handle_wrapper) 55 gin::Handle<HandleWrapper> handle_wrapper)
57 : wait_id_(0), handle_wrapper_(handle_wrapper.get()), weak_factory_(this) { 56 : started_handle_watcher_(false),
57 handle_wrapper_(handle_wrapper.get()),
58 weak_factory_(this) {
58 handle_wrapper_->AddCloseObserver(this); 59 handle_wrapper_->AddCloseObserver(this);
59 v8::Handle<v8::Context> context = isolate->GetCurrentContext(); 60 v8::Handle<v8::Context> context = isolate->GetCurrentContext();
60 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); 61 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr();
61 GetWrapper(isolate) 62 GetWrapper(isolate)
62 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback) 63 ->SetPrivate(context, GetHiddenPropertyName(isolate), callback)
63 .FromJust(); 64 .FromJust();
64 } 65 }
65 66
66 WaitingCallback::~WaitingCallback() { 67 WaitingCallback::~WaitingCallback() {
67 Cancel(); 68 Cancel();
68 } 69 }
69 70
70 // static 71 void WaitingCallback::ClearWaitFlag() {
71 void WaitingCallback::CallOnHandleReady(void* closure, MojoResult result) { 72 started_handle_watcher_ = false;
72 static_cast<WaitingCallback*>(closure)->OnHandleReady(result);
73 }
74
75 void WaitingCallback::ClearWaitId() {
76 wait_id_ = 0;
77 handle_wrapper_->RemoveCloseObserver(this); 73 handle_wrapper_->RemoveCloseObserver(this);
78 handle_wrapper_ = nullptr; 74 handle_wrapper_ = nullptr;
79 } 75 }
80 76
81 void WaitingCallback::OnHandleReady(MojoResult result) { 77 void WaitingCallback::OnHandleReady(MojoResult result) {
82 ClearWaitId(); 78 ClearWaitFlag();
83 CallCallback(result); 79 CallCallback(result);
84 } 80 }
85 81
86 void WaitingCallback::CallCallback(MojoResult result) { 82 void WaitingCallback::CallCallback(MojoResult result) {
87 // ClearWaitId must already have been called. 83 // ClearWaitFlag must already have been called.
88 DCHECK(!wait_id_); 84 DCHECK(!started_handle_watcher_);
89 DCHECK(!handle_wrapper_); 85 DCHECK(!handle_wrapper_);
90 86
91 if (!runner_) 87 if (!runner_)
92 return; 88 return;
93 89
94 gin::Runner::Scope scope(runner_.get()); 90 gin::Runner::Scope scope(runner_.get());
95 v8::Isolate* isolate = runner_->GetContextHolder()->isolate(); 91 v8::Isolate* isolate = runner_->GetContextHolder()->isolate();
96 92
97 v8::Handle<v8::Value> hidden_value = 93 v8::Handle<v8::Value> hidden_value =
98 GetWrapper(isolate) 94 GetWrapper(isolate)
99 ->GetPrivate(runner_->GetContextHolder()->context(), 95 ->GetPrivate(runner_->GetContextHolder()->context(),
100 GetHiddenPropertyName(isolate)) 96 GetHiddenPropertyName(isolate))
101 .ToLocalChecked(); 97 .ToLocalChecked();
102 v8::Handle<v8::Function> callback; 98 v8::Handle<v8::Function> callback;
103 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback)); 99 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback));
104 100
105 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) }; 101 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) };
106 runner_->Call(callback, runner_->global(), 1, args); 102 runner_->Call(callback, runner_->global(), 1, args);
107 } 103 }
108 104
109 void WaitingCallback::OnWillCloseHandle() { 105 void WaitingCallback::OnWillCloseHandle() {
110 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_); 106 handle_watcher_.Stop();
111 107
112 // This may be called from GC, so we can't execute Javascript now, call 108 // This may be called from GC, so we can't execute Javascript now, call
113 // ClearWaitId explicitly, and CallCallback asynchronously. 109 // ClearWaitFlag explicitly, and CallCallback asynchronously.
114 ClearWaitId(); 110 ClearWaitFlag();
115 base::MessageLoop::current()->PostTask( 111 base::MessageLoop::current()->PostTask(
116 FROM_HERE, 112 FROM_HERE,
117 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(), 113 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(),
118 MOJO_RESULT_INVALID_ARGUMENT)); 114 MOJO_RESULT_INVALID_ARGUMENT));
119 } 115 }
120 116
121 } // namespace js 117 } // namespace js
122 } // namespace edk 118 } // namespace edk
123 } // namespace mojo 119 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/js/waiting_callback.h ('k') | mojo/mojo_edk.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698