| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| 6 #define CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | 6 #define CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/singleton.h" | 12 #include "base/singleton.h" |
| 13 #include "chrome/browser/worker_host/worker_process_host.h" | 13 #include "chrome/browser/worker_host/worker_process_host.h" |
| 14 #include "chrome/common/notification_registrar.h" | |
| 15 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 16 #include "ipc/ipc_message.h" | 15 #include "ipc/ipc_message.h" |
| 17 | 16 |
| 18 class ChromeURLRequestContext; | 17 class URLRequestContextGetter; |
| 19 class ResourceDispatcherHost; | 18 struct ViewHostMsg_CreateWorker_Params; |
| 20 | 19 |
| 21 class WorkerService : public NotificationObserver { | 20 // A singelton for managing HTML5 web workers. |
| 21 class WorkerService { |
| 22 public: | 22 public: |
| 23 // Returns the WorkerService singleton. | 23 // Returns the WorkerService singleton. |
| 24 static WorkerService* GetInstance(); | 24 static WorkerService* GetInstance(); |
| 25 | 25 |
| 26 // Initialize the WorkerService. OK to be called multiple times. | 26 // These methods correspond to worker related IPCs. |
| 27 void Initialize(ResourceDispatcherHost* rdh); | 27 void CreateWorker(const ViewHostMsg_CreateWorker_Params& params, |
| 28 int route_id, |
| 29 WorkerMessageFilter* filter, |
| 30 URLRequestContextGetter* request_context); |
| 31 void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params, |
| 32 int route_id, |
| 33 WorkerMessageFilter* filter, |
| 34 bool off_the_record, |
| 35 bool* exists, |
| 36 bool* url_error); |
| 37 void CancelCreateDedicatedWorker(int route_id, WorkerMessageFilter* filter); |
| 38 void ForwardToWorker(const IPC::Message& message, |
| 39 WorkerMessageFilter* filter); |
| 40 void DocumentDetached(unsigned long long document_id, |
| 41 WorkerMessageFilter* filter); |
| 28 | 42 |
| 29 // Creates a decidated worker. Returns true on success. | 43 void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); |
| 30 bool CreateDedicatedWorker(const GURL &url, | |
| 31 bool is_off_the_record, | |
| 32 unsigned long long document_id, | |
| 33 int renderer_pid, | |
| 34 int render_view_route_id, | |
| 35 IPC::Message::Sender* sender, | |
| 36 int sender_route_id, | |
| 37 int parent_process_id, | |
| 38 int parent_appcache_host_id, | |
| 39 ChromeURLRequestContext* request_context); | |
| 40 | |
| 41 // Creates a shared worker. Returns true on success. | |
| 42 bool CreateSharedWorker(const GURL &url, | |
| 43 bool is_off_the_record, | |
| 44 const string16& name, | |
| 45 unsigned long long document_id, | |
| 46 int renderer_pid, | |
| 47 int render_view_route_id, | |
| 48 IPC::Message::Sender* sender, | |
| 49 int sender_route_id, | |
| 50 int64 main_resource_appcache_id, | |
| 51 ChromeURLRequestContext* request_context); | |
| 52 | |
| 53 // Validates the passed URL and checks for the existence of matching shared | |
| 54 // worker. Returns true if the url was found, and sets the url_mismatch out | |
| 55 // param to true/false depending on whether there's a url mismatch with an | |
| 56 // existing shared worker with the same name. | |
| 57 bool LookupSharedWorker(const GURL &url, | |
| 58 const string16& name, | |
| 59 bool off_the_record, | |
| 60 unsigned long long document_id, | |
| 61 int renderer_pid, | |
| 62 int render_view_route_id, | |
| 63 IPC::Message::Sender* sender, | |
| 64 int sender_route_id, | |
| 65 bool* url_mismatch); | |
| 66 | |
| 67 // Notification from the renderer that a given document has detached, so any | |
| 68 // associated shared workers can be shut down. | |
| 69 void DocumentDetached(IPC::Message::Sender* sender, | |
| 70 unsigned long long document_id); | |
| 71 | |
| 72 // Cancel creation of a dedicated worker that hasn't started yet. | |
| 73 void CancelCreateDedicatedWorker(IPC::Message::Sender* sender, | |
| 74 int sender_route_id); | |
| 75 | |
| 76 // Called by the worker creator when a message arrives that should be | |
| 77 // forwarded to the worker process. | |
| 78 void ForwardMessage(const IPC::Message& message, | |
| 79 IPC::Message::Sender* sender); | |
| 80 | 44 |
| 81 int next_worker_route_id() { return ++next_worker_route_id_; } | 45 int next_worker_route_id() { return ++next_worker_route_id_; } |
| 82 | 46 |
| 47 // Given a worker's process id, return the IDs of the renderer process and |
| 48 // render view that created it. For shared workers, this returns the first |
| 49 // parent. |
| 83 // TODO(dimich): This code assumes there is 1 worker per worker process, which | 50 // TODO(dimich): This code assumes there is 1 worker per worker process, which |
| 84 // is how it is today until V8 can run in separate threads. | 51 // is how it is today until V8 can run in separate threads. |
| 52 bool GetRendererForWorker(int worker_process_id, |
| 53 int* render_process_id, |
| 54 int* render_view_id) const; |
| 85 const WorkerProcessHost::WorkerInstance* FindWorkerInstance( | 55 const WorkerProcessHost::WorkerInstance* FindWorkerInstance( |
| 86 int worker_process_id); | 56 int worker_process_id); |
| 87 | 57 |
| 88 WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance( | |
| 89 const GURL& url, const string16& name, bool off_the_record); | |
| 90 | |
| 91 // Used when multiple workers can run in the same process. | 58 // Used when multiple workers can run in the same process. |
| 92 static const int kMaxWorkerProcessesWhenSharing; | 59 static const int kMaxWorkerProcessesWhenSharing; |
| 93 | 60 |
| 94 // Used when we run each worker in a separate process. | 61 // Used when we run each worker in a separate process. |
| 95 static const int kMaxWorkersWhenSeparate; | 62 static const int kMaxWorkersWhenSeparate; |
| 96 static const int kMaxWorkersPerTabWhenSeparate; | 63 static const int kMaxWorkersPerTabWhenSeparate; |
| 97 | 64 |
| 98 private: | 65 private: |
| 99 friend struct DefaultSingletonTraits<WorkerService>; | 66 friend struct DefaultSingletonTraits<WorkerService>; |
| 100 | 67 |
| 101 WorkerService(); | 68 WorkerService(); |
| 102 ~WorkerService(); | 69 ~WorkerService(); |
| 103 | 70 |
| 104 bool CreateWorker(const GURL &url, | |
| 105 bool is_shared, | |
| 106 bool is_off_the_record, | |
| 107 const string16& name, | |
| 108 unsigned long long document_id, | |
| 109 int renderer_pid, | |
| 110 int render_view_route_id, | |
| 111 IPC::Message::Sender* sender, | |
| 112 int sender_route_id, | |
| 113 int parent_process_id, | |
| 114 int parent_appcache_host_id, | |
| 115 int64 main_resource_appcache_id, | |
| 116 ChromeURLRequestContext* request_context); | |
| 117 | |
| 118 // Given a WorkerInstance, create an associated worker process. | 71 // Given a WorkerInstance, create an associated worker process. |
| 119 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance); | 72 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance); |
| 120 | 73 |
| 121 // Returns a WorkerProcessHost object if one exists for the given domain, or | 74 // Returns a WorkerProcessHost object if one exists for the given domain, or |
| 122 // NULL if there are no such workers yet. | 75 // NULL if there are no such workers yet. |
| 123 WorkerProcessHost* GetProcessForDomain(const GURL& url); | 76 WorkerProcessHost* GetProcessForDomain(const GURL& url); |
| 124 | 77 |
| 125 // Returns a WorkerProcessHost based on a strategy of creating one worker per | 78 // Returns a WorkerProcessHost based on a strategy of creating one worker per |
| 126 // core. | 79 // core. |
| 127 WorkerProcessHost* GetProcessToFillUpCores(); | 80 WorkerProcessHost* GetProcessToFillUpCores(); |
| 128 | 81 |
| 129 // Returns the WorkerProcessHost from the existing set that has the least | 82 // Returns the WorkerProcessHost from the existing set that has the least |
| 130 // number of worker instance running. | 83 // number of worker instance running. |
| 131 WorkerProcessHost* GetLeastLoadedWorker(); | 84 WorkerProcessHost* GetLeastLoadedWorker(); |
| 132 | 85 |
| 133 // Checks if we can create a worker process based on the process limit when | 86 // Checks if we can create a worker process based on the process limit when |
| 134 // we're using a strategy of one process per core. | 87 // we're using a strategy of one process per core. |
| 135 bool CanCreateWorkerProcess( | 88 bool CanCreateWorkerProcess( |
| 136 const WorkerProcessHost::WorkerInstance& instance); | 89 const WorkerProcessHost::WorkerInstance& instance); |
| 137 | 90 |
| 138 // Checks if the tab associated with the passed RenderView can create a | 91 // Checks if the tab associated with the passed RenderView can create a |
| 139 // worker process based on the process limit when we're using a strategy of | 92 // worker process based on the process limit when we're using a strategy of |
| 140 // one worker per process. | 93 // one worker per process. |
| 141 bool TabCanCreateWorkerProcess( | 94 bool TabCanCreateWorkerProcess( |
| 142 int renderer_id, int render_view_route_id, bool* hit_total_worker_limit); | 95 int render_process_id, int render_route_id, bool* hit_total_worker_limit); |
| 143 | 96 |
| 144 // NotificationObserver interface. | 97 // Tries to see if any of the queued workers can be created. |
| 145 virtual void Observe(NotificationType type, | 98 void TryStartingQueuedWorker(); |
| 146 const NotificationSource& source, | |
| 147 const NotificationDetails& details); | |
| 148 | |
| 149 // Notifies us that a process that's talking to a worker has shut down. | |
| 150 void SenderShutdown(IPC::Message::Sender* sender); | |
| 151 | |
| 152 // Notifies us that a worker process has closed. | |
| 153 void WorkerProcessDestroyed(WorkerProcessHost* process); | |
| 154 | 99 |
| 155 // APIs for manipulating our set of pending shared worker instances. | 100 // APIs for manipulating our set of pending shared worker instances. |
| 156 WorkerProcessHost::WorkerInstance* CreatePendingInstance( | 101 WorkerProcessHost::WorkerInstance* CreatePendingInstance( |
| 157 const GURL& url, const string16& name, bool off_the_record); | 102 const GURL& url, const string16& name, bool off_the_record); |
| 158 WorkerProcessHost::WorkerInstance* FindPendingInstance( | 103 WorkerProcessHost::WorkerInstance* FindPendingInstance( |
| 159 const GURL& url, const string16& name, bool off_the_record); | 104 const GURL& url, const string16& name, bool off_the_record); |
| 160 void RemovePendingInstances( | 105 void RemovePendingInstances( |
| 161 const GURL& url, const string16& name, bool off_the_record); | 106 const GURL& url, const string16& name, bool off_the_record); |
| 162 | 107 |
| 108 WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance( |
| 109 const GURL& url, const string16& name, bool off_the_record); |
| 110 |
| 163 NotificationRegistrar registrar_; | 111 NotificationRegistrar registrar_; |
| 164 int next_worker_route_id_; | 112 int next_worker_route_id_; |
| 165 ResourceDispatcherHost* resource_dispatcher_host_; | |
| 166 | 113 |
| 167 WorkerProcessHost::Instances queued_workers_; | 114 WorkerProcessHost::Instances queued_workers_; |
| 168 | 115 |
| 169 // These are shared workers that have been looked up, but not created yet. | 116 // These are shared workers that have been looked up, but not created yet. |
| 170 // We need to keep a list of these to synchronously detect shared worker | 117 // We need to keep a list of these to synchronously detect shared worker |
| 171 // URL mismatches when two pages launch shared workers simultaneously. | 118 // URL mismatches when two pages launch shared workers simultaneously. |
| 172 WorkerProcessHost::Instances pending_shared_workers_; | 119 WorkerProcessHost::Instances pending_shared_workers_; |
| 173 | 120 |
| 174 DISALLOW_COPY_AND_ASSIGN(WorkerService); | 121 DISALLOW_COPY_AND_ASSIGN(WorkerService); |
| 175 }; | 122 }; |
| 176 | 123 |
| 177 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | 124 #endif // CHROME_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ |
| OLD | NEW |