| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | |
| 6 #define CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include "base/basictypes.h" | |
| 10 #include "base/memory/singleton.h" | |
| 11 #include "base/observer_list.h" | |
| 12 #include "base/threading/non_thread_safe.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" | |
| 16 #include "googleurl/src/gurl.h" | |
| 17 | |
| 18 struct ViewHostMsg_CreateWorker_Params; | |
| 19 class WorkerServiceObserver; | |
| 20 | |
| 21 namespace content { | |
| 22 class ResourceContext; | |
| 23 } // namespace content | |
| 24 | |
| 25 // A singleton for managing HTML5 web workers. | |
| 26 class CONTENT_EXPORT WorkerService { | |
| 27 public: | |
| 28 // Returns the WorkerService singleton. | |
| 29 static WorkerService* GetInstance(); | |
| 30 | |
| 31 // These methods correspond to worker related IPCs. | |
| 32 void CreateWorker(const ViewHostMsg_CreateWorker_Params& params, | |
| 33 int route_id, | |
| 34 WorkerMessageFilter* filter, | |
| 35 const content::ResourceContext& resource_context); | |
| 36 void LookupSharedWorker(const ViewHostMsg_CreateWorker_Params& params, | |
| 37 int route_id, | |
| 38 WorkerMessageFilter* filter, | |
| 39 const content::ResourceContext* resource_context, | |
| 40 bool* exists, | |
| 41 bool* url_error); | |
| 42 void CancelCreateDedicatedWorker(int route_id, WorkerMessageFilter* filter); | |
| 43 void ForwardToWorker(const IPC::Message& message, | |
| 44 WorkerMessageFilter* filter); | |
| 45 void DocumentDetached(unsigned long long document_id, | |
| 46 WorkerMessageFilter* filter); | |
| 47 | |
| 48 void OnWorkerMessageFilterClosing(WorkerMessageFilter* filter); | |
| 49 | |
| 50 int next_worker_route_id() { return ++next_worker_route_id_; } | |
| 51 | |
| 52 // 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 | |
| 54 // parent. | |
| 55 // 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. | |
| 57 bool GetRendererForWorker(int worker_process_id, | |
| 58 int* render_process_id, | |
| 59 int* render_view_id) const; | |
| 60 const WorkerProcessHost::WorkerInstance* FindWorkerInstance( | |
| 61 int worker_process_id); | |
| 62 | |
| 63 void AddObserver(WorkerServiceObserver* observer); | |
| 64 void RemoveObserver(WorkerServiceObserver* observer); | |
| 65 | |
| 66 void NotifyWorkerDestroyed( | |
| 67 WorkerProcessHost* process, | |
| 68 int worker_route_id); | |
| 69 void NotifyWorkerContextStarted( | |
| 70 WorkerProcessHost* process, | |
| 71 int worker_route_id); | |
| 72 | |
| 73 // Used when multiple workers can run in the same process. | |
| 74 static const int kMaxWorkerProcessesWhenSharing; | |
| 75 | |
| 76 // Used when we run each worker in a separate process. | |
| 77 static const int kMaxWorkersWhenSeparate; | |
| 78 static const int kMaxWorkersPerTabWhenSeparate; | |
| 79 | |
| 80 private: | |
| 81 friend struct DefaultSingletonTraits<WorkerService>; | |
| 82 | |
| 83 WorkerService(); | |
| 84 ~WorkerService(); | |
| 85 | |
| 86 // Given a WorkerInstance, create an associated worker process. | |
| 87 bool CreateWorkerFromInstance(WorkerProcessHost::WorkerInstance instance); | |
| 88 | |
| 89 // Returns a WorkerProcessHost object if one exists for the given domain, or | |
| 90 // NULL if there are no such workers yet. | |
| 91 WorkerProcessHost* GetProcessForDomain(const GURL& url); | |
| 92 | |
| 93 // Returns a WorkerProcessHost based on a strategy of creating one worker per | |
| 94 // core. | |
| 95 WorkerProcessHost* GetProcessToFillUpCores(); | |
| 96 | |
| 97 // Returns the WorkerProcessHost from the existing set that has the least | |
| 98 // number of worker instance running. | |
| 99 WorkerProcessHost* GetLeastLoadedWorker(); | |
| 100 | |
| 101 // Checks if we can create a worker process based on the process limit when | |
| 102 // we're using a strategy of one process per core. | |
| 103 bool CanCreateWorkerProcess( | |
| 104 const WorkerProcessHost::WorkerInstance& instance); | |
| 105 | |
| 106 // Checks if the tab associated with the passed RenderView can create a | |
| 107 // worker process based on the process limit when we're using a strategy of | |
| 108 // one worker per process. | |
| 109 bool TabCanCreateWorkerProcess( | |
| 110 int render_process_id, int render_route_id, bool* hit_total_worker_limit); | |
| 111 | |
| 112 // Tries to see if any of the queued workers can be created. | |
| 113 void TryStartingQueuedWorker(); | |
| 114 | |
| 115 // APIs for manipulating our set of pending shared worker instances. | |
| 116 WorkerProcessHost::WorkerInstance* CreatePendingInstance( | |
| 117 const GURL& url, | |
| 118 const string16& name, | |
| 119 const content::ResourceContext* resource_context); | |
| 120 WorkerProcessHost::WorkerInstance* FindPendingInstance( | |
| 121 const GURL& url, | |
| 122 const string16& name, | |
| 123 const content::ResourceContext* resource_context); | |
| 124 void RemovePendingInstances( | |
| 125 const GURL& url, | |
| 126 const string16& name, | |
| 127 const content::ResourceContext* resource_context); | |
| 128 | |
| 129 WorkerProcessHost::WorkerInstance* FindSharedWorkerInstance( | |
| 130 const GURL& url, | |
| 131 const string16& name, | |
| 132 const content::ResourceContext* resource_context); | |
| 133 | |
| 134 content::NotificationRegistrar registrar_; | |
| 135 int next_worker_route_id_; | |
| 136 | |
| 137 WorkerProcessHost::Instances queued_workers_; | |
| 138 | |
| 139 // 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 | |
| 141 // URL mismatches when two pages launch shared workers simultaneously. | |
| 142 WorkerProcessHost::Instances pending_shared_workers_; | |
| 143 | |
| 144 ObserverList<WorkerServiceObserver> observers_; | |
| 145 | |
| 146 DISALLOW_COPY_AND_ASSIGN(WorkerService); | |
| 147 }; | |
| 148 | |
| 149 #endif // CONTENT_BROWSER_WORKER_HOST_WORKER_SERVICE_H_ | |
| OLD | NEW |