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

Side by Side Diff: Source/modules/navigatorconnect/ServicePort.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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "config.h"
6 #include "modules/navigatorconnect/ServicePort.h"
7
8 #include "bindings/core/v8/ScriptValue.h"
9 #include "bindings/core/v8/SerializedScriptValueFactory.h"
10 #include "core/dom/MessagePort.h"
11 #include "modules/navigatorconnect/ServicePortCollection.h"
12 #include "public/platform/Platform.h"
13 #include "public/platform/modules/navigator_services/WebServicePortProvider.h"
14
15 namespace blink {
16
17 ServicePort* ServicePort::create(WeakPtrWillBeRawPtr<ServicePortCollection> coll ection, const WebServicePort& port)
18 {
19 return new ServicePort(collection, port);
20 }
21
22 ServicePort::~ServicePort()
23 {
24 if (m_isOpen && m_collection) {
25 m_collection->closePort(this);
26 }
27 }
28
29 String ServicePort::targetURL() const
30 {
31 return m_port.targetUrl.string();
32 }
33
34 String ServicePort::name() const
35 {
36 return m_port.name;
37 }
38
39 ScriptValue ServicePort::data(ScriptState* scriptState) const
40 {
41 if (!m_serializedData)
42 return ScriptValue::createNull(scriptState);
43
44 return ScriptValue(scriptState, m_serializedData->deserialize(scriptState->i solate()));
45 }
46
47 void ServicePort::postMessage(ExecutionContext* executionContext, PassRefPtr<Ser ializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exce ptionState)
48 {
49 OwnPtr<MessagePortChannelArray> channels;
50 if (ports) {
51 channels = MessagePort::disentanglePorts(executionContext, ports, except ionState);
52 if (exceptionState.hadException())
53 return;
54 }
55
56 WebString messageString = message->toWireString();
57 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo rtChannelArray(channels.release());
58 if (m_collection) {
59 WebServicePortProvider* provider = m_collection->provider();
60 provider->postMessage(m_port.id, messageString, webChannels.leakPtr());
61 }
62 }
63
64 void ServicePort::close()
65 {
66 // TODO(mek): Figure out if this should throw instead of just quietly fail.
67 if (!m_isOpen)
68 return;
69
70 if (m_collection) {
71 m_collection->closePort(this);
72 m_collection = nullptr;
73 }
74 m_isOpen = false;
75 }
76
77 ServicePort::ServicePort(WeakPtrWillBeRawPtr<ServicePortCollection> collection, const WebServicePort& port)
78 : m_isOpen(true), m_port(port), m_collection(collection)
79 {
80 if (!m_port.data.isEmpty()) {
81 m_serializedData = SerializedScriptValueFactory::instance().createFromWi re(m_port.data);
scheib 2015/06/24 04:16:55 Just checking, but there can be different contexts
Marijn Kruisselbrink 2015/06/24 17:03:27 Great question, I should probably add a comment, a
haraken 2015/06/24 23:50:31 Yes, your understanding is correct. Given that we
82 }
83 }
84
85 } // namespace blink
86
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698