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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_
11 #include "base/callback.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/weak_ptr.h"
15 #include "content/common/service_worker/service_worker_status_code.h"
24 // Interacts with the UI thread to keep RenderProcessHosts alive while the
25 // ServiceWorker system is using them. It also tracks candidate processes
26 // for each pattern. Each instance of ServiceWorkerProcessManager is destroyed
27 // on the UI thread shortly after its ServiceWorkerContextWrapper is destroyed.
28 class CONTENT_EXPORT ServiceWorkerProcessManager
{
30 // |*this| must be owned by a ServiceWorkerContextWrapper in a
31 // StoragePartition within |browser_context|.
32 explicit ServiceWorkerProcessManager(BrowserContext
* browser_context
);
34 // Shutdown must be called before the ProcessManager is destroyed.
35 ~ServiceWorkerProcessManager();
37 // Synchronously prevents new processes from being allocated
38 // and drops references to RenderProcessHosts.
41 // Returns a reference to a running process suitable for starting the Service
42 // Worker at |script_url|. Posts |callback| to the IO thread to indicate
43 // whether creation succeeded and the process ID that has a new reference.
45 // Allocation can fail with SERVICE_WORKER_PROCESS_NOT_FOUND if
46 // RenderProcessHost::Init fails.
47 void AllocateWorkerProcess(
48 int embedded_worker_id
,
50 const GURL
& script_url
,
51 const base::Callback
<void(ServiceWorkerStatusCode
,
53 bool is_new_process
)>& callback
);
55 // Drops a reference to a process that was running a Service Worker, and its
56 // SiteInstance. This must match a call to AllocateWorkerProcess.
57 void ReleaseWorkerProcess(int embedded_worker_id
);
59 // Sets a single process ID that will be used for all embedded workers. This
60 // bypasses the work of creating a process and managing its worker refcount so
61 // that unittests can run without a BrowserContext. The test is in charge of
62 // making sure this is only called on the same thread as runs the UI message
64 void SetProcessIdForTest(int process_id
) {
65 process_id_for_test_
= process_id
;
68 // Adds/removes process reference for the |pattern|, the process with highest
69 // references count will be chosen to start a worker.
70 void AddProcessReferenceToPattern(const GURL
& pattern
, int process_id
);
71 void RemoveProcessReferenceFromPattern(const GURL
& pattern
, int process_id
);
73 // Returns true if the |pattern| has at least one process to run.
74 bool PatternHasProcessToRun(const GURL
& pattern
) const;
77 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProcessManagerTest
, SortProcess
);
79 // Information about the process for an EmbeddedWorkerInstance.
81 explicit ProcessInfo(const scoped_refptr
<SiteInstance
>& site_instance
);
82 explicit ProcessInfo(int process_id
);
85 // Stores the SiteInstance the Worker lives inside. This needs to outlive
86 // the instance's use of its RPH to uphold assumptions in the
87 // ContentBrowserClient interface.
88 scoped_refptr
<SiteInstance
> site_instance
;
90 // In case the process was allocated without using a SiteInstance, we need
91 // to store a process ID to decrement a worker reference on shutdown.
92 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in
93 // so all processes can be allocated with a SiteInstance.
97 // Maps the process ID to its reference count.
98 typedef std::map
<int, int> ProcessRefMap
;
100 // Maps registration scope pattern to ProcessRefMap.
101 typedef std::map
<const GURL
, ProcessRefMap
> PatternProcessRefMap
;
103 // Returns a process vector sorted by the reference count for the |pattern|.
104 std::vector
<int> SortProcessesForPattern(const GURL
& pattern
) const;
106 // These fields are only accessed on the UI thread.
107 BrowserContext
* browser_context_
;
109 // Maps the ID of a running EmbeddedWorkerInstance to information about the
110 // process it's running inside. Since the Instances themselves live on the IO
111 // thread, this can be slightly out of date:
112 // * instance_info_ is populated while an Instance is STARTING and before
114 // * instance_info_ is depopulated in a message sent as the Instance becomes
116 std::map
<int, ProcessInfo
> instance_info_
;
118 // In unit tests, this will be returned as the process for all
119 // EmbeddedWorkerInstances.
120 int process_id_for_test_
;
122 // Candidate processes info for each pattern, should be accessed on the
124 PatternProcessRefMap pattern_processes_
;
126 // Used to double-check that we don't access *this after it's destroyed.
127 base::WeakPtr
<ServiceWorkerProcessManager
> weak_this_
;
128 base::WeakPtrFactory
<ServiceWorkerProcessManager
> weak_this_factory_
;
131 } // namespace content
134 // Specialized to post the deletion to the UI thread.
136 struct CONTENT_EXPORT DefaultDeleter
<content::ServiceWorkerProcessManager
> {
137 void operator()(content::ServiceWorkerProcessManager
* ptr
) const;
141 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_