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

Side by Side Diff: content/browser/navigator_connect/navigator_connect_context.cc

Issue 861373002: Refactor navigator.connect code to make it more flexible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix typo Created 5 years, 10 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 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 "content/browser/navigator_connect/navigator_connect_context.h"
6
7 #include "content/browser/message_port_service.h"
8 #include "content/browser/service_worker/service_worker_context_wrapper.h"
9 #include "content/browser/service_worker/service_worker_utils.h"
10 #include "content/common/navigator_connect_types.h"
11
12 namespace content {
13
14 struct NavigatorConnectContext::Connection {
15 CrossOriginServiceWorkerClient client;
16 int64 service_worker_registration_id;
17 GURL service_worker_registration_origin;
18 };
19
20 NavigatorConnectContext::NavigatorConnectContext(
21 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
22 : service_worker_context_(service_worker_context) {
23 }
24
25 NavigatorConnectContext::~NavigatorConnectContext() {
26 }
27
28 void NavigatorConnectContext::RegisterConnection(
29 const CrossOriginServiceWorkerClient& client,
30 const scoped_refptr<ServiceWorkerRegistration>&
31 service_worker_registration) {
32 MessagePortService::GetInstance()->UpdateMessagePort(
33 client.message_port_id, this, client.message_port_id);
34 MessagePortService::GetInstance()->ReleaseMessages(client.message_port_id);
35 Connection& connection = connections_[client.message_port_id];
36 connection.client = client;
37 connection.service_worker_registration_id = service_worker_registration->id();
38 connection.service_worker_registration_origin =
39 service_worker_registration->pattern().GetOrigin();
40 }
41
42 void NavigatorConnectContext::SendMessage(
43 int route_id,
44 const base::string16& message,
45 const std::vector<int>& sent_message_port_ids) {
46 DCHECK(connections_.find(route_id) != connections_.end());
47 const Connection& connection = connections_[route_id];
48
49 // Hold messages while service worker is found, activated, and message sent
50 // causing ServiceWorkerScriptContext::OnCrossOriginMessageToWorker to
51 // construct WebMessagePortChannelImpl instances which send
52 // MessagePortHostMsg_ReleaseMessages.
53 for (int sent_message_port_id : sent_message_port_ids)
54 MessagePortService::GetInstance()->HoldMessages(sent_message_port_id);
55
56 service_worker_context_->context()->storage()->FindRegistrationForId(
57 connection.service_worker_registration_id,
58 connection.service_worker_registration_origin,
59 base::Bind(&NavigatorConnectContext::DoSendMessage, this,
60 connection.client, message, sent_message_port_ids));
61 }
62
63 void NavigatorConnectContext::SendMessagesAreQueued(int route_id) {
64 NOTREACHED() << "navigator.connect endpoints should never queue messages.";
65 }
66
67 void NavigatorConnectContext::DoSendMessage(
68 const CrossOriginServiceWorkerClient& client,
69 const base::string16& message,
70 const std::vector<int>& sent_message_port_ids,
71 ServiceWorkerStatusCode service_worker_status,
72 const scoped_refptr<ServiceWorkerRegistration>&
73 service_worker_registration) {
74 if (service_worker_status != SERVICE_WORKER_OK) {
75 // TODO(mek): Do something when no service worker was found.
76 return;
77 }
78
79 ServiceWorkerVersion* active_version =
80 service_worker_registration->active_version();
81 if (!active_version) {
82 // TODO(mek): Do something when no active version exists.
83 return;
84 }
85
86 active_version->DispatchCrossOriginMessageEvent(
87 client, message, sent_message_port_ids,
88 base::Bind(&ServiceWorkerUtils::NoOpStatusCallback));
89 }
90
91 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698