OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/id_map.h" |
13 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
15 #include "content/browser/service_worker/embedded_worker_instance.h" | 16 #include "content/browser/service_worker/embedded_worker_instance.h" |
16 #include "content/common/content_export.h" | 17 #include "content/common/content_export.h" |
17 #include "content/common/service_worker/service_worker_status_code.h" | 18 #include "content/common/service_worker/service_worker_status_code.h" |
18 | 19 |
19 class GURL; | 20 class GURL; |
20 | 21 |
21 namespace content { | 22 namespace content { |
22 | 23 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // | 56 // |
56 // And finally, is_shutdown_ is detects the live-ness of the object | 57 // And finally, is_shutdown_ is detects the live-ness of the object |
57 // itself. If the object is shut down, then it is in the process of | 58 // itself. If the object is shut down, then it is in the process of |
58 // being deleted from memory. This happens when a version is replaced | 59 // being deleted from memory. This happens when a version is replaced |
59 // as well as at browser shutdown. | 60 // as well as at browser shutdown. |
60 class CONTENT_EXPORT ServiceWorkerVersion | 61 class CONTENT_EXPORT ServiceWorkerVersion |
61 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), | 62 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), |
62 public EmbeddedWorkerInstance::Observer { | 63 public EmbeddedWorkerInstance::Observer { |
63 public: | 64 public: |
64 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 65 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
| 66 typedef base::Callback<void(ServiceWorkerStatusCode, |
| 67 const IPC::Message& message)> MessageCallback; |
65 | 68 |
66 enum Status { | 69 enum Status { |
67 STOPPED = EmbeddedWorkerInstance::STOPPED, | 70 STOPPED = EmbeddedWorkerInstance::STOPPED, |
68 STARTING = EmbeddedWorkerInstance::STARTING, | 71 STARTING = EmbeddedWorkerInstance::STARTING, |
69 RUNNING = EmbeddedWorkerInstance::RUNNING, | 72 RUNNING = EmbeddedWorkerInstance::RUNNING, |
70 STOPPING = EmbeddedWorkerInstance::STOPPING, | 73 STOPPING = EmbeddedWorkerInstance::STOPPING, |
71 }; | 74 }; |
72 | 75 |
73 ServiceWorkerVersion( | 76 ServiceWorkerVersion( |
74 ServiceWorkerRegistration* registration, | 77 ServiceWorkerRegistration* registration, |
(...skipping 10 matching lines...) Expand all Loading... |
85 } | 88 } |
86 | 89 |
87 // Starts an embedded worker for this version. | 90 // Starts an embedded worker for this version. |
88 // This returns OK (success) if the worker is already running. | 91 // This returns OK (success) if the worker is already running. |
89 void StartWorker(const StatusCallback& callback); | 92 void StartWorker(const StatusCallback& callback); |
90 | 93 |
91 // Starts an embedded worker for this version. | 94 // Starts an embedded worker for this version. |
92 // This returns OK (success) if the worker is already stopped. | 95 // This returns OK (success) if the worker is already stopped. |
93 void StopWorker(const StatusCallback& callback); | 96 void StopWorker(const StatusCallback& callback); |
94 | 97 |
| 98 // Sends an IPC message to the worker. |
| 99 // If the worker is not running this first tries to start it by |
| 100 // calling StartWorker internally. |
| 101 // |callback| can be null if the sender does not need to know if the |
| 102 // message is successfully sent or not. |
| 103 // (If the sender expects the receiver to respond please use |
| 104 // SendMessageAndRegisterCallback instead) |
| 105 void SendMessage(const IPC::Message& message, const StatusCallback& callback); |
| 106 |
| 107 // Sends an IPC message to the worker and registers |callback| to |
| 108 // be notified when a response message is received. |
| 109 // The |callback| will be also fired with an error code if the worker |
| 110 // is unexpectedly (being) stopped. |
| 111 // If the worker is not running this first tries to start it by |
| 112 // calling StartWorker internally. |
| 113 void SendMessageAndRegisterCallback(const IPC::Message& message, |
| 114 const MessageCallback& callback); |
| 115 |
95 // Sends fetch event to the associated embedded worker. | 116 // Sends fetch event to the associated embedded worker. |
96 // This immediately returns false if the worker is not running | 117 // This immediately returns false if the worker is not running |
97 // or sending a message to the child process fails. | 118 // or sending a message to the child process fails. |
98 // TODO(kinuko): Make this take callback as well. | 119 // TODO(kinuko): Make this take callback as well. |
99 bool DispatchFetchEvent(const ServiceWorkerFetchRequest& request); | 120 bool DispatchFetchEvent(const ServiceWorkerFetchRequest& request); |
100 | 121 |
101 // These are expected to be called when a renderer process host for the | 122 // These are expected to be called when a renderer process host for the |
102 // same-origin as for this ServiceWorkerVersion is created. The added | 123 // same-origin as for this ServiceWorkerVersion is created. The added |
103 // processes are used to run an in-renderer embedded worker. | 124 // processes are used to run an in-renderer embedded worker. |
104 void AddProcessToWorker(int process_id); | 125 void AddProcessToWorker(int process_id); |
105 void RemoveProcessToWorker(int process_id); | 126 void RemoveProcessToWorker(int process_id); |
106 | 127 |
107 EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); } | 128 EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); } |
108 | 129 |
109 // EmbeddedWorkerInstance::Observer overrides: | 130 // EmbeddedWorkerInstance::Observer overrides: |
110 virtual void OnStarted() OVERRIDE; | 131 virtual void OnStarted() OVERRIDE; |
111 virtual void OnStopped() OVERRIDE; | 132 virtual void OnStopped() OVERRIDE; |
112 virtual void OnMessageReceived(const IPC::Message& message) OVERRIDE; | 133 virtual void OnMessageReceived(int request_id, |
| 134 const IPC::Message& message) OVERRIDE; |
113 | 135 |
114 private: | 136 private: |
| 137 typedef ServiceWorkerVersion self; |
115 friend class base::RefCounted<ServiceWorkerVersion>; | 138 friend class base::RefCounted<ServiceWorkerVersion>; |
116 | 139 |
117 virtual ~ServiceWorkerVersion(); | 140 virtual ~ServiceWorkerVersion(); |
118 | 141 |
119 const int64 version_id_; | 142 const int64 version_id_; |
120 | 143 |
121 bool is_shutdown_; | 144 bool is_shutdown_; |
122 scoped_refptr<ServiceWorkerRegistration> registration_; | 145 scoped_refptr<ServiceWorkerRegistration> registration_; |
123 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; | 146 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; |
124 | 147 |
| 148 // Pending callbacks. |
125 std::vector<StatusCallback> start_callbacks_; | 149 std::vector<StatusCallback> start_callbacks_; |
126 std::vector<StatusCallback> stop_callbacks_; | 150 std::vector<StatusCallback> stop_callbacks_; |
127 | 151 |
| 152 IDMap<MessageCallback, IDMapOwnPointer> message_callbacks_; |
| 153 |
| 154 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 155 |
128 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 156 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
129 }; | 157 }; |
130 | 158 |
131 } // namespace content | 159 } // namespace content |
132 | 160 |
133 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 161 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
OLD | NEW |