Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_process_manager.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_
7
8 #include <vector>
9
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "content/common/service_worker/service_worker_status_code.h"
14
15 class GURL;
16
17 namespace content {
18
19 class BrowserContext;
20 class ServiceWorkerContextWrapper;
21
22 // Interacts with the UI thread to keep RenderProcessHosts alive while the
23 // ServiceWorker system is using them.  Each instance of
24 // ServiceWorkerProcessManager is destroyed on the UI thread shortly after its
25 // ServiceWorkerContextCore is destroyed on the IO thread.
26 class CONTENT_EXPORT ServiceWorkerProcessManager {
27  public:
28   // |*this| must be owned by |context_wrapper|->context().
29   explicit ServiceWorkerProcessManager(
30       ServiceWorkerContextWrapper* context_wrapper);
31
32   ~ServiceWorkerProcessManager();
33
34   // Returns a reference to a running process suitable for starting the Service
35   // Worker at |script_url|. Processes in |process_ids| will be checked in order
36   // for existence, and if none exist, then a new process will be created. Posts
37   // |callback| to the IO thread to indicate whether creation succeeded and the
38   // process ID that has a new reference.
39   //
40   // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if
41   // RenderProcessHost::Init fails.
42   void AllocateWorkerProcess(
43       const std::vector<int>& process_ids,
44       const GURL& script_url,
45       const base::Callback<void(ServiceWorkerStatusCode, int process_id)>&
46           callback) const;
47
48   // Drops a reference to a process that was running a Service Worker.  This
49   // must match a call to AllocateWorkerProcess.
50   void ReleaseWorkerProcess(int process_id);
51
52   // |increment_for_test| and |decrement_for_test| define how to look up a
53   // process by ID and increment or decrement its worker reference count. This
54   // must be called before any reference to this object escapes to another
55   // thread, and is considered part of construction.
56   void SetProcessRefcountOpsForTest(
57       const base::Callback<bool(int)>& increment_for_test,
58       const base::Callback<bool(int)>& decrement_for_test);
59
60  private:
61   bool IncrementWorkerRefcountByPid(int process_id) const;
62   bool DecrementWorkerRefcountByPid(int process_id) const;
63
64   // These fields are only accessed on the UI thread after construction.
65   // The reference cycle through context_wrapper_ is broken in
66   // ServiceWorkerContextWrapper::Shutdown().
67   scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_;
68   base::Callback<bool(int)> increment_for_test_;
69   base::Callback<bool(int)> decrement_for_test_;
70
71   // Used to double-check that we don't access *this after it's destroyed.
72   base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_;
73   base::WeakPtr<ServiceWorkerProcessManager> weak_this_;
74 };
75
76 }  // namespace content
77
78 namespace base {
79 // Specialized to post the deletion to the UI thread.
80 template <>
81 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> {
82   void operator()(content::ServiceWorkerProcessManager* ptr) const;
83 };
84 }  // namespace base
85
86 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_