1f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// found in the LICENSE file.
4f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
5f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
6f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
7f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
85c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include <vector>
95c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu
10f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/files/file_path.h"
11f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/ref_counted.h"
12f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
13effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/browser/service_worker/service_worker_context_core.h"
14f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "content/common/content_export.h"
15effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "content/public/browser/service_worker_context.h"
16f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
17f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace base {
18f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class FilePath;
19010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class SequencedTaskRunner;
2003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)class SingleThreadTaskRunner;
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
22f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)namespace net {
246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class URLRequestContextGetter;
256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)namespace storage {
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class QuotaManagerProxy;
29f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
31f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)namespace content {
32f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
335c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass BrowserContext;
346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class ChromeBlobStorageContext;
355c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liuclass ServiceWorkerContextCore;
360529e5d033099cbfc42635f6f6183833b09dff6eBen Murdochclass ServiceWorkerContextObserver;
37f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
38f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// A refcounted wrapper class for our core object. Higher level content lib
39f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// classes keep references to this class on mutliple threads. The inner core
40f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// instance is strictly single threaded and is not refcounted, the core object
41f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)// is what is used internally in the service worker lib.
42f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)class CONTENT_EXPORT ServiceWorkerContextWrapper
43f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    : NON_EXPORTED_BASE(public ServiceWorkerContext),
44f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      public base::RefCountedThreadSafe<ServiceWorkerContextWrapper> {
45f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) public:
465c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  ServiceWorkerContextWrapper(BrowserContext* browser_context);
47f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
48f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Init and Shutdown are for use on the UI thread when the profile,
49f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // storagepartition is being setup and torn down.
50f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Init(const base::FilePath& user_data_directory,
5103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)            storage::QuotaManagerProxy* quota_manager_proxy);
52f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Shutdown();
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Deletes all files on disk and restarts the system asynchronously. This
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // leaves the system in a disabled state until it's done. This should be
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // called on the IO thread.
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void DeleteAndStartOver();
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // The core context is only for use on the IO thread.
60f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  ServiceWorkerContextCore* context();
61f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // The process manager can be used on either UI or IO.
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ServiceWorkerProcessManager* process_manager() {
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return process_manager_.get();
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
67effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  // ServiceWorkerContext implementation:
685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  virtual void RegisterServiceWorker(
695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const GURL& pattern,
705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const GURL& script_url,
715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      const ResultCallback& continuation) OVERRIDE;
72effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  virtual void UnregisterServiceWorker(const GURL& pattern,
73effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch                                       const ResultCallback& continuation)
74effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch      OVERRIDE;
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  virtual void Terminate() OVERRIDE;
766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void GetAllOriginsInfo(const GetUsageInfoCallback& callback) OVERRIDE;
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  virtual void DeleteForOrigin(const GURL& origin_url) OVERRIDE;
78effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch
790529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void AddObserver(ServiceWorkerContextObserver* observer);
800529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void RemoveObserver(ServiceWorkerContextObserver* observer);
810529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_incognito() const { return is_incognito_; }
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // The URLRequestContext doesn't exist until after the StoragePartition is
856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // made (which is after this object is made). This function must be called
866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // after this object is created but before any ServiceWorkerCache operations.
876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // It must be called on the IO thread. If either parameter is NULL the
886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // function immediately returns without forwarding to the
896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // ServiceWorkerCacheStorageManager.
906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void SetBlobParametersForCache(
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      net::URLRequestContextGetter* request_context,
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      ChromeBlobStorageContext* blob_storage_context);
936e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) private:
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  friend class base::RefCountedThreadSafe<ServiceWorkerContextWrapper>;
96010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  friend class EmbeddedWorkerTestHelper;
975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  friend class ServiceWorkerProcessManager;
98f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  virtual ~ServiceWorkerContextWrapper();
99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
10003b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)  void InitInternal(
10103b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const base::FilePath& user_data_directory,
10203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const scoped_refptr<base::SequencedTaskRunner>& stores_task_runner,
10303b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const scoped_refptr<base::SequencedTaskRunner>& database_task_runner,
10403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      const scoped_refptr<base::SingleThreadTaskRunner>& disk_cache_thread,
10503b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)      storage::QuotaManagerProxy* quota_manager_proxy);
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  void ShutdownOnIO();
107010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
108116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void DidDeleteAndStartOver(ServiceWorkerStatusCode status);
109116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
1106e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void DidGetAllRegistrationsForGetAllOrigins(
1116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const GetUsageInfoCallback& callback,
1126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const std::vector<ServiceWorkerRegistrationInfo>& registrations);
1136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  void DidGetAllRegistrationsForDeleteForOrigin(
1146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const GURL& origin,
1156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      const std::vector<ServiceWorkerRegistrationInfo>& registrations);
1166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
1175c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  const scoped_refptr<ObserverListThreadSafe<ServiceWorkerContextObserver> >
1180529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch      observer_list_;
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const scoped_ptr<ServiceWorkerProcessManager> process_manager_;
1205c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  // Cleared in Shutdown():
1215c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu  scoped_ptr<ServiceWorkerContextCore> context_core_;
122116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
123116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Initialized in Init(); true of the user data directory is empty.
124116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  bool is_incognito_;
125f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)};
126f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
127f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}  // namespace content
128f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
129f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_CONTEXT_WRAPPER_H_
130