OLD | NEW |
| (Empty) |
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 #include "webkit/browser/quota/mock_quota_manager.h" | |
6 | |
7 #include "base/memory/ref_counted.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "url/gurl.h" | |
12 | |
13 namespace quota { | |
14 | |
15 MockQuotaManager::OriginInfo::OriginInfo( | |
16 const GURL& origin, | |
17 StorageType type, | |
18 int quota_client_mask, | |
19 base::Time modified) | |
20 : origin(origin), | |
21 type(type), | |
22 quota_client_mask(quota_client_mask), | |
23 modified(modified) { | |
24 } | |
25 | |
26 MockQuotaManager::OriginInfo::~OriginInfo() {} | |
27 | |
28 MockQuotaManager::StorageInfo::StorageInfo() : usage(0), quota(kint64max) {} | |
29 MockQuotaManager::StorageInfo::~StorageInfo() {} | |
30 | |
31 MockQuotaManager::MockQuotaManager( | |
32 bool is_incognito, | |
33 const base::FilePath& profile_path, | |
34 base::SingleThreadTaskRunner* io_thread, | |
35 base::SequencedTaskRunner* db_thread, | |
36 SpecialStoragePolicy* special_storage_policy) | |
37 : QuotaManager(is_incognito, profile_path, io_thread, db_thread, | |
38 special_storage_policy), | |
39 weak_factory_(this) { | |
40 } | |
41 | |
42 void MockQuotaManager::GetUsageAndQuota( | |
43 const GURL& origin, | |
44 quota::StorageType type, | |
45 const GetUsageAndQuotaCallback& callback) { | |
46 StorageInfo& info = usage_and_quota_map_[std::make_pair(origin, type)]; | |
47 callback.Run(quota::kQuotaStatusOk, info.usage, info.quota); | |
48 } | |
49 | |
50 void MockQuotaManager::SetQuota(const GURL& origin, StorageType type, | |
51 int64 quota) { | |
52 usage_and_quota_map_[std::make_pair(origin, type)].quota = quota; | |
53 } | |
54 | |
55 bool MockQuotaManager::AddOrigin( | |
56 const GURL& origin, | |
57 StorageType type, | |
58 int quota_client_mask, | |
59 base::Time modified) { | |
60 origins_.push_back(OriginInfo(origin, type, quota_client_mask, modified)); | |
61 return true; | |
62 } | |
63 | |
64 bool MockQuotaManager::OriginHasData( | |
65 const GURL& origin, | |
66 StorageType type, | |
67 QuotaClient::ID quota_client) const { | |
68 for (std::vector<OriginInfo>::const_iterator current = origins_.begin(); | |
69 current != origins_.end(); | |
70 ++current) { | |
71 if (current->origin == origin && | |
72 current->type == type && | |
73 current->quota_client_mask & quota_client) | |
74 return true; | |
75 } | |
76 return false; | |
77 } | |
78 | |
79 void MockQuotaManager::GetOriginsModifiedSince( | |
80 StorageType type, | |
81 base::Time modified_since, | |
82 const GetOriginsCallback& callback) { | |
83 std::set<GURL>* origins_to_return = new std::set<GURL>(); | |
84 for (std::vector<OriginInfo>::const_iterator current = origins_.begin(); | |
85 current != origins_.end(); | |
86 ++current) { | |
87 if (current->type == type && current->modified >= modified_since) | |
88 origins_to_return->insert(current->origin); | |
89 } | |
90 | |
91 base::MessageLoop::current()->PostTask( | |
92 FROM_HERE, | |
93 base::Bind(&MockQuotaManager::DidGetModifiedSince, | |
94 weak_factory_.GetWeakPtr(), | |
95 callback, | |
96 base::Owned(origins_to_return), | |
97 type)); | |
98 } | |
99 | |
100 void MockQuotaManager::DeleteOriginData( | |
101 const GURL& origin, | |
102 StorageType type, | |
103 int quota_client_mask, | |
104 const StatusCallback& callback) { | |
105 for (std::vector<OriginInfo>::iterator current = origins_.begin(); | |
106 current != origins_.end(); | |
107 ++current) { | |
108 if (current->origin == origin && current->type == type) { | |
109 // Modify the mask: if it's 0 after "deletion", remove the origin. | |
110 current->quota_client_mask &= ~quota_client_mask; | |
111 if (current->quota_client_mask == 0) | |
112 origins_.erase(current); | |
113 break; | |
114 } | |
115 } | |
116 | |
117 base::MessageLoop::current()->PostTask( | |
118 FROM_HERE, | |
119 base::Bind(&MockQuotaManager::DidDeleteOriginData, | |
120 weak_factory_.GetWeakPtr(), | |
121 callback, | |
122 kQuotaStatusOk)); | |
123 } | |
124 | |
125 MockQuotaManager::~MockQuotaManager() {} | |
126 | |
127 void MockQuotaManager::UpdateUsage( | |
128 const GURL& origin, StorageType type, int64 delta) { | |
129 usage_and_quota_map_[std::make_pair(origin, type)].usage += delta; | |
130 } | |
131 | |
132 void MockQuotaManager::DidGetModifiedSince( | |
133 const GetOriginsCallback& callback, | |
134 std::set<GURL>* origins, | |
135 StorageType storage_type) { | |
136 callback.Run(*origins, storage_type); | |
137 } | |
138 | |
139 void MockQuotaManager::DidDeleteOriginData( | |
140 const StatusCallback& callback, | |
141 QuotaStatusCode status) { | |
142 callback.Run(status); | |
143 } | |
144 | |
145 } // namespace quota | |
OLD | NEW |