OLD | NEW |
1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 #ifndef CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_H_ | 5 #ifndef CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_H_ |
6 #define CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_H_ | 6 #define CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_H_ |
7 | 7 |
| 8 #include <memory> |
| 9 |
8 #include "base/id_map.h" | 10 #include "base/id_map.h" |
9 #include "base/optional.h" | 11 #include "base/optional.h" |
| 12 #include "base/time/time.h" |
10 #include "content/child/child_thread_impl.h" | 13 #include "content/child/child_thread_impl.h" |
| 14 #include "content/child/scoped_child_process_reference.h" |
11 #include "content/common/service_worker/embedded_worker.mojom.h" | 15 #include "content/common/service_worker/embedded_worker.mojom.h" |
12 #include "content/renderer/service_worker/embedded_worker_dispatcher.h" | |
13 #include "mojo/public/cpp/bindings/binding.h" | 16 #include "mojo/public/cpp/bindings/binding.h" |
14 #include "mojo/public/cpp/bindings/interface_ptr.h" | 17 |
| 18 namespace blink { |
| 19 |
| 20 class WebEmbeddedWorker; |
| 21 |
| 22 } // namespace blink |
15 | 23 |
16 namespace content { | 24 namespace content { |
17 | 25 |
| 26 class EmbeddedWorkerDevToolsAgent; |
| 27 class ServiceWorkerContextClient; |
| 28 |
18 // This class exposes interfaces of WebEmbeddedWorker to the browser process. | 29 // This class exposes interfaces of WebEmbeddedWorker to the browser process. |
19 // Unless otherwise noted, all methods should be called on the main thread. | 30 // Unless otherwise noted, all methods should be called on the main thread. |
20 class EmbeddedWorkerInstanceClientImpl | 31 class EmbeddedWorkerInstanceClientImpl |
21 : public mojom::EmbeddedWorkerInstanceClient { | 32 : public mojom::EmbeddedWorkerInstanceClient { |
22 public: | 33 public: |
23 static void Create( | 34 static void Create( |
24 EmbeddedWorkerDispatcher* dispatcher, | |
25 mojo::InterfaceRequest<mojom::EmbeddedWorkerInstanceClient> request); | 35 mojo::InterfaceRequest<mojom::EmbeddedWorkerInstanceClient> request); |
26 | 36 |
27 ~EmbeddedWorkerInstanceClientImpl() override; | 37 ~EmbeddedWorkerInstanceClientImpl() override; |
28 | 38 |
29 // Called from ServiceWorkerContextClient. | 39 // Called from ServiceWorkerContextClient. |
30 void StopWorkerCompleted(); | 40 void WorkerContextDestroyed(); |
31 EmbeddedWorkerDevToolsAgent* devtools_agent() { | 41 EmbeddedWorkerDevToolsAgent* devtools_agent() { |
32 return wrapper_->devtools_agent(); | 42 return wrapper_->devtools_agent(); |
33 }; | 43 }; |
34 | 44 |
35 private: | 45 private: |
| 46 // A thin wrapper of WebEmbeddedWorker which also adds and releases process |
| 47 // references automatically. |
| 48 class WorkerWrapper { |
| 49 public: |
| 50 WorkerWrapper(blink::WebEmbeddedWorker* worker, |
| 51 int devtools_agent_route_id); |
| 52 ~WorkerWrapper(); |
| 53 |
| 54 blink::WebEmbeddedWorker* worker() { return worker_.get(); } |
| 55 EmbeddedWorkerDevToolsAgent* devtools_agent() { |
| 56 return devtools_agent_.get(); |
| 57 } |
| 58 |
| 59 private: |
| 60 ScopedChildProcessReference process_ref_; |
| 61 std::unique_ptr<blink::WebEmbeddedWorker> worker_; |
| 62 std::unique_ptr<EmbeddedWorkerDevToolsAgent> devtools_agent_; |
| 63 }; |
| 64 |
36 EmbeddedWorkerInstanceClientImpl( | 65 EmbeddedWorkerInstanceClientImpl( |
37 EmbeddedWorkerDispatcher* dispatcher, | |
38 mojo::InterfaceRequest<mojom::EmbeddedWorkerInstanceClient> request); | 66 mojo::InterfaceRequest<mojom::EmbeddedWorkerInstanceClient> request); |
39 | 67 |
40 // mojom::EmbeddedWorkerInstanceClient implementation | 68 // mojom::EmbeddedWorkerInstanceClient implementation |
41 void StartWorker( | 69 void StartWorker( |
42 const EmbeddedWorkerStartParams& params, | 70 const EmbeddedWorkerStartParams& params, |
43 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request) override; | 71 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, |
44 void StopWorker(const StopWorkerCallback& callback) override; | 72 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo instance_host) |
| 73 override; |
| 74 void StopWorker() override; |
45 void ResumeAfterDownload() override; | 75 void ResumeAfterDownload() override; |
46 void AddMessageToConsole(blink::WebConsoleMessage::Level level, | 76 void AddMessageToConsole(blink::WebConsoleMessage::Level level, |
47 const std::string& message) override; | 77 const std::string& message) override; |
48 | 78 |
49 // Handler of connection error bound to |binding_| | 79 // Handler of connection error bound to |binding_| |
50 void OnError(); | 80 void OnError(); |
51 | 81 |
52 EmbeddedWorkerDispatcher* dispatcher_; | 82 std::unique_ptr<WorkerWrapper> StartWorkerContext( |
| 83 const EmbeddedWorkerStartParams& params, |
| 84 std::unique_ptr<ServiceWorkerContextClient> context_client); |
| 85 |
53 mojo::Binding<mojom::EmbeddedWorkerInstanceClient> binding_; | 86 mojo::Binding<mojom::EmbeddedWorkerInstanceClient> binding_; |
54 | 87 |
55 // This is valid before StartWorker is called. After that, this object | 88 // This is valid before StartWorker is called. After that, this object |
56 // will be passed to ServiceWorkerContextClient. | 89 // will be passed to ServiceWorkerContextClient. |
57 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> temporal_self_; | 90 std::unique_ptr<EmbeddedWorkerInstanceClientImpl> temporal_self_; |
58 | 91 |
59 base::Optional<int> embedded_worker_id_; | 92 // nullptr means the worker is not running. |
| 93 std::unique_ptr<WorkerWrapper> wrapper_; |
60 | 94 |
61 // Owned by EmbeddedWorkerDispatcher. This can be nullptr while a worker is | 95 base::Optional<base::TimeTicks> stop_worker_time_; |
62 // not running. | |
63 EmbeddedWorkerDispatcher::WorkerWrapper* wrapper_; | |
64 | |
65 StopWorkerCallback stop_callback_; | |
66 | 96 |
67 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceClientImpl); | 97 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstanceClientImpl); |
68 }; | 98 }; |
69 | 99 |
70 } // namespace content | 100 } // namespace content |
71 | 101 |
72 #endif // CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_
H_ | 102 #endif // CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_CLIENT_IMPL_
H_ |
OLD | NEW |