OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "content/browser/navigator_connect/navigator_connect_dispatcher_host.h" | 5 #include "content/browser/navigator_connect/navigator_connect_dispatcher_host.h" |
6 | 6 |
7 #include "content/browser/message_port_service.h" | 7 #include "content/browser/message_port_service.h" |
8 #include "content/browser/navigator_connect/navigator_connect_context.h" | 8 #include "content/browser/navigator_connect/navigator_connect_context_impl.h" |
9 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 9 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
10 #include "content/common/navigator_connect_messages.h" | 10 #include "content/common/navigator_connect_messages.h" |
11 #include "content/common/navigator_connect_types.h" | 11 #include "content/public/common/navigator_connect_client.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 NavigatorConnectDispatcherHost::NavigatorConnectDispatcherHost( | 15 NavigatorConnectDispatcherHost::NavigatorConnectDispatcherHost( |
16 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context, | 16 const scoped_refptr<NavigatorConnectContextImpl>& navigator_connect_context) |
17 const scoped_refptr<NavigatorConnectContext>& navigator_connect_context) | |
18 : BrowserMessageFilter(NavigatorConnectMsgStart), | 17 : BrowserMessageFilter(NavigatorConnectMsgStart), |
19 service_worker_context_(service_worker_context), | |
20 navigator_connect_context_(navigator_connect_context) { | 18 navigator_connect_context_(navigator_connect_context) { |
21 } | 19 } |
22 | 20 |
23 NavigatorConnectDispatcherHost::~NavigatorConnectDispatcherHost() { | 21 NavigatorConnectDispatcherHost::~NavigatorConnectDispatcherHost() { |
24 } | 22 } |
25 | 23 |
26 bool NavigatorConnectDispatcherHost::OnMessageReceived( | 24 bool NavigatorConnectDispatcherHost::OnMessageReceived( |
27 const IPC::Message& message) { | 25 const IPC::Message& message) { |
28 bool handled = true; | 26 bool handled = true; |
29 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectDispatcherHost, message) | 27 IPC_BEGIN_MESSAGE_MAP(NavigatorConnectDispatcherHost, message) |
30 IPC_MESSAGE_HANDLER(NavigatorConnectHostMsg_Connect, OnConnect) | 28 IPC_MESSAGE_HANDLER(NavigatorConnectHostMsg_Connect, OnConnect) |
31 IPC_MESSAGE_UNHANDLED(handled = false) | 29 IPC_MESSAGE_UNHANDLED(handled = false) |
32 IPC_END_MESSAGE_MAP() | 30 IPC_END_MESSAGE_MAP() |
33 return handled; | 31 return handled; |
34 } | 32 } |
35 | 33 |
36 void NavigatorConnectDispatcherHost::OnConnect( | 34 void NavigatorConnectDispatcherHost::OnConnect( |
37 int thread_id, | 35 int thread_id, |
38 int request_id, | 36 int request_id, |
39 const CrossOriginServiceWorkerClient& client) { | 37 const NavigatorConnectClient& client) { |
40 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 38 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
41 | 39 |
42 // Hold messages for port while setting up connection. | 40 navigator_connect_context_->Connect( |
43 MessagePortService::GetInstance()->HoldMessages(client.message_port_id); | 41 client, base::Bind(&NavigatorConnectDispatcherHost::OnConnectResult, this, |
44 | 42 thread_id, request_id)); |
45 // Find the right service worker to service this connection. | |
46 service_worker_context_->context()->storage()->FindRegistrationForDocument( | |
47 client.target_url, | |
48 base::Bind(&NavigatorConnectDispatcherHost::GotServiceWorkerRegistration, | |
49 this, thread_id, request_id, client)); | |
50 } | |
51 | |
52 void NavigatorConnectDispatcherHost::GotServiceWorkerRegistration( | |
53 int thread_id, | |
54 int request_id, | |
55 const CrossOriginServiceWorkerClient& client, | |
56 ServiceWorkerStatusCode status, | |
57 const scoped_refptr<ServiceWorkerRegistration>& registration) { | |
58 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
59 | |
60 if (status != SERVICE_WORKER_OK) { | |
61 // No service worker found, reject connection attempt. | |
62 OnConnectResult(thread_id, request_id, client, registration, status, false); | |
63 return; | |
64 } | |
65 | |
66 ServiceWorkerVersion* active_version = registration->active_version(); | |
67 if (!active_version) { | |
68 // No active version, reject connection attempt. | |
69 OnConnectResult(thread_id, request_id, client, registration, status, false); | |
70 return; | |
71 } | |
72 | |
73 active_version->DispatchCrossOriginConnectEvent( | |
74 base::Bind(&NavigatorConnectDispatcherHost::OnConnectResult, this, | |
75 thread_id, request_id, client, registration), | |
76 client); | |
77 } | 43 } |
78 | 44 |
79 void NavigatorConnectDispatcherHost::OnConnectResult( | 45 void NavigatorConnectDispatcherHost::OnConnectResult( |
80 int thread_id, | 46 int thread_id, |
81 int request_id, | 47 int request_id, |
82 const CrossOriginServiceWorkerClient& client, | |
83 const scoped_refptr<ServiceWorkerRegistration>& registration, | |
84 ServiceWorkerStatusCode status, | |
85 bool accept_connection) { | 48 bool accept_connection) { |
86 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
87 | 50 Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, |
88 if (status != SERVICE_WORKER_OK || !accept_connection) { | 51 accept_connection)); |
89 // Close port since connection failed. | |
90 MessagePortService::GetInstance()->ClosePort(client.message_port_id); | |
91 Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, false)); | |
92 return; | |
93 } | |
94 | |
95 // Register connection and post back result. | |
96 navigator_connect_context_->RegisterConnection(client, registration); | |
97 Send(new NavigatorConnectMsg_ConnectResult(thread_id, request_id, true)); | |
98 } | 52 } |
99 | 53 |
100 } // namespace content | 54 } // namespace content |
OLD | NEW |