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

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: improve 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
(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 // TODO(mek): Deserializing this every time it is accessed is a bit wasteful ,
45 // investigate caching this similar to how MessageEvent caches data.
haraken 2015/06/24 23:59:59 If this really becomes a performance problem, we c
Marijn Kruisselbrink 2015/06/25 01:07:26 Ah, okay, I'll just remove that comment for now th
46 return ScriptValue(scriptState, m_serializedData->deserialize(scriptState->i solate()));
47 }
48
49 void ServicePort::postMessage(ExecutionContext* executionContext, PassRefPtr<Ser ializedScriptValue> message, const MessagePortArray* ports, ExceptionState& exce ptionState)
50 {
51 OwnPtr<MessagePortChannelArray> channels;
52 if (ports) {
53 channels = MessagePort::disentanglePorts(executionContext, ports, except ionState);
54 if (exceptionState.hadException())
55 return;
56 }
57
58 WebString messageString = message->toWireString();
59 OwnPtr<WebMessagePortChannelArray> webChannels = MessagePort::toWebMessagePo rtChannelArray(channels.release());
60 if (m_collection) {
61 WebServicePortProvider* provider = m_collection->provider();
62 provider->postMessage(m_port.id, messageString, webChannels.leakPtr());
63 }
64 }
65
66 void ServicePort::close()
67 {
68 // TODO(mek): Figure out if this should throw instead of just quietly fail.
69 if (!m_isOpen)
70 return;
71
72 if (m_collection) {
73 m_collection->closePort(this);
74 m_collection = nullptr;
75 }
76 m_isOpen = false;
77 }
78
79 ServicePort::ServicePort(WeakPtrWillBeRawPtr<ServicePortCollection> collection, const WebServicePort& port)
80 : m_isOpen(true), m_port(port), m_collection(collection)
81 {
82 if (!m_port.data.isEmpty()) {
83 m_serializedData = SerializedScriptValueFactory::instance().createFromWi re(m_port.data);
84 }
85 }
86
87 } // namespace blink
88
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698