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_PUBLIC_TEST_MOCK_STORAGE_CLIENT_H_
6#define CONTENT_PUBLIC_TEST_MOCK_STORAGE_CLIENT_H_
7
8#include <map>
9#include <set>
10#include <string>
11#include <utility>
12
13#include "base/compiler_specific.h"
14#include "base/memory/weak_ptr.h"
15#include "base/time/time.h"
16#include "storage/browser/quota/quota_client.h"
17#include "url/gurl.h"
18
19namespace storage {
20class QuotaManagerProxy;
21}
22
23using storage::QuotaClient;
24using storage::QuotaManagerProxy;
25using storage::StorageType;
26
27namespace content {
28
29struct MockOriginData {
30  const char* origin;
31  StorageType type;
32  int64 usage;
33};
34
35// Mock storage class for testing.
36class MockStorageClient : public QuotaClient {
37 public:
38  MockStorageClient(QuotaManagerProxy* quota_manager_proxy,
39                    const MockOriginData* mock_data,
40                    QuotaClient::ID id,
41                    size_t mock_data_size);
42  virtual ~MockStorageClient();
43
44  // To add or modify mock data in this client.
45  void AddOriginAndNotify(
46      const GURL& origin_url, StorageType type, int64 size);
47  void ModifyOriginAndNotify(
48      const GURL& origin_url, StorageType type, int64 delta);
49  void TouchAllOriginsAndNotify();
50
51  void AddOriginToErrorSet(const GURL& origin_url, StorageType type);
52
53  base::Time IncrementMockTime();
54
55  // QuotaClient methods.
56  virtual QuotaClient::ID id() const OVERRIDE;
57  virtual void OnQuotaManagerDestroyed() OVERRIDE;
58  virtual void GetOriginUsage(const GURL& origin_url,
59                              StorageType type,
60                              const GetUsageCallback& callback) OVERRIDE;
61  virtual void GetOriginsForType(StorageType type,
62                                 const GetOriginsCallback& callback) OVERRIDE;
63  virtual void GetOriginsForHost(StorageType type, const std::string& host,
64                                 const GetOriginsCallback& callback) OVERRIDE;
65  virtual void DeleteOriginData(const GURL& origin,
66                                StorageType type,
67                                const DeletionCallback& callback) OVERRIDE;
68  virtual bool DoesSupport(storage::StorageType type) const OVERRIDE;
69
70 private:
71  void RunGetOriginUsage(const GURL& origin_url,
72                         StorageType type,
73                         const GetUsageCallback& callback);
74  void RunGetOriginsForType(StorageType type,
75                            const GetOriginsCallback& callback);
76  void RunGetOriginsForHost(StorageType type,
77                            const std::string& host,
78                            const GetOriginsCallback& callback);
79  void RunDeleteOriginData(const GURL& origin_url,
80                           StorageType type,
81                           const DeletionCallback& callback);
82
83  void Populate(const MockOriginData* mock_data, size_t mock_data_size);
84
85  scoped_refptr<QuotaManagerProxy> quota_manager_proxy_;
86  const ID id_;
87
88  typedef std::map<std::pair<GURL, StorageType>, int64> OriginDataMap;
89  OriginDataMap origin_data_;
90  typedef std::set<std::pair<GURL, StorageType> > ErrorOriginSet;
91  ErrorOriginSet error_origins_;
92
93  int mock_time_counter_;
94
95  base::WeakPtrFactory<MockStorageClient> weak_factory_;
96
97  DISALLOW_COPY_AND_ASSIGN(MockStorageClient);
98};
99
100}  // namespace content
101
102#endif  // CONTENT_PUBLIC_TEST_MOCK_STORAGE_CLIENT_H_
103