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

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: address more comments 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
14 PassRefPtrWillBeRawPtr<ServicePortCollection> ServicePortCollection::create(Exec utionContext* context) 22 namespace {
23
24 class ConnectCallbacks : public WebServicePortConnectCallbacks {
25 public:
26 ConnectCallbacks(PassRefPtrWillBeRawPtr<ScriptPromiseResolver> resolver, Ser vicePortCollection* collection, const KURL& targetUrl, const String& portName, c onst 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 ServicePort* port = ServicePort::create(m_collection, webPort);
46 m_collection->addPort(port);
47 m_resolver->resolve(port);
48 }
49
50 void onError() override
51 {
52 // TODO(mek): Pass actual error code back.
53 if (!m_resolver->executionContext() || m_resolver->executionContext()->a ctiveDOMObjectsAreStopped()) {
54 return;
55 }
56 m_resolver->reject(DOMException::create(AbortError));
57 }
58
59 private:
60 RefPtrWillBePersistent<ScriptPromiseResolver> m_resolver;
61 Persistent<ServicePortCollection> m_collection;
62 KURL m_targetUrl;
63 String m_portName;
64 String m_serializedPortData;
65
66 WTF_MAKE_NONCOPYABLE(ConnectCallbacks);
67 };
68
69 } // namespace
70
71 ServicePortCollection* ServicePortCollection::create(ExecutionContext* context)
15 { 72 {
16 return adoptRefWillBeNoop(new ServicePortCollection(context)); 73 return new ServicePortCollection(context);
17 } 74 }
18 75
19 ServicePortCollection::~ServicePortCollection() 76 ServicePortCollection::~ServicePortCollection()
20 { 77 {
21 } 78 }
22 79
23 ScriptPromise ServicePortCollection::connect(ScriptState* scriptState, const Str ing& url, const ServicePortConnectOptions& options) 80 void ServicePortCollection::addPort(ServicePort* port)
24 { 81 {
25 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 82 m_ports.append(port);
83 }
84
85 void ServicePortCollection::closePort(ServicePort* port)
86 {
87 m_ports.remove(m_ports.find(port));
88 if (m_provider)
89 m_provider->closePort(port->id());
90 }
91
92 ScriptPromise ServicePortCollection::connect(ScriptState* scriptState, const Str ing& url, const ServicePortConnectOptions& options, ExceptionState& exceptionSta te)
93 {
94 if (!m_provider)
95 return ScriptPromise::rejectWithDOMException(scriptState, DOMException:: create(NotSupportedError));
96
97 RefPtr<SerializedScriptValue> portData;
98 if (options.hasData()) {
99 portData = SerializedScriptValueFactory::instance().create(options.data( ).isolate(), options.data(), nullptr, exceptionState);
100 if (exceptionState.hadException())
101 return exceptionState.reject(scriptState);
102 }
103 RefPtrWillBeRawPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver:: create(scriptState);
104 ScriptPromise promise = resolver->promise();
105 KURL targetUrl = scriptState->executionContext()->completeURL(url);
106 m_provider->connect(
107 targetUrl,
108 scriptState->executionContext()->securityOrigin()->toString(),
109 new ConnectCallbacks(resolver, this, targetUrl, options.name(), portData ? portData->toWireString() : String()));
110 return promise;
26 } 111 }
27 112
28 ScriptPromise ServicePortCollection::match(ScriptState* scriptState, const Servi cePortMatchOptions& options) 113 ScriptPromise ServicePortCollection::match(ScriptState* scriptState, const Servi cePortMatchOptions& options)
29 { 114 {
30 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 115 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError));
31 } 116 }
32 117
33 ScriptPromise ServicePortCollection::matchAll(ScriptState* scriptState, const Se rvicePortMatchOptions& options) 118 ScriptPromise ServicePortCollection::matchAll(ScriptState* scriptState, const Se rvicePortMatchOptions& options)
34 { 119 {
35 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError)); 120 return ScriptPromise::rejectWithDOMException(scriptState, DOMException::crea te(NotSupportedError));
36 } 121 }
37 122
38 const AtomicString& ServicePortCollection::interfaceName() const 123 const AtomicString& ServicePortCollection::interfaceName() const
39 { 124 {
40 return EventTargetNames::ServicePortCollection; 125 return EventTargetNames::ServicePortCollection;
41 } 126 }
42 127
43 ExecutionContext* ServicePortCollection::executionContext() const 128 ExecutionContext* ServicePortCollection::executionContext() const
44 { 129 {
45 return ContextLifecycleObserver::executionContext(); 130 return ContextLifecycleObserver::executionContext();
46 } 131 }
47 132
133 void ServicePortCollection::postMessage(WebServicePortID portId, const WebString & messageString, const WebMessagePortChannelArray& webChannels)
134 {
135 OwnPtr<MessagePortChannelArray> channels;
136 if (webChannels.size()) {
137 channels = adoptPtr(new MessagePortChannelArray(webChannels.size()));
138 for (size_t i = 0; i < webChannels.size(); ++i)
139 (*channels)[i] = adoptPtr(webChannels[i]);
140 }
141 RefPtr<SerializedScriptValue> message = SerializedScriptValueFactory::instan ce().createFromWire(messageString);
142
143 OwnPtrWillBeRawPtr<MessagePortArray> ports = MessagePort::entanglePorts(*exe cutionContext(), channels.release());
144 RefPtrWillBeRawPtr<Event> evt = MessageEvent::create(ports.release(), messag e.release());
145 // TODO(mek): Lookup ServicePort and set events source attribute.
146 dispatchEvent(evt.release(), ASSERT_NO_EXCEPTION);
147 }
148
48 DEFINE_TRACE(ServicePortCollection) 149 DEFINE_TRACE(ServicePortCollection)
49 { 150 {
151 visitor->trace(m_ports);
50 EventTargetWithInlineData::trace(visitor); 152 EventTargetWithInlineData::trace(visitor);
51 ContextLifecycleObserver::trace(visitor); 153 ContextLifecycleObserver::trace(visitor);
52 } 154 }
53 155
54 ServicePortCollection::ServicePortCollection(ExecutionContext* context) 156 ServicePortCollection::ServicePortCollection(ExecutionContext* context)
55 : ContextLifecycleObserver(context) 157 : ContextLifecycleObserver(context)
158 , m_provider(adoptPtr(Platform::current()->createServicePortProvider(this)))
56 { 159 {
57 } 160 }
58 161
59 } // namespace blink 162 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/navigatorconnect/ServicePortCollection.h ('k') | Source/modules/navigatorconnect/ServicePortCollection.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698