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 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 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 file_path = indexed_db_context_->GetIndexedDBFilePath(origin_id); |
| 48 usage_ = 0; |
| 49 if (!file_util::GetFileSize(file_path, &usage_)) { |
| 50 LOG(ERROR) << "Failed to get file size for " << file_path.value(); |
| 51 } |
| 52 } |
| 53 virtual void Completed() OVERRIDE { |
| 54 client_->DidGetOriginUsage(origin_url_, usage_); |
| 55 } |
| 56 GURL origin_url_; |
| 57 int64 usage_; |
| 58 }; |
| 59 |
| 60 class IndexedDBQuotaClient::GetOriginsTaskBase : public HelperTask { |
| 61 protected: |
| 62 GetOriginsTaskBase( |
| 63 IndexedDBQuotaClient* client, |
| 64 base::MessageLoopProxy* webkit_thread_message_loop) |
| 65 : HelperTask(client, webkit_thread_message_loop) { |
| 66 } |
| 67 |
| 68 virtual bool ShouldAddOrigin(const GURL& origin) = 0; |
| 69 |
| 70 virtual void RunOnTargetThread() OVERRIDE { |
| 71 // TODO(dgrogan): Implement. |
| 72 } |
| 73 |
| 74 std::set<GURL> origins_; |
| 75 }; |
| 76 |
| 77 class IndexedDBQuotaClient::GetAllOriginsTask : public GetOriginsTaskBase { |
| 78 public: |
| 79 GetAllOriginsTask( |
| 80 IndexedDBQuotaClient* client, |
| 81 base::MessageLoopProxy* webkit_thread_message_loop) |
| 82 : GetOriginsTaskBase(client, webkit_thread_message_loop) { |
| 83 } |
| 84 |
| 85 protected: |
| 86 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { |
| 87 return true; |
| 88 } |
| 89 virtual void Completed() OVERRIDE { |
| 90 client_->DidGetAllOrigins(origins_); |
| 91 } |
| 92 }; |
| 93 |
| 94 class IndexedDBQuotaClient::GetOriginsForHostTask : public GetOriginsTaskBase { |
| 95 public: |
| 96 GetOriginsForHostTask( |
| 97 IndexedDBQuotaClient* client, |
| 98 base::MessageLoopProxy* webkit_thread_message_loop, |
| 99 const std::string& host) |
| 100 : GetOriginsTaskBase(client, webkit_thread_message_loop), |
| 101 host_(host) { |
| 102 } |
| 103 |
| 104 private: |
| 105 virtual bool ShouldAddOrigin(const GURL& origin) OVERRIDE { |
| 106 return host_ == net::GetHostOrSpecFromURL(origin); |
| 107 } |
| 108 virtual void Completed() OVERRIDE { |
| 109 client_->DidGetOriginsForHost(host_, origins_); |
| 110 } |
| 111 std::string host_; |
| 112 }; |
| 113 |
| 114 // IndexedDBQuotaClient -------------------------------------------------------- |
| 115 |
| 116 IndexedDBQuotaClient::IndexedDBQuotaClient( |
| 117 base::MessageLoopProxy* webkit_thread_message_loop, |
| 118 IndexedDBContext* indexed_db_context) |
| 119 : webkit_thread_message_loop_(webkit_thread_message_loop), |
| 120 indexed_db_context_(indexed_db_context) { |
| 121 } |
| 122 |
| 123 IndexedDBQuotaClient::~IndexedDBQuotaClient() { |
| 124 } |
| 125 |
| 126 QuotaClient::ID IndexedDBQuotaClient::id() const { |
| 127 return kIndexedDatabase; |
| 128 } |
| 129 |
| 130 void IndexedDBQuotaClient::OnQuotaManagerDestroyed() { |
| 131 delete this; |
| 132 } |
| 133 |
| 134 void IndexedDBQuotaClient::GetOriginUsage( |
| 135 const GURL& origin_url, |
| 136 quota::StorageType type, |
| 137 GetUsageCallback* callback_ptr) { |
| 138 DCHECK(callback_ptr); |
| 139 DCHECK(indexed_db_context_.get()); |
| 140 scoped_ptr<GetUsageCallback> callback(callback_ptr); |
| 141 |
| 142 // IndexedDB is in the temp namespace for now. |
| 143 if (type != quota::kStorageTypeTemporary) { |
| 144 callback->Run(0); |
| 145 return; |
| 146 } |
| 147 |
| 148 if (usage_for_origin_callbacks_.Add(origin_url, callback.release())) { |
| 149 scoped_refptr<GetOriginUsageTask> task( |
| 150 new GetOriginUsageTask(this, webkit_thread_message_loop_, origin_url)); |
| 151 task->Start(); |
| 152 } |
| 153 } |
| 154 |
| 155 void IndexedDBQuotaClient::GetOriginsForType( |
| 156 quota::StorageType type, |
| 157 GetOriginsCallback* callback_ptr) { |
| 158 DCHECK(callback_ptr); |
| 159 DCHECK(indexed_db_context_.get()); |
| 160 scoped_ptr<GetOriginsCallback> callback(callback_ptr); |
| 161 |
| 162 // All databases are in the temp namespace for now. |
| 163 if (type != quota::kStorageTypeTemporary) { |
| 164 callback->Run(std::set<GURL>()); |
| 165 return; |
| 166 } |
| 167 |
| 168 if (origins_for_type_callbacks_.Add(callback.release())) { |
| 169 scoped_refptr<GetAllOriginsTask> task( |
| 170 new GetAllOriginsTask(this, webkit_thread_message_loop_)); |
| 171 task->Start(); |
| 172 } |
| 173 } |
| 174 |
| 175 void IndexedDBQuotaClient::GetOriginsForHost( |
| 176 quota::StorageType type, |
| 177 const std::string& host, |
| 178 GetOriginsCallback* callback_ptr) { |
| 179 DCHECK(callback_ptr); |
| 180 DCHECK(indexed_db_context_.get()); |
| 181 scoped_ptr<GetOriginsCallback> callback(callback_ptr); |
| 182 |
| 183 // All databases are in the temp namespace for now. |
| 184 if (type != quota::kStorageTypeTemporary) { |
| 185 callback->Run(std::set<GURL>()); |
| 186 return; |
| 187 } |
| 188 |
| 189 if (origins_for_host_callbacks_.Add(host, callback.release())) { |
| 190 scoped_refptr<GetOriginsForHostTask> task( |
| 191 new GetOriginsForHostTask(this, webkit_thread_message_loop_, host)); |
| 192 task->Start(); |
| 193 } |
| 194 } |
| 195 |
| 196 void IndexedDBQuotaClient::DeleteOriginData(const GURL& origin, |
| 197 quota::StorageType type, |
| 198 DeletionCallback* callback) { |
| 199 // TODO(tzik): implement me |
| 200 callback->Run(quota::kQuotaErrorNotSupported); |
| 201 delete callback; |
| 202 } |
| 203 |
| 204 void IndexedDBQuotaClient::DidGetOriginUsage( |
| 205 const GURL& origin_url, int64 usage) { |
| 206 DCHECK(usage_for_origin_callbacks_.HasCallbacks(origin_url)); |
| 207 usage_for_origin_callbacks_.Run(origin_url, usage); |
| 208 } |
| 209 |
| 210 void IndexedDBQuotaClient::DidGetAllOrigins(const std::set<GURL>& origins) { |
| 211 DCHECK(origins_for_type_callbacks_.HasCallbacks()); |
| 212 origins_for_type_callbacks_.Run(origins); |
| 213 } |
| 214 |
| 215 void IndexedDBQuotaClient::DidGetOriginsForHost( |
| 216 const std::string& host, const std::set<GURL>& origins) { |
| 217 DCHECK(origins_for_host_callbacks_.HasCallbacks(host)); |
| 218 origins_for_host_callbacks_.Run(host, origins); |
| 219 } |
OLD | NEW |