OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "content/browser/in_process_webkit/indexed_db_quota_client.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/file_util.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "content/browser/in_process_webkit/indexed_db_context.h" |
| 12 #include "net/base/net_util.h" |
| 13 #include "webkit/database/database_util.h" |
| 14 |
| 15 using quota::QuotaClient; |
| 16 |
| 17 |
| 18 // Helper tasks --------------------------------------------------------------- |
| 19 |
| 20 class IndexedDBQuotaClient::HelperTask : public quota::QuotaThreadTask { |
| 21 protected: |
| 22 HelperTask( |
| 23 IndexedDBQuotaClient* client, |
| 24 scoped_refptr<base::MessageLoopProxy> webkit_thread_message_loop) |
| 25 : QuotaThreadTask(client, webkit_thread_message_loop), |
| 26 client_(client), indexed_db_context_(client->indexed_db_context_) { |
| 27 } |
| 28 |
| 29 IndexedDBQuotaClient* client_; |
| 30 scoped_refptr<IndexedDBContext> indexed_db_context_; |
| 31 }; |
| 32 |
| 33 class IndexedDBQuotaClient::GetOriginUsageTask : public HelperTask { |
| 34 public: |
| 35 GetOriginUsageTask( |
| 36 IndexedDBQuotaClient* client, |
| 37 scoped_refptr<base::MessageLoopProxy> webkit_thread_message_loop, |
| 38 const GURL& origin_url) |
| 39 : HelperTask(client, webkit_thread_message_loop), |
| 40 origin_url_(origin_url), usage_(0) { |
| 41 } |
| 42 |
| 43 private: |
| 44 virtual void RunOnTargetThread() OVERRIDE { |
| 45 string16 origin_id = |
| 46 webkit_database::DatabaseUtil::GetOriginIdentifier(origin_url_); |
| 47 FilePath filePath = indexed_db_context_->GetIndexedDBFilePath(origin_id); |
| 48 if (!file_util::AbsolutePath(&filePath)) { |
| 49 LOG(ERROR) << "Failed to convert " << filePath.value() |
| 50 << " to absolute path"; |
| 51 } |
| 52 usage_ = 999; // Arbitrary number that might be recognizable as an error. |
| 53 if (!file_util::GetFileSize(filePath, &usage_)) { |
| 54 LOG(ERROR) << "Failed to get file size for " << filePath.value(); |
| 55 } |
| 56 } |
| 57 virtual void Completed() OVERRIDE { |
| 58 client_->DidGetOriginUsage(origin_url_, usage_); |
| 59 } |
| 60 GURL origin_url_; |
| 61 int64 usage_; |
| 62 }; |
| 63 |
| 64 class IndexedDBQuotaClient::GetOriginsTaskBase : public HelperTask { |
| 65 protected: |
| 66 GetOriginsTaskBase( |
| 67 IndexedDBQuotaClient* client, |
| 68 scoped_refptr<base::MessageLoopProxy> webkit_thread_message_loop) |
| 69 : HelperTask(client, webkit_thread_message_loop) { |
| 70 } |
| 71 |
| 72 virtual bool ShouldAddOrigin(const GURL& origin) = 0; |
| 73 |
| 74 virtual void RunOnTargetThread() OVERRIDE { |
| 75 // TODO(dgrogan): Implement. |
| 76 } |
| 77 |
| 78 std::set<GURL> origins_; |
| 79 }; |
| 80 |
| 81 class IndexedDBQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase { |
| 82 public: |
| 83 GetAllOriginsTask( |
| 84 IndexedDBQuotaClient* client, |
| 85 scoped_refptr<base::MessageLoopProxy> webkit_thread_message_loop) |
| 86 : GetOriginsTaskBase(client, webkit_thread_message_loop) { |
| 87 } |
| 88 |
| 89 protected: |
| 90 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { |
| 91 return true; |
| 92 } |
| 93 virtual void Completed() OVERRIDE { |
| 94 client_->DidGetAllOrigins(origins_); |
| 95 } |
| 96 }; |
| 97 |
| 98 class IndexedDBQuotaClient::GetOriginsForHostTask : public GetOriginsTaskBase { |
| 99 public: |
| 100 GetOriginsForHostTask( |
| 101 IndexedDBQuotaClient* client, |
| 102 scoped_refptr<base::MessageLoopProxy> webkit_thread_message_loop, |
| 103 const std::string& host) |
| 104 : GetOriginsTaskBase(client, webkit_thread_message_loop), |
| 105 host_(host) { |
| 106 } |
| 107 |
| 108 private: |
| 109 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { |
| 110 return host_ == net::GetHostOrSpecFromURL(origin); |
| 111 } |
| 112 virtual void Completed() OVERRIDE { |
| 113 client_->DidGetOriginsForHost(host_, origins_); |
| 114 } |
| 115 std::string host_; |
| 116 }; |
| 117 |
| 118 // IndexedDBQuotaClient -------------------------------------------------------- |
| 119 |
| 120 IndexedDBQuotaClient::IndexedDBQuotaClient( |
| 121 base::MessageLoopProxy* webkit_thread_message_loop, |
| 122 IndexedDBContext* indexed_db_context) |
| 123 : webkit_thread_message_loop_(webkit_thread_message_loop), |
| 124 indexed_db_context_(indexed_db_context) { |
| 125 } |
| 126 |
| 127 IndexedDBQuotaClient::~IndexedDBQuotaClient() { |
| 128 } |
| 129 |
| 130 QuotaClient::ID IndexedDBQuotaClient::id() const { |
| 131 return kIndexedDatabase; |
| 132 } |
| 133 |
| 134 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { |
| 135 delete this; |
| 136 } |
| 137 |
| 138 void IndexedDBQuotaClient::GetOriginUsage( |
| 139 const GURL& origin_url, |
| 140 quota::StorageType type, |
| 141 GetUsageCallback* callback_ptr) { |
| 142 DCHECK(callback_ptr); |
| 143 DCHECK(indexed_db_context_.get()); |
| 144 scoped_ptr<GetUsageCallback> callback(callback_ptr); |
| 145 |
| 146 // IndexedDB is in the temp namespace for now. |
| 147 if (type != quota::kStorageTypeTemporary) { |
| 148 callback->Run(0); |
| 149 return; |
| 150 } |
| 151 |
| 152 if (usage_for_origin_callbacks_.Add(origin_url, callback.release())) { |
| 153 scoped_refptr<GetOriginUsageTask> task( |
| 154 new GetOriginUsageTask(this, webkit_thread_message_loop_, origin_url)); |
| 155 task->Start(); |
| 156 } |
| 157 } |
| 158 |
| 159 void IndexedDBQuotaClient::GetOriginsForType( |
| 160 quota::StorageType type, |
| 161 GetOriginsCallback* callback_ptr) { |
| 162 DCHECK(callback_ptr); |
| 163 DCHECK(indexed_db_context_.get()); |
| 164 scoped_ptr<GetOriginsCallback> callback(callback_ptr); |
| 165 |
| 166 // All databases are in the temp namespace for now. |
| 167 if (type != quota::kStorageTypeTemporary) { |
| 168 callback->Run(std::set<GURL>()); |
| 169 return; |
| 170 } |
| 171 |
| 172 if (origins_for_type_callbacks_.Add(callback.release())) { |
| 173 scoped_refptr<GetAllOriginsTask> task( |
| 174 new GetAllOriginsTask(this, webkit_thread_message_loop_)); |
| 175 task->Start(); |
| 176 } |
| 177 } |
| 178 |
| 179 void IndexedDBQuotaClient::GetOriginsForHost( |
| 180 quota::StorageType type, |
| 181 const std::string& host, |
| 182 GetOriginsCallback* callback_ptr) { |
| 183 DCHECK(callback_ptr); |
| 184 DCHECK(indexed_db_context_.get()); |
| 185 scoped_ptr<GetOriginsCallback> callback(callback_ptr); |
| 186 |
| 187 // All databases are in the temp namespace for now. |
| 188 if (type != quota::kStorageTypeTemporary) { |
| 189 callback->Run(std::set<GURL>()); |
| 190 return; |
| 191 } |
| 192 |
| 193 if (origins_for_host_callbacks_.Add(host, callback.release())) { |
| 194 scoped_refptr<GetOriginsForHostTask> task( |
| 195 new GetOriginsForHostTask(this, webkit_thread_message_loop_, host)); |
| 196 task->Start(); |
| 197 } |
| 198 } |
| 199 |
| 200 void IndexedDBQuotaClient::DeleteOriginData(const GURL& origin, |
| 201 quota::StorageType type, |
| 202 DeletionCallback* callback) { |
| 203 // TODO(tzik): implement me |
| 204 callback->Run(quota::kQuotaErrorNotSupported); |
| 205 delete callback; |
| 206 } |
| 207 |
| 208 void IndexedDBQuotaClient::DidGetOriginUsage( |
| 209 const GURL& origin_url, int64 usage) { |
| 210 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); |
| 211 usage_for_origin_callbacks_.Run(origin_url, usage); |
| 212 } |
| 213 |
| 214 void IndexedDBQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { |
| 215 DCHECK(origins_for_type_callbacks_.HasCallbacks()); |
| 216 origins_for_type_callbacks_.Run(origins); |
| 217 } |
| 218 |
| 219 void IndexedDBQuotaClient::DidGetOriginsForHost( |
| 220 const std::string& host, const std::set<GURL>& origins) { |
| 221 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); |
| 222 origins_for_host_callbacks_.Run(host, origins); |
| 223 } |
OLD | NEW |