summaryrefslogtreecommitdiff
path: root/chromium/content/browser/cache_storage/cache_storage_manager.h
blob: 63a255bd785c618e9ce7fb04ac5cc223a5862c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_
#define CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_

#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted.h"
#include "content/browser/cache_storage/cache_storage.h"
#include "content/common/content_export.h"
#include "content/public/browser/cache_storage_context.h"
#include "content/public/browser/cache_storage_usage_info.h"
#include "net/url_request/url_request_context_getter.h"
#include "storage/browser/quota/quota_client.h"
#include "url/gurl.h"

namespace base {
class SequencedTaskRunner;
}

namespace storage {
class BlobStorageContext;
class QuotaManagerProxy;
}

namespace content {

class CacheStorageQuotaClient;

// Keeps track of a CacheStorage per origin. There is one
// CacheStorageManager per ServiceWorkerContextCore.
// TODO(jkarlin): Remove CacheStorage from memory once they're no
// longer in active use.
class CONTENT_EXPORT CacheStorageManager {
 public:
  static scoped_ptr<CacheStorageManager> Create(
      const base::FilePath& path,
      const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner,
      const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy);

  static scoped_ptr<CacheStorageManager> Create(
      CacheStorageManager* old_manager);

  // Map a database identifier (computed from an origin) to the path.
  static base::FilePath ConstructOriginPath(const base::FilePath& root_path,
                                            const GURL& origin);

  virtual ~CacheStorageManager();

  // Methods to support the CacheStorage spec. These methods call the
  // corresponding CacheStorage method on the appropriate thread.
  void OpenCache(const GURL& origin,
                 const std::string& cache_name,
                 const CacheStorage::CacheAndErrorCallback& callback);
  void HasCache(const GURL& origin,
                const std::string& cache_name,
                const CacheStorage::BoolAndErrorCallback& callback);
  void DeleteCache(const GURL& origin,
                   const std::string& cache_name,
                   const CacheStorage::BoolAndErrorCallback& callback);
  void EnumerateCaches(const GURL& origin,
                       const CacheStorage::StringsAndErrorCallback& callback);
  void MatchCache(const GURL& origin,
                  const std::string& cache_name,
                  scoped_ptr<ServiceWorkerFetchRequest> request,
                  const CacheStorageCache::ResponseCallback& callback);
  void MatchAllCaches(const GURL& origin,
                      scoped_ptr<ServiceWorkerFetchRequest> request,
                      const CacheStorageCache::ResponseCallback& callback);

  // This must be called before creating any of the public *Cache functions
  // above.
  void SetBlobParametersForCache(
      const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
      base::WeakPtr<storage::BlobStorageContext> blob_storage_context);

  base::WeakPtr<CacheStorageManager> AsWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  friend class CacheStorageContextImpl;
  friend class CacheStorageManagerTest;
  friend class CacheStorageMigrationTest;
  friend class CacheStorageQuotaClient;

  typedef std::map<GURL, CacheStorage*> CacheStorageMap;

  CacheStorageManager(
      const base::FilePath& path,
      const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner,
      const scoped_refptr<storage::QuotaManagerProxy>& quota_manager_proxy);

  // The returned CacheStorage* is owned by this manager.
  CacheStorage* FindOrCreateCacheStorage(const GURL& origin);

  // QuotaClient and Browsing Data Deletion support
  void GetAllOriginsUsage(
      const CacheStorageContext::GetUsageInfoCallback& callback);
  void GetOriginUsage(const GURL& origin_url,
                      const storage::QuotaClient::GetUsageCallback& callback);
  void GetOrigins(const storage::QuotaClient::GetOriginsCallback& callback);
  void GetOriginsForHost(
      const std::string& host,
      const storage::QuotaClient::GetOriginsCallback& callback);
  void DeleteOriginData(const GURL& origin,
                        const storage::QuotaClient::DeletionCallback& callback);
  void DeleteOriginData(const GURL& origin);
  static void DeleteOriginDidClose(
      const GURL& origin,
      const storage::QuotaClient::DeletionCallback& callback,
      scoped_ptr<CacheStorage> cache_storage,
      base::WeakPtr<CacheStorageManager> cache_manager);

  scoped_refptr<net::URLRequestContextGetter> url_request_context_getter()
      const {
    return request_context_getter_;
  }
  base::WeakPtr<storage::BlobStorageContext> blob_storage_context() const {
    return blob_context_;
  }
  base::FilePath root_path() const { return root_path_; }
  const scoped_refptr<base::SequencedTaskRunner>& cache_task_runner() const {
    return cache_task_runner_;
  }

  bool IsMemoryBacked() const { return root_path_.empty(); }

  // Map a origin to the path. Exposed for testing.
  static base::FilePath ConstructLegacyOriginPath(
      const base::FilePath& root_path,
      const GURL& origin);

  // Migrate from old origin-based path to storage identifier-based path.
  // TODO(jsbell); Remove method and all calls after a few releases.
  void MigrateOrigin(const GURL& origin);
  static void MigrateOriginOnTaskRunner(const base::FilePath& old_path,
                                        const base::FilePath& new_path);

  base::FilePath root_path_;
  scoped_refptr<base::SequencedTaskRunner> cache_task_runner_;

  scoped_refptr<storage::QuotaManagerProxy> quota_manager_proxy_;

  // The map owns the CacheStorages and the CacheStorages are only accessed on
  // |cache_task_runner_|.
  CacheStorageMap cache_storage_map_;

  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
  base::WeakPtr<storage::BlobStorageContext> blob_context_;

  base::WeakPtrFactory<CacheStorageManager> weak_ptr_factory_;
  DISALLOW_COPY_AND_ASSIGN(CacheStorageManager);
};

}  // namespace content

#endif  // CONTENT_BROWSER_CACHE_STORAGE_CACHE_STORAGE_MANAGER_H_