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

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

Issue 814543006: Move //mojo/{public, edk} underneath //third_party (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 11 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/edk/mojo_edk.gni » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "mojo/edk/js/waiting_callback.h"
6
7 #include "base/bind.h"
8 #include "base/message_loop/message_loop.h"
9 #include "gin/per_context_data.h"
10 #include "mojo/public/cpp/environment/environment.h"
11
12 namespace mojo {
13 namespace js {
14
15 namespace {
16
17 v8::Handle<v8::String> GetHiddenPropertyName(v8::Isolate* isolate) {
18 return gin::StringToSymbol(isolate, "::mojo::js::WaitingCallback");
19 }
20
21 } // namespace
22
23 gin::WrapperInfo WaitingCallback::kWrapperInfo = { gin::kEmbedderNativeGin };
24
25 // static
26 gin::Handle<WaitingCallback> WaitingCallback::Create(
27 v8::Isolate* isolate,
28 v8::Handle<v8::Function> callback,
29 gin::Handle<HandleWrapper> handle_wrapper,
30 MojoHandleSignals signals) {
31 gin::Handle<WaitingCallback> waiting_callback = gin::CreateHandle(
32 isolate, new WaitingCallback(isolate, callback, handle_wrapper));
33 waiting_callback->wait_id_ = Environment::GetDefaultAsyncWaiter()->AsyncWait(
34 handle_wrapper->get().value(),
35 signals,
36 MOJO_DEADLINE_INDEFINITE,
37 &WaitingCallback::CallOnHandleReady,
38 waiting_callback.get());
39 return waiting_callback;
40 }
41
42 void WaitingCallback::Cancel() {
43 if (!wait_id_)
44 return;
45
46 handle_wrapper_->RemoveCloseObserver(this);
47 handle_wrapper_ = NULL;
48 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_);
49 wait_id_ = 0;
50 }
51
52 WaitingCallback::WaitingCallback(v8::Isolate* isolate,
53 v8::Handle<v8::Function> callback,
54 gin::Handle<HandleWrapper> handle_wrapper)
55 : wait_id_(0), handle_wrapper_(handle_wrapper.get()), weak_factory_(this) {
56 handle_wrapper_->AddCloseObserver(this);
57 v8::Handle<v8::Context> context = isolate->GetCurrentContext();
58 runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr();
59 GetWrapper(isolate)->SetHiddenValue(GetHiddenPropertyName(isolate), callback);
60 }
61
62 WaitingCallback::~WaitingCallback() {
63 Cancel();
64 }
65
66 // static
67 void WaitingCallback::CallOnHandleReady(void* closure, MojoResult result) {
68 static_cast<WaitingCallback*>(closure)->OnHandleReady(result);
69 }
70
71 void WaitingCallback::ClearWaitId() {
72 wait_id_ = 0;
73 handle_wrapper_->RemoveCloseObserver(this);
74 handle_wrapper_ = nullptr;
75 }
76
77 void WaitingCallback::OnHandleReady(MojoResult result) {
78 ClearWaitId();
79 CallCallback(result);
80 }
81
82 void WaitingCallback::CallCallback(MojoResult result) {
83 // ClearWaitId must already have been called.
84 DCHECK(!wait_id_);
85 DCHECK(!handle_wrapper_);
86
87 if (!runner_)
88 return;
89
90 gin::Runner::Scope scope(runner_.get());
91 v8::Isolate* isolate = runner_->GetContextHolder()->isolate();
92
93 v8::Handle<v8::Value> hidden_value =
94 GetWrapper(isolate)->GetHiddenValue(GetHiddenPropertyName(isolate));
95 v8::Handle<v8::Function> callback;
96 CHECK(gin::ConvertFromV8(isolate, hidden_value, &callback));
97
98 v8::Handle<v8::Value> args[] = { gin::ConvertToV8(isolate, result) };
99 runner_->Call(callback, runner_->global(), 1, args);
100 }
101
102 void WaitingCallback::OnWillCloseHandle() {
103 Environment::GetDefaultAsyncWaiter()->CancelWait(wait_id_);
104
105 // This may be called from GC, so we can't execute Javascript now, call
106 // ClearWaitId explicitly, and CallCallback asynchronously.
107 ClearWaitId();
108 base::MessageLoop::current()->PostTask(
109 FROM_HERE,
110 base::Bind(&WaitingCallback::CallCallback, weak_factory_.GetWeakPtr(),
111 MOJO_RESULT_INVALID_ARGUMENT));
112 }
113
114 } // namespace js
115 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/edk/js/waiting_callback.h ('k') | mojo/edk/mojo_edk.gni » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698