| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api.h" | 5 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 sync_file_system::SyncStatusCodeToString(code), | 61 sync_file_system::SyncStatusCodeToString(code), |
| 62 static_cast<int>(code)); | 62 static_cast<int>(code)); |
| 63 } | 63 } |
| 64 | 64 |
| 65 } // namespace | 65 } // namespace |
| 66 | 66 |
| 67 bool SyncFileSystemDeleteFileSystemFunction::RunAsync() { | 67 bool SyncFileSystemDeleteFileSystemFunction::RunAsync() { |
| 68 std::string url; | 68 std::string url; |
| 69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 69 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 70 | 70 |
| 71 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 71 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 72 BrowserContext::GetStoragePartition(GetProfile(), | 72 BrowserContext::GetStoragePartition(GetProfile(), |
| 73 render_view_host()->GetSiteInstance()) | 73 render_view_host()->GetSiteInstance()) |
| 74 ->GetFileSystemContext(); | 74 ->GetFileSystemContext(); |
| 75 fileapi::FileSystemURL file_system_url( | 75 storage::FileSystemURL file_system_url( |
| 76 file_system_context->CrackURL(GURL(url))); | 76 file_system_context->CrackURL(GURL(url))); |
| 77 | 77 |
| 78 BrowserThread::PostTask( | 78 BrowserThread::PostTask( |
| 79 BrowserThread::IO, | 79 BrowserThread::IO, |
| 80 FROM_HERE, | 80 FROM_HERE, |
| 81 Bind(&fileapi::FileSystemContext::DeleteFileSystem, | 81 Bind(&storage::FileSystemContext::DeleteFileSystem, |
| 82 file_system_context, | 82 file_system_context, |
| 83 source_url().GetOrigin(), | 83 source_url().GetOrigin(), |
| 84 file_system_url.type(), | 84 file_system_url.type(), |
| 85 Bind(&SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem, | 85 Bind(&SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem, |
| 86 this))); | 86 this))); |
| 87 return true; | 87 return true; |
| 88 } | 88 } |
| 89 | 89 |
| 90 void SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem( | 90 void SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem( |
| 91 base::File::Error error) { | 91 base::File::Error error) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 113 } | 113 } |
| 114 | 114 |
| 115 bool SyncFileSystemRequestFileSystemFunction::RunAsync() { | 115 bool SyncFileSystemRequestFileSystemFunction::RunAsync() { |
| 116 // SyncFileSystem initialization is done in OpenFileSystem below, but we call | 116 // SyncFileSystem initialization is done in OpenFileSystem below, but we call |
| 117 // GetSyncFileSystemService here too to initialize sync event observer for | 117 // GetSyncFileSystemService here too to initialize sync event observer for |
| 118 // extensions API. | 118 // extensions API. |
| 119 GetSyncFileSystemService(GetProfile()); | 119 GetSyncFileSystemService(GetProfile()); |
| 120 | 120 |
| 121 // Initializes sync context for this extension and continue to open | 121 // Initializes sync context for this extension and continue to open |
| 122 // a new file system. | 122 // a new file system. |
| 123 BrowserThread::PostTask( | 123 BrowserThread::PostTask(BrowserThread::IO, |
| 124 BrowserThread::IO, FROM_HERE, | 124 FROM_HERE, |
| 125 Bind(&fileapi::FileSystemContext::OpenFileSystem, | 125 Bind(&storage::FileSystemContext::OpenFileSystem, |
| 126 GetFileSystemContext(), | 126 GetFileSystemContext(), |
| 127 source_url().GetOrigin(), | 127 source_url().GetOrigin(), |
| 128 fileapi::kFileSystemTypeSyncable, | 128 storage::kFileSystemTypeSyncable, |
| 129 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 129 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 130 base::Bind(&self::DidOpenFileSystem, this))); | 130 base::Bind(&self::DidOpenFileSystem, this))); |
| 131 return true; | 131 return true; |
| 132 } | 132 } |
| 133 | 133 |
| 134 fileapi::FileSystemContext* | 134 storage::FileSystemContext* |
| 135 SyncFileSystemRequestFileSystemFunction::GetFileSystemContext() { | 135 SyncFileSystemRequestFileSystemFunction::GetFileSystemContext() { |
| 136 DCHECK(render_view_host()); | 136 DCHECK(render_view_host()); |
| 137 return BrowserContext::GetStoragePartition( | 137 return BrowserContext::GetStoragePartition( |
| 138 GetProfile(), render_view_host()->GetSiteInstance()) | 138 GetProfile(), render_view_host()->GetSiteInstance()) |
| 139 ->GetFileSystemContext(); | 139 ->GetFileSystemContext(); |
| 140 } | 140 } |
| 141 | 141 |
| 142 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem( | 142 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem( |
| 143 const GURL& root_url, | 143 const GURL& root_url, |
| 144 const std::string& file_system_name, | 144 const std::string& file_system_name, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 164 SetResult(dict); | 164 SetResult(dict); |
| 165 dict->SetString("name", file_system_name); | 165 dict->SetString("name", file_system_name); |
| 166 dict->SetString("root", root_url.spec()); | 166 dict->SetString("root", root_url.spec()); |
| 167 SendResponse(true); | 167 SendResponse(true); |
| 168 } | 168 } |
| 169 | 169 |
| 170 bool SyncFileSystemGetFileStatusFunction::RunAsync() { | 170 bool SyncFileSystemGetFileStatusFunction::RunAsync() { |
| 171 std::string url; | 171 std::string url; |
| 172 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 172 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 173 | 173 |
| 174 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 174 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 175 BrowserContext::GetStoragePartition(GetProfile(), | 175 BrowserContext::GetStoragePartition(GetProfile(), |
| 176 render_view_host()->GetSiteInstance()) | 176 render_view_host()->GetSiteInstance()) |
| 177 ->GetFileSystemContext(); | 177 ->GetFileSystemContext(); |
| 178 fileapi::FileSystemURL file_system_url( | 178 storage::FileSystemURL file_system_url( |
| 179 file_system_context->CrackURL(GURL(url))); | 179 file_system_context->CrackURL(GURL(url))); |
| 180 | 180 |
| 181 GetSyncFileSystemService(GetProfile())->GetFileSyncStatus( | 181 GetSyncFileSystemService(GetProfile())->GetFileSyncStatus( |
| 182 file_system_url, | 182 file_system_url, |
| 183 Bind(&SyncFileSystemGetFileStatusFunction::DidGetFileStatus, this)); | 183 Bind(&SyncFileSystemGetFileStatusFunction::DidGetFileStatus, this)); |
| 184 return true; | 184 return true; |
| 185 } | 185 } |
| 186 | 186 |
| 187 void SyncFileSystemGetFileStatusFunction::DidGetFileStatus( | 187 void SyncFileSystemGetFileStatusFunction::DidGetFileStatus( |
| 188 const SyncStatusCode sync_status_code, | 188 const SyncStatusCode sync_status_code, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 204 } | 204 } |
| 205 | 205 |
| 206 SyncFileSystemGetFileStatusesFunction::~SyncFileSystemGetFileStatusesFunction( | 206 SyncFileSystemGetFileStatusesFunction::~SyncFileSystemGetFileStatusesFunction( |
| 207 ) {} | 207 ) {} |
| 208 | 208 |
| 209 bool SyncFileSystemGetFileStatusesFunction::RunAsync() { | 209 bool SyncFileSystemGetFileStatusesFunction::RunAsync() { |
| 210 // All FileEntries converted into array of URL Strings in JS custom bindings. | 210 // All FileEntries converted into array of URL Strings in JS custom bindings. |
| 211 base::ListValue* file_entry_urls = NULL; | 211 base::ListValue* file_entry_urls = NULL; |
| 212 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &file_entry_urls)); | 212 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &file_entry_urls)); |
| 213 | 213 |
| 214 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 214 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 215 BrowserContext::GetStoragePartition(GetProfile(), | 215 BrowserContext::GetStoragePartition(GetProfile(), |
| 216 render_view_host()->GetSiteInstance()) | 216 render_view_host()->GetSiteInstance()) |
| 217 ->GetFileSystemContext(); | 217 ->GetFileSystemContext(); |
| 218 | 218 |
| 219 // Map each file path->SyncFileStatus in the callback map. | 219 // Map each file path->SyncFileStatus in the callback map. |
| 220 // TODO(calvinlo): Overload GetFileSyncStatus to take in URL array. | 220 // TODO(calvinlo): Overload GetFileSyncStatus to take in URL array. |
| 221 num_expected_results_ = file_entry_urls->GetSize(); | 221 num_expected_results_ = file_entry_urls->GetSize(); |
| 222 num_results_received_ = 0; | 222 num_results_received_ = 0; |
| 223 file_sync_statuses_.clear(); | 223 file_sync_statuses_.clear(); |
| 224 sync_file_system::SyncFileSystemService* sync_file_system_service = | 224 sync_file_system::SyncFileSystemService* sync_file_system_service = |
| 225 GetSyncFileSystemService(GetProfile()); | 225 GetSyncFileSystemService(GetProfile()); |
| 226 for (unsigned int i = 0; i < num_expected_results_; i++) { | 226 for (unsigned int i = 0; i < num_expected_results_; i++) { |
| 227 std::string url; | 227 std::string url; |
| 228 file_entry_urls->GetString(i, &url); | 228 file_entry_urls->GetString(i, &url); |
| 229 fileapi::FileSystemURL file_system_url( | 229 storage::FileSystemURL file_system_url( |
| 230 file_system_context->CrackURL(GURL(url))); | 230 file_system_context->CrackURL(GURL(url))); |
| 231 | 231 |
| 232 sync_file_system_service->GetFileSyncStatus( | 232 sync_file_system_service->GetFileSyncStatus( |
| 233 file_system_url, | 233 file_system_url, |
| 234 Bind(&SyncFileSystemGetFileStatusesFunction::DidGetFileStatus, | 234 Bind(&SyncFileSystemGetFileStatusesFunction::DidGetFileStatus, |
| 235 this, file_system_url)); | 235 this, file_system_url)); |
| 236 } | 236 } |
| 237 | 237 |
| 238 return true; | 238 return true; |
| 239 } | 239 } |
| 240 | 240 |
| 241 void SyncFileSystemGetFileStatusesFunction::DidGetFileStatus( | 241 void SyncFileSystemGetFileStatusesFunction::DidGetFileStatus( |
| 242 const fileapi::FileSystemURL& file_system_url, | 242 const storage::FileSystemURL& file_system_url, |
| 243 SyncStatusCode sync_status_code, | 243 SyncStatusCode sync_status_code, |
| 244 SyncFileStatus sync_file_status) { | 244 SyncFileStatus sync_file_status) { |
| 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 245 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 246 num_results_received_++; | 246 num_results_received_++; |
| 247 DCHECK_LE(num_results_received_, num_expected_results_); | 247 DCHECK_LE(num_results_received_, num_expected_results_); |
| 248 | 248 |
| 249 file_sync_statuses_[file_system_url] = | 249 file_sync_statuses_[file_system_url] = |
| 250 std::make_pair(sync_status_code, sync_file_status); | 250 std::make_pair(sync_status_code, sync_file_status); |
| 251 | 251 |
| 252 // Keep mapping file statuses until all of them have been received. | 252 // Keep mapping file statuses until all of them have been received. |
| 253 // TODO(calvinlo): Get rid of this check when batch version of | 253 // TODO(calvinlo): Get rid of this check when batch version of |
| 254 // GetFileSyncStatus(GURL urls[]); is added. | 254 // GetFileSyncStatus(GURL urls[]); is added. |
| 255 if (num_results_received_ < num_expected_results_) | 255 if (num_results_received_ < num_expected_results_) |
| 256 return; | 256 return; |
| 257 | 257 |
| 258 // All results received. Dump array of statuses into extension enum values. | 258 // All results received. Dump array of statuses into extension enum values. |
| 259 // Note that the enum types need to be set as strings manually as the | 259 // Note that the enum types need to be set as strings manually as the |
| 260 // autogenerated Results::Create function thinks the enum values should be | 260 // autogenerated Results::Create function thinks the enum values should be |
| 261 // returned as int values. | 261 // returned as int values. |
| 262 base::ListValue* status_array = new base::ListValue(); | 262 base::ListValue* status_array = new base::ListValue(); |
| 263 for (URLToStatusMap::iterator it = file_sync_statuses_.begin(); | 263 for (URLToStatusMap::iterator it = file_sync_statuses_.begin(); |
| 264 it != file_sync_statuses_.end(); ++it) { | 264 it != file_sync_statuses_.end(); ++it) { |
| 265 base::DictionaryValue* dict = new base::DictionaryValue(); | 265 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 266 status_array->Append(dict); | 266 status_array->Append(dict); |
| 267 | 267 |
| 268 fileapi::FileSystemURL url = it->first; | 268 storage::FileSystemURL url = it->first; |
| 269 SyncStatusCode file_error = it->second.first; | 269 SyncStatusCode file_error = it->second.first; |
| 270 api::sync_file_system::FileStatus file_status = | 270 api::sync_file_system::FileStatus file_status = |
| 271 SyncFileStatusToExtensionEnum(it->second.second); | 271 SyncFileStatusToExtensionEnum(it->second.second); |
| 272 | 272 |
| 273 dict->Set("entry", CreateDictionaryValueForFileSystemEntry( | 273 dict->Set("entry", CreateDictionaryValueForFileSystemEntry( |
| 274 url, sync_file_system::SYNC_FILE_TYPE_FILE)); | 274 url, sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 275 dict->SetString("status", ToString(file_status)); | 275 dict->SetString("status", ToString(file_status)); |
| 276 | 276 |
| 277 if (file_error == sync_file_system::SYNC_STATUS_OK) | 277 if (file_error == sync_file_system::SYNC_STATUS_OK) |
| 278 continue; | 278 continue; |
| 279 dict->SetString("error", ErrorToString(file_error)); | 279 dict->SetString("error", ErrorToString(file_error)); |
| 280 } | 280 } |
| 281 SetResult(status_array); | 281 SetResult(status_array); |
| 282 | 282 |
| 283 SendResponse(true); | 283 SendResponse(true); |
| 284 } | 284 } |
| 285 | 285 |
| 286 bool SyncFileSystemGetUsageAndQuotaFunction::RunAsync() { | 286 bool SyncFileSystemGetUsageAndQuotaFunction::RunAsync() { |
| 287 std::string url; | 287 std::string url; |
| 288 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 288 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 289 | 289 |
| 290 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 290 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 291 BrowserContext::GetStoragePartition(GetProfile(), | 291 BrowserContext::GetStoragePartition(GetProfile(), |
| 292 render_view_host()->GetSiteInstance()) | 292 render_view_host()->GetSiteInstance()) |
| 293 ->GetFileSystemContext(); | 293 ->GetFileSystemContext(); |
| 294 fileapi::FileSystemURL file_system_url( | 294 storage::FileSystemURL file_system_url( |
| 295 file_system_context->CrackURL(GURL(url))); | 295 file_system_context->CrackURL(GURL(url))); |
| 296 | 296 |
| 297 scoped_refptr<quota::QuotaManager> quota_manager = | 297 scoped_refptr<storage::QuotaManager> quota_manager = |
| 298 BrowserContext::GetStoragePartition(GetProfile(), | 298 BrowserContext::GetStoragePartition(GetProfile(), |
| 299 render_view_host()->GetSiteInstance()) | 299 render_view_host()->GetSiteInstance()) |
| 300 ->GetQuotaManager(); | 300 ->GetQuotaManager(); |
| 301 | 301 |
| 302 BrowserThread::PostTask( | 302 BrowserThread::PostTask( |
| 303 BrowserThread::IO, | 303 BrowserThread::IO, |
| 304 FROM_HERE, | 304 FROM_HERE, |
| 305 Bind("a::QuotaManager::GetUsageAndQuotaForWebApps, | 305 Bind(&storage::QuotaManager::GetUsageAndQuotaForWebApps, |
| 306 quota_manager, | 306 quota_manager, |
| 307 source_url().GetOrigin(), | 307 source_url().GetOrigin(), |
| 308 fileapi::FileSystemTypeToQuotaStorageType(file_system_url.type()), | 308 storage::FileSystemTypeToQuotaStorageType(file_system_url.type()), |
| 309 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, | 309 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, |
| 310 this))); | 310 this))); |
| 311 | 311 |
| 312 return true; | 312 return true; |
| 313 } | 313 } |
| 314 | 314 |
| 315 void SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota( | 315 void SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota( |
| 316 quota::QuotaStatusCode status, int64 usage, int64 quota) { | 316 storage::QuotaStatusCode status, |
| 317 int64 usage, |
| 318 int64 quota) { |
| 317 // Repost to switch from IO thread to UI thread for SendResponse(). | 319 // Repost to switch from IO thread to UI thread for SendResponse(). |
| 318 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 320 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 319 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 321 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 320 BrowserThread::PostTask( | 322 BrowserThread::PostTask( |
| 321 BrowserThread::UI, | 323 BrowserThread::UI, |
| 322 FROM_HERE, | 324 FROM_HERE, |
| 323 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, this, | 325 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, this, |
| 324 status, usage, quota)); | 326 status, usage, quota)); |
| 325 return; | 327 return; |
| 326 } | 328 } |
| 327 | 329 |
| 328 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 330 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 329 if (status != quota::kQuotaStatusOk) { | 331 if (status != storage::kQuotaStatusOk) { |
| 330 error_ = QuotaStatusCodeToString(status); | 332 error_ = QuotaStatusCodeToString(status); |
| 331 SendResponse(false); | 333 SendResponse(false); |
| 332 return; | 334 return; |
| 333 } | 335 } |
| 334 | 336 |
| 335 api::sync_file_system::StorageInfo info; | 337 api::sync_file_system::StorageInfo info; |
| 336 info.usage_bytes = usage; | 338 info.usage_bytes = usage; |
| 337 info.quota_bytes = quota; | 339 info.quota_bytes = quota; |
| 338 results_ = api::sync_file_system::GetUsageAndQuota::Results::Create(info); | 340 results_ = api::sync_file_system::GetUsageAndQuota::Results::Create(info); |
| 339 SendResponse(true); | 341 SendResponse(true); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 361 | 363 |
| 362 bool SyncFileSystemGetServiceStatusFunction::RunSync() { | 364 bool SyncFileSystemGetServiceStatusFunction::RunSync() { |
| 363 sync_file_system::SyncFileSystemService* service = | 365 sync_file_system::SyncFileSystemService* service = |
| 364 GetSyncFileSystemService(GetProfile()); | 366 GetSyncFileSystemService(GetProfile()); |
| 365 results_ = api::sync_file_system::GetServiceStatus::Results::Create( | 367 results_ = api::sync_file_system::GetServiceStatus::Results::Create( |
| 366 SyncServiceStateToExtensionEnum(service->GetSyncServiceState())); | 368 SyncServiceStateToExtensionEnum(service->GetSyncServiceState())); |
| 367 return true; | 369 return true; |
| 368 } | 370 } |
| 369 | 371 |
| 370 } // namespace extensions | 372 } // namespace extensions |
| OLD | NEW |