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

Side by Side Diff: Source/modules/navigatorconnect/ServicePortCollection.cpp

Issue 1191393003: Update client side navigator.connect API to use ServicePortCollection [1/3] (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@serviceport
Patch Set: Created 5 years, 6 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
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "config.h" 5 #include "config.h"
6 #include "modules/navigatorconnect/ServicePortCollection.h" 6 #include "modules/navigatorconnect/ServicePortCollection.h"
7 7
8 #include "bindings/core/v8/ScriptPromiseResolver.h"
9 #include "bindings/core/v8/SerializedScriptValueFactory.h"
8 #include "core/dom/DOMException.h" 10 #include "core/dom/DOMException.h"
9 #include "core/dom/ExceptionCode.h" 11 #include "core/dom/ExceptionCode.h"
12 #include "core/dom/MessagePort.h"
13 #include "core/events/MessageEvent.h"
10 #include "modules/EventTargetModules.h" 14 #include "modules/EventTargetModules.h"
15 #include "modules/navigatorconnect/ServicePort.h"
16 #include "modules/navigatorconnect/ServicePortConnectOptions.h"
17 #include "public/platform/Platform.h"
18 #include "public/platform/modules/navigator_services/WebServicePortProvider.h"
11 19
12 namespace blink { 20 namespace blink {
13 21
22 namespace {
23
24 class ConnectCallbacks : public WebServicePortConnectCallbacks {
25 public:
26 ConnectCallbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver, Pas sRefPtrWillBeRawPtr<ServicePortCollection> collection, const KURL& targetUrl, co nst String& portName, const String& serializedPortData)
27 : m_resolver(resolver), m_collection(collection), m_targetUrl(targetUrl) , m_portName(portName), m_serializedPortData(serializedPortData)
28 {
29 ASSERT(m_resolver);
30 }
31
32 ~ConnectCallbacks() override { }
33
34 void onSuccess(WebServicePortID* portIdRaw) override
35 {
36 OwnPtr<WebServicePortID> webPortId = adoptPtr(portIdRaw);
37 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) {
38 return;
39 }
40 WebServicePort webPort;
41 webPort.id = *webPortId;
42 webPort.targetUrl = m_targetUrl;
43 webPort.name = m_portName;
44 webPort.data = m_serializedPortData;
45 #if !ENABLE(OILPAN)
46 ServicePort* port = ServicePort::create(m_collection->weakPtr(), webPort );
47 #else
48 ServicePort* port = ServicePort::create(m_collection, webPort);
49 #endif
50 m_collection->addPort(port);
51 m_resolver->resolve(port);
52 }
53
54 void onError() override
55 {
56 // TODO(mek): Pass actual error code back.
57 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) {
58 return;
59 }
60 m_resolver->reject(DOMException::create(AbortError));
61 }
62
63 private:
64 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver;
65 RefPtrWillBePersistent<ServicePortCollection> m_collection;
66 KURL m_targetUrl;
67 String m_portName;
68 String m_serializedPortData;
69
70 WTF_MAKE_NONCOPYABLE(ConnectCallbacks);
71 };
72
73 } // namespace
74
14 PassRefPtrWillBeRawPtr<ServicePortCollection> ServicePortCollection::create(Exec utionContext* context) 75 PassRefPtrWillBeRawPtr<ServicePortCollection> ServicePortCollection::create(Exec utionContext* context)
15 { 76 {
16 return adoptRefWillBeNoop(new ServicePortCollection(context)); 77 return adoptRefWillBeNoop(new ServicePortCollection(context));
17 } 78 }
18 79
19 ServicePortCollection::ServicePortCollection(ExecutionContext* context)
20 : ContextLifecycleObserver(context)
21 {
22 }
23
24 ServicePortCollection::~ServicePortCollection() 80 ServicePortCollection::~ServicePortCollection()
25 { 81 {
26 } 82 }
27 83
28 ScriptPromise ServicePortCollection::connect(ScriptState* scriptState, const Str ing& url, const ServicePortConnectOptions& options) 84 void ServicePortCollection::addPort(ServicePort* port)
29 { 85 {
30 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 86 m_ports.append(port);
87 }
88
89 void ServicePortCollection::closePort(ServicePort* port)
90 {
91 m_ports.remove(m_ports.find(port));
92 m_provider->closePort(port->id());
93 }
94
95 ScriptPromise ServicePortCollection::connect(ScriptState* scriptState, const Str ing& url, const ServicePortConnectOptions& options, ExceptionState& exceptionSta te)
96 {
97 if (!m_provider)
98 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(NotSupportedError));
99
100 RefPtr<SerializedScriptValue> portData;
101 if (options.hasData()) {
102 portData = SerializedScriptValueFactory::instance().create(options.data( ).isolate(), options.data(), nullptr, exceptionState);
103 if (exceptionState.hadException())
104 return exceptionState.reject(scriptState);
105 }
106 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
107 ScriptPromise promise = resolver->promise();
108 KURL targetUrl = scriptState->executionContext()->completeURL(url);
109 m_provider->connect(
110 targetUrl,
111 scriptState->executionContext()->securityOrigin()->toString(),
112 new ConnectCallbacks(resolver, this, targetUrl, options.name(), portData ? portData->toWireString() : String()));
113 return promise;
31 } 114 }
32 115
33 ScriptPromise ServicePortCollection::match(ScriptState* scriptState, const Servi cePortMatchOptions& options) 116 ScriptPromise ServicePortCollection::match(ScriptState* scriptState, const Servi cePortMatchOptions& options)
34 { 117 {
35 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 118 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError));
36 } 119 }
37 120
38 ScriptPromise ServicePortCollection::matchAll(ScriptState* scriptState, const Se rvicePortMatchOptions& options) 121 ScriptPromise ServicePortCollection::matchAll(ScriptState* scriptState, const Se rvicePortMatchOptions& options)
39 { 122 {
40 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 123 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError));
41 } 124 }
42 125
43 const AtomicString& ServicePortCollection::interfaceName() const 126 const AtomicString& ServicePortCollection::interfaceName() const
44 { 127 {
45 return EventTargetNames::ServicePortCollection; 128 return EventTargetNames::ServicePortCollection;
46 } 129 }
47 130
48 ExecutionContext* ServicePortCollection::executionContext() const 131 ExecutionContext* ServicePortCollection::executionContext() const
49 { 132 {
50 return ContextLifecycleObserver::executionContext(); 133 return ContextLifecycleObserver::executionContext();
51 } 134 }
52 135
136 void ServicePortCollection::postMessage(WebServicePortID portId, const WebString & messageString, const WebMessagePortChannelArray& webChannels)
137 {
138 OwnPtr<MessagePortChannelArray> channels;
139 if (webChannels.size()) {
140 channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
141 for (size_t i = 0; i < webChannels.size(); ++i)
142 (*channels)[i] = adoptPtr(webChannels[i]);
143 }
144 RefPtr<SerializedScriptValue> message = SerializedScriptValueFactory::instan ce().createFromWire(messageString);
145
146 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts(*exe cutionContext(), channels.release());
147 RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), messag e.release());
148 // TODO(mek): Lookup ServicePort and set events source attribute.
149 dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION);
150 }
151
53 DEFINE_TRACE(ServicePortCollection) 152 DEFINE_TRACE(ServicePortCollection)
54 { 153 {
55 EventTargetWithInlineData::trace(visitor); 154 EventTargetWithInlineData::trace(visitor);
56 ContextLifecycleObserver::trace(visitor); 155 ContextLifecycleObserver::trace(visitor);
57 } 156 }
58 157
158 ServicePortCollection::ServicePortCollection(ExecutionContext* context)
159 : ContextLifecycleObserver(context)
160 , m_provider(adoptPtr(Platform::current()->createServicePortProvider(this)))
161 #if !ENABLE(OILPAN)
162 , m_weakFactory(this)
163 #endif
164 {
165 }
166
59 } // namespace blink 167 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698