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 #ifndef CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ | 5 #ifndef CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ |
6 #define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ | 6 #define CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
12 #include "base/sequenced_task_runner_helpers.h" | 12 #include "base/sequenced_task_runner_helpers.h" |
13 #include "base/synchronization/lock.h" | |
14 #include "content/common/service_worker/service_worker_types.h" | 13 #include "content/common/service_worker/service_worker_types.h" |
15 | 14 |
16 namespace base { | 15 namespace base { |
17 class SingleThreadTaskRunner; | 16 class SingleThreadTaskRunner; |
18 } | 17 } |
19 | 18 |
20 namespace content { | 19 namespace content { |
21 | 20 |
22 class ServiceWorkerHandleReference; | 21 class ServiceWorkerHandleReference; |
23 class ServiceWorkerRegistrationHandleReference; | 22 class ServiceWorkerRegistrationHandleReference; |
24 struct ServiceWorkerProviderContextDeleter; | 23 struct ServiceWorkerProviderContextDeleter; |
25 class ThreadSafeSender; | 24 class ThreadSafeSender; |
26 | 25 |
27 // An instance of this class holds information related to Document/Worker. | 26 // An instance of this class holds information related to Document/Worker. |
28 // Created and destructed on the main thread. Unless otherwise noted, all | 27 // Created and destructed on the main thread. Unless otherwise noted, all |
29 // methods are called on the main thread. | 28 // methods are called on the main thread. The lifetime of this class is equals |
| 29 // to the corresponding ServiceWorkerNetworkProvider. |
| 30 // |
| 31 // The role of this class varies for controllees and controllers: |
| 32 // - For controllees, this is used for keeping the associated registration and |
| 33 // the controller alive to create controllee's ServiceWorkerContainer. The |
| 34 // references to them are kept until OnDisassociateRegistration() is called |
| 35 // or OnSetControllerServiceWorker() is called with an invalid worker info. |
| 36 // - For controllers, this is used for keeping the associated registration and |
| 37 // its versions alive to create controller's ServiceWorkerGlobalScope. The |
| 38 // references to them are kept until OnDisassociateRegistration() is called. |
| 39 // |
| 40 // These operations are actually done in delegate classes owned by this class: |
| 41 // ControlleeDelegate and ControllerDelegate. |
30 class ServiceWorkerProviderContext | 42 class ServiceWorkerProviderContext |
31 : public base::RefCountedThreadSafe<ServiceWorkerProviderContext, | 43 : public base::RefCountedThreadSafe<ServiceWorkerProviderContext, |
32 ServiceWorkerProviderContextDeleter> { | 44 ServiceWorkerProviderContextDeleter> { |
33 public: | 45 public: |
34 explicit ServiceWorkerProviderContext(int provider_id); | 46 ServiceWorkerProviderContext(int provider_id, |
| 47 ServiceWorkerProviderType provider_type); |
35 | 48 |
36 // Called from ServiceWorkerDispatcher. | 49 // Called from ServiceWorkerDispatcher. |
37 void OnAssociateRegistration(const ServiceWorkerRegistrationObjectInfo& info, | 50 void OnAssociateRegistration(const ServiceWorkerRegistrationObjectInfo& info, |
38 const ServiceWorkerVersionAttributes& attrs); | 51 const ServiceWorkerVersionAttributes& attrs); |
39 void OnDisassociateRegistration(); | 52 void OnDisassociateRegistration(); |
40 void OnSetControllerServiceWorker(const ServiceWorkerObjectInfo& info); | 53 void OnSetControllerServiceWorker(const ServiceWorkerObjectInfo& info); |
41 | 54 |
| 55 // Called on the worker thread. Used for initializing |
| 56 // ServiceWorkerGlobalScope. |
| 57 void GetAssociatedRegistration(ServiceWorkerRegistrationObjectInfo* info, |
| 58 ServiceWorkerVersionAttributes* attrs); |
| 59 |
42 int provider_id() const { return provider_id_; } | 60 int provider_id() const { return provider_id_; } |
43 | 61 |
44 ServiceWorkerHandleReference* controller(); | 62 ServiceWorkerHandleReference* controller(); |
45 | 63 |
46 // Called on the worker thread. | |
47 bool GetRegistrationInfoAndVersionAttributes( | |
48 ServiceWorkerRegistrationObjectInfo* info, | |
49 ServiceWorkerVersionAttributes* attrs); | |
50 | |
51 private: | 64 private: |
52 friend class base::DeleteHelper<ServiceWorkerProviderContext>; | 65 friend class base::DeleteHelper<ServiceWorkerProviderContext>; |
53 friend class base::RefCountedThreadSafe<ServiceWorkerProviderContext, | 66 friend class base::RefCountedThreadSafe<ServiceWorkerProviderContext, |
54 ServiceWorkerProviderContextDeleter>; | 67 ServiceWorkerProviderContextDeleter>; |
55 friend struct ServiceWorkerProviderContextDeleter; | 68 friend struct ServiceWorkerProviderContextDeleter; |
56 | 69 |
| 70 class Delegate; |
| 71 class ControlleeDelegate; |
| 72 class ControllerDelegate; |
| 73 |
57 ~ServiceWorkerProviderContext(); | 74 ~ServiceWorkerProviderContext(); |
58 void DestructOnMainThread() const; | 75 void DestructOnMainThread() const; |
59 | 76 |
60 const int provider_id_; | 77 const int provider_id_; |
61 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; | 78 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner_; |
62 scoped_refptr<ThreadSafeSender> thread_safe_sender_; | 79 scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
63 | 80 |
64 // Protects (installing, waiting, active) worker and registration references. | 81 scoped_ptr<Delegate> delegate_; |
65 base::Lock lock_; | |
66 | |
67 // Used on both the main thread and the worker thread. | |
68 scoped_ptr<ServiceWorkerHandleReference> installing_; | |
69 scoped_ptr<ServiceWorkerHandleReference> waiting_; | |
70 scoped_ptr<ServiceWorkerHandleReference> active_; | |
71 scoped_ptr<ServiceWorkerRegistrationHandleReference> registration_; | |
72 | |
73 // Used only on the main thread. | |
74 scoped_ptr<ServiceWorkerHandleReference> controller_; | |
75 | 82 |
76 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderContext); | 83 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerProviderContext); |
77 }; | 84 }; |
78 | 85 |
79 struct ServiceWorkerProviderContextDeleter { | 86 struct ServiceWorkerProviderContextDeleter { |
80 static void Destruct(const ServiceWorkerProviderContext* context) { | 87 static void Destruct(const ServiceWorkerProviderContext* context) { |
81 context->DestructOnMainThread(); | 88 context->DestructOnMainThread(); |
82 } | 89 } |
83 }; | 90 }; |
84 | 91 |
85 } // namespace content | 92 } // namespace content |
86 | 93 |
87 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ | 94 #endif // CONTENT_CHILD_SERVICE_WORKER_SERVICE_WORKER_PROVIDER_CONTEXT_H_ |
OLD | NEW |