| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_WORKER_HOST_WORKER_SERVICE_H_ | 5 #ifndef CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| 6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | 6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/observer_list.h" | 11 #include "base/observer_list.h" |
| 12 #include "base/threading/non_thread_safe.h" | 12 #include "base/threading/non_thread_safe.h" |
| 13 #include "content/browser/worker_host/worker_process_host.h" | 13 #include "content/browser/worker_host/worker_process_host.h" |
| 14 #include "content/common/content_export.h" | |
| 15 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
| 16 #include "googleurl/src/gurl.h" | 15 #include "content/public/browser/worker_service.h" |
| 17 | 16 |
| 17 class GURL; |
| 18 struct ViewHostMsg_CreateWorker_Params; | 18 struct ViewHostMsg_CreateWorker_Params; |
| 19 class WorkerServiceObserver; | |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 class ResourceContext; | 21 class ResourceContext; |
| 23 } // namespace content | 22 class WorkerServiceObserver; |
| 24 | 23 |
| 25 // A singleton for managing HTML5 web workers. | 24 class CONTENT_EXPORT WorkerServiceImpl : public WorkerService { |
| 26 class CONTENT_EXPORT WorkerService { | |
| 27 public: | 25 public: |
| 28 // Returns the WorkerService singleton. | 26 // Returns the WorkerServiceImpl singleton. |
| 29 static WorkerService* GetInstance(); | 27 static WorkerServiceImpl* GetInstance(); |
| 28 |
| 29 // WorkerService implementation: |
| 30 virtual void AddObserver(WorkerServiceObserver* observer) OVERRIDE; |
| 31 virtual void RemoveObserver(WorkerServiceObserver* observer) OVERRIDE; |
| 30 | 32 |
| 31 // These methods correspond to worker related IPCs. | 33 // These methods correspond to worker related IPCs. |
| 32 void CreateWorker(const ViewHostMsg_CreateWorker_Params& params, | 34 void CreateWorker(const ViewHostMsg_CreateWorker_Params& params, |
| 33 int route_id, | 35 int route_id, |
| 34 WorkerMessageFilter* filter, | 36 WorkerMessageFilter* filter, |
| 35 const content::ResourceContext& resource_context); | 37 const ResourceContext& resource_context); |
| 36 void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params, | 38 void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params, |
| 37 int route_id, | 39 int route_id, |
| 38 WorkerMessageFilter* filter, | 40 WorkerMessageFilter* filter, |
| 39 const content::ResourceContext* resource_context, | 41 const ResourceContext* resource_context, |
| 40 bool* exists, | 42 bool* exists, |
| 41 bool* url_error); | 43 bool* url_error); |
| 42 void CancelCreateDedicatedWorker(int route_id, WorkerMessageFilter* filter); | 44 void CancelCreateDedicatedWorker(int route_id, WorkerMessageFilter* filter); |
| 43 void ForwardToWorker(const IPC::Message& message, | 45 void ForwardToWorker(const IPC::Message& message, |
| 44 WorkerMessageFilter* filter); | 46 WorkerMessageFilter* filter); |
| 45 void DocumentDetached(unsigned long long document_id, | 47 void DocumentDetached(unsigned long long document_id, |
| 46 WorkerMessageFilter* filter); | 48 WorkerMessageFilter* filter); |
| 47 | 49 |
| 48 void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); | 50 void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); |
| 49 | 51 |
| 50 int next_worker_route_id() { return ++next_worker_route_id_; } | 52 int next_worker_route_id() { return ++next_worker_route_id_; } |
| 51 | 53 |
| 52 // Given a worker's process id, return the IDs of the renderer process and | 54 // Given a worker's process id, return the IDs of the renderer process and |
| 53 // render view that created it. For shared workers, this returns the first | 55 // render view that created it. For shared workers, this returns the first |
| 54 // parent. | 56 // parent. |
| 55 // TODO(dimich): This code assumes there is 1 worker per worker process, which | 57 // TODO(dimich): This code assumes there is 1 worker per worker process, which |
| 56 // is how it is today until V8 can run in separate threads. | 58 // is how it is today until V8 can run in separate threads. |
| 57 bool GetRendererForWorker(int worker_process_id, | 59 bool GetRendererForWorker(int worker_process_id, |
| 58 int* render_process_id, | 60 int* render_process_id, |
| 59 int* render_view_id) const; | 61 int* render_view_id) const; |
| 60 const WorkerProcessHost::WorkerInstance* FindWorkerInstance( | 62 const WorkerProcessHost::WorkerInstance* FindWorkerInstance( |
| 61 int worker_process_id); | 63 int worker_process_id); |
| 62 | 64 |
| 63 void AddObserver(WorkerServiceObserver* observer); | |
| 64 void RemoveObserver(WorkerServiceObserver* observer); | |
| 65 | |
| 66 void NotifyWorkerDestroyed( | 65 void NotifyWorkerDestroyed( |
| 67 WorkerProcessHost* process, | 66 WorkerProcessHost* process, |
| 68 int worker_route_id); | 67 int worker_route_id); |
| 69 void NotifyWorkerContextStarted( | 68 void NotifyWorkerContextStarted( |
| 70 WorkerProcessHost* process, | 69 WorkerProcessHost* process, |
| 71 int worker_route_id); | 70 int worker_route_id); |
| 72 | 71 |
| 73 // Used when multiple workers can run in the same process. | 72 // Used when multiple workers can run in the same process. |
| 74 static const int kMaxWorkerProcessesWhenSharing; | 73 static const int kMaxWorkerProcessesWhenSharing; |
| 75 | 74 |
| 76 // Used when we run each worker in a separate process. | 75 // Used when we run each worker in a separate process. |
| 77 static const int kMaxWorkersWhenSeparate; | 76 static const int kMaxWorkersWhenSeparate; |
| 78 static const int kMaxWorkersPerTabWhenSeparate; | 77 static const int kMaxWorkersPerTabWhenSeparate; |
| 79 | 78 |
| 80 private: | 79 private: |
| 81 friend struct DefaultSingletonTraits<WorkerService>; | 80 friend struct DefaultSingletonTraits<WorkerServiceImpl>; |
| 82 | 81 |
| 83 WorkerService(); | 82 WorkerServiceImpl(); |
| 84 ~WorkerService(); | 83 virtual ~WorkerServiceImpl(); |
| 85 | 84 |
| 86 // Given a WorkerInstance, create an associated worker process. | 85 // Given a WorkerInstance, create an associated worker process. |
| 87 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance); | 86 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance); |
| 88 | 87 |
| 89 // Returns a WorkerProcessHost object if one exists for the given domain, or | 88 // Returns a WorkerProcessHost object if one exists for the given domain, or |
| 90 // NULL if there are no such workers yet. | 89 // NULL if there are no such workers yet. |
| 91 WorkerProcessHost* GetProcessForDomain(const GURL& url); | 90 WorkerProcessHost* GetProcessForDomain(const GURL& url); |
| 92 | 91 |
| 93 // Returns a WorkerProcessHost based on a strategy of creating one worker per | 92 // Returns a WorkerProcessHost based on a strategy of creating one worker per |
| 94 // core. | 93 // core. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 109 bool TabCanCreateWorkerProcess( | 108 bool TabCanCreateWorkerProcess( |
| 110 int render_process_id, int render_route_id, bool* hit_total_worker_limit); | 109 int render_process_id, int render_route_id, bool* hit_total_worker_limit); |
| 111 | 110 |
| 112 // Tries to see if any of the queued workers can be created. | 111 // Tries to see if any of the queued workers can be created. |
| 113 void TryStartingQueuedWorker(); | 112 void TryStartingQueuedWorker(); |
| 114 | 113 |
| 115 // APIs for manipulating our set of pending shared worker instances. | 114 // APIs for manipulating our set of pending shared worker instances. |
| 116 WorkerProcessHost::WorkerInstance* CreatePendingInstance( | 115 WorkerProcessHost::WorkerInstance* CreatePendingInstance( |
| 117 const GURL& url, | 116 const GURL& url, |
| 118 const string16& name, | 117 const string16& name, |
| 119 const content::ResourceContext* resource_context); | 118 const ResourceContext* resource_context); |
| 120 WorkerProcessHost::WorkerInstance* FindPendingInstance( | 119 WorkerProcessHost::WorkerInstance* FindPendingInstance( |
| 121 const GURL& url, | 120 const GURL& url, |
| 122 const string16& name, | 121 const string16& name, |
| 123 const content::ResourceContext* resource_context); | 122 const ResourceContext* resource_context); |
| 124 void RemovePendingInstances( | 123 void RemovePendingInstances( |
| 125 const GURL& url, | 124 const GURL& url, |
| 126 const string16& name, | 125 const string16& name, |
| 127 const content::ResourceContext* resource_context); | 126 const ResourceContext* resource_context); |
| 128 | 127 |
| 129 WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance( | 128 WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance( |
| 130 const GURL& url, | 129 const GURL& url, |
| 131 const string16& name, | 130 const string16& name, |
| 132 const content::ResourceContext* resource_context); | 131 const ResourceContext* resource_context); |
| 133 | 132 |
| 134 content::NotificationRegistrar registrar_; | 133 NotificationRegistrar registrar_; |
| 135 int next_worker_route_id_; | 134 int next_worker_route_id_; |
| 136 | 135 |
| 137 WorkerProcessHost::Instances queued_workers_; | 136 WorkerProcessHost::Instances queued_workers_; |
| 138 | 137 |
| 139 // These are shared workers that have been looked up, but not created yet. | 138 // These are shared workers that have been looked up, but not created yet. |
| 140 // We need to keep a list of these to synchronously detect shared worker | 139 // We need to keep a list of these to synchronously detect shared worker |
| 141 // URL mismatches when two pages launch shared workers simultaneously. | 140 // URL mismatches when two pages launch shared workers simultaneously. |
| 142 WorkerProcessHost::Instances pending_shared_workers_; | 141 WorkerProcessHost::Instances pending_shared_workers_; |
| 143 | 142 |
| 144 ObserverList<WorkerServiceObserver> observers_; | 143 ObserverList<WorkerServiceObserver> observers_; |
| 145 | 144 |
| 146 DISALLOW_COPY_AND_ASSIGN(WorkerService); | 145 DISALLOW_COPY_AND_ASSIGN(WorkerServiceImpl); |
| 147 }; | 146 }; |
| 148 | 147 |
| 148 } // namespace content |
| 149 |
| 149 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | 150 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| OLD | NEW |