OLD | NEW |
---|---|
(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 "config.h" | |
6 #include "modules/navigatorconnect/NavigatorConnect.h" | |
7 | |
8 #include "bindings/core/v8/ScriptPromise.h" | |
9 #include "bindings/core/v8/ScriptPromiseResolver.h" | |
10 #include "core/dom/DOMException.h" | |
11 #include "core/dom/ExceptionCode.h" | |
12 #include "core/dom/MessageChannel.h" | |
13 #include "core/dom/MessagePort.h" | |
14 #include "public/platform/Platform.h" | |
15 #include "public/platform/WebCallbacks.h" | |
16 #include "public/platform/WebNavigatorConnectProvider.h" | |
17 | |
18 namespace blink { | |
19 | |
20 namespace { | |
21 | |
22 class ConnectCallbacks : public WebCallbacks<void, void> { | |
23 public: | |
24 ConnectCallbacks(PassRefPtr<ScriptPromiseResolver> resolver, PassRefPtrWillB eRawPtr<MessagePort> port) | |
25 : m_resolver(resolver), m_port(port) | |
26 { | |
27 ASSERT(m_resolver); | |
28 } | |
29 ~ConnectCallbacks() override { } | |
30 | |
31 void onSuccess() override | |
32 { | |
33 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) { | |
34 return; | |
35 } | |
36 m_resolver->resolve(m_port); | |
37 } | |
38 | |
39 void onError() override | |
40 { | |
41 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) { | |
42 return; | |
43 } | |
44 m_resolver->reject(DOMException::create(AbortError)); | |
45 } | |
46 | |
47 private: | |
48 RefPtr<ScriptPromiseResolver> m_resolver; | |
49 RefPtrWillBeMember<MessagePort> m_port; | |
50 WTF_MAKE_NONCOPYABLE(ConnectCallbacks); | |
51 }; | |
52 | |
53 } // namespace | |
54 | |
55 ScriptPromise NavigatorConnect::connect(ScriptState* scriptState, Navigator&, co nst String& url) | |
56 { | |
57 WebNavigatorConnectProvider* provider = Platform::current()->navigatorConnec tProvider(); | |
58 if (!provider) | |
59 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(NotSupportedError)); | |
60 | |
61 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip tState); | |
62 ScriptPromise promise = resolver->promise(); | |
63 // Ownership of port1 of this channel is kept in the ConnectCallbacks and | |
64 // later passed to javascript (or released), the other port is disentangled | |
65 // and passed to the content layer to be transfered to the service provider. | |
66 // The MessageChannel object itself will be released at the end of this | |
67 // method, which is okay as it's only the ports within that matter. | |
scheib
2014/12/05 17:39:35
It is a bit indirect that ownership of port2 is tr
Marijn Kruisselbrink
2014/12/08 18:50:36
Done.
| |
68 RefPtrWillBeRawPtr<MessageChannel> channel(MessageChannel::create(scriptStat e->executionContext())); | |
69 OwnPtr<WebMessagePortChannel> webchannel = channel->port2()->disentangle(); | |
70 provider->connect( | |
71 scriptState->executionContext()->completeURL(url), | |
72 webchannel.leakPtr(), | |
73 new ConnectCallbacks(resolver, channel->port1())); | |
74 return promise; | |
75 } | |
76 | |
77 | |
78 } // namespace blink | |
OLD | NEW |