service_worker_context_wrapper.h revision 116680a4aac90f2aa7413d9095a592090648e557
1// Copyright 2013 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_CONTEXT_WRAPPER_H_
6#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
7
8#include <vector>
9
10#include "base/files/file_path.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_ptr.h"
13#include "content/browser/service_worker/service_worker_context_core.h"
14#include "content/common/content_export.h"
15#include "content/public/browser/service_worker_context.h"
16
17namespace base {
18class FilePath;
19class MessageLoopProxy;
20class SequencedTaskRunner;
21}
22
23namespace quota {
24class QuotaManagerProxy;
25}
26
27namespace content {
28
29class BrowserContext;
30class ServiceWorkerContextCore;
31class ServiceWorkerContextObserver;
32
33// A refcounted wrapper class for our core object. Higher level content lib
34// classes keep references to this class on mutliple threads. The inner core
35// instance is strictly single threaded and is not refcounted, the core object
36// is what is used internally in the service worker lib.
37class CONTENT_EXPORT ServiceWorkerContextWrapper
38    : NON_EXPORTED_BASE(public ServiceWorkerContext),
39      public base::RefCountedThreadSafe<ServiceWorkerContextWrapper> {
40 public:
41  ServiceWorkerContextWrapper(BrowserContext* browser_context);
42
43  // Init and Shutdown are for use on the UI thread when the profile,
44  // storagepartition is being setup and torn down.
45  void Init(const base::FilePath& user_data_directory,
46            quota::QuotaManagerProxy* quota_manager_proxy);
47  void Shutdown();
48
49  // Deletes all files on disk and restarts the system asynchronously. This
50  // leaves the system in a disabled state until it's done. This should be
51  // called on the IO thread.
52  void DeleteAndStartOver();
53
54  // The core context is only for use on the IO thread.
55  ServiceWorkerContextCore* context();
56
57  // The process manager can be used on either UI or IO.
58  ServiceWorkerProcessManager* process_manager() {
59    return process_manager_.get();
60  }
61
62  // ServiceWorkerContext implementation:
63  virtual void RegisterServiceWorker(
64      const GURL& pattern,
65      const GURL& script_url,
66      const ResultCallback& continuation) OVERRIDE;
67  virtual void UnregisterServiceWorker(const GURL& pattern,
68                                       const ResultCallback& continuation)
69      OVERRIDE;
70  virtual void Terminate() OVERRIDE;
71
72  void AddObserver(ServiceWorkerContextObserver* observer);
73  void RemoveObserver(ServiceWorkerContextObserver* observer);
74
75  bool is_incognito() const { return is_incognito_; }
76
77 private:
78  friend class base::RefCountedThreadSafe<ServiceWorkerContextWrapper>;
79  friend class EmbeddedWorkerTestHelper;
80  friend class ServiceWorkerProcessManager;
81  virtual ~ServiceWorkerContextWrapper();
82
83  void InitInternal(const base::FilePath& user_data_directory,
84                    base::SequencedTaskRunner* database_task_runner,
85                    base::MessageLoopProxy* disk_cache_thread,
86                    quota::QuotaManagerProxy* quota_manager_proxy);
87  void ShutdownOnIO();
88
89  void DidDeleteAndStartOver(ServiceWorkerStatusCode status);
90
91  const scoped_refptr<ObserverListThreadSafe<ServiceWorkerContextObserver> >
92      observer_list_;
93  const scoped_ptr<ServiceWorkerProcessManager> process_manager_;
94  // Cleared in Shutdown():
95  scoped_ptr<ServiceWorkerContextCore> context_core_;
96
97  // Initialized in Init(); true of the user data directory is empty.
98  bool is_incognito_;
99};
100
101}  // namespace content
102
103#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
104