| 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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/extensions/api/sync_file_system/extension_sync_event_ob
server.h" | 13 #include "chrome/browser/extensions/api/sync_file_system/extension_sync_event_ob
server.h" |
| 14 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api_he
lpers.h" | 14 #include "chrome/browser/extensions/api/sync_file_system/sync_file_system_api_he
lpers.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_servi
ce.h" | 16 #include "chrome/browser/sync_file_system/drive_backend_v1/drive_file_sync_servi
ce.h" |
| 17 #include "chrome/browser/sync_file_system/sync_file_status.h" | 17 #include "chrome/browser/sync_file_system/sync_file_status.h" |
| 18 #include "chrome/browser/sync_file_system/sync_file_system_service.h" | 18 #include "chrome/browser/sync_file_system/sync_file_system_service.h" |
| 19 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" | 19 #include "chrome/browser/sync_file_system/sync_file_system_service_factory.h" |
| 20 #include "chrome/common/extensions/api/sync_file_system.h" | 20 #include "chrome/common/extensions/api/sync_file_system.h" |
| 21 #include "content/public/browser/browser_context.h" | 21 #include "content/public/browser/browser_context.h" |
| 22 #include "content/public/browser/render_view_host.h" | 22 #include "content/public/browser/render_view_host.h" |
| 23 #include "content/public/browser/storage_partition.h" | 23 #include "content/public/browser/storage_partition.h" |
| 24 #include "content/public/common/content_client.h" | 24 #include "content/public/common/content_client.h" |
| 25 #include "webkit/browser/fileapi/file_system_context.h" | 25 #include "storage/browser/fileapi/file_system_context.h" |
| 26 #include "webkit/browser/fileapi/file_system_url.h" | 26 #include "storage/browser/fileapi/file_system_url.h" |
| 27 #include "webkit/browser/quota/quota_manager.h" | 27 #include "storage/browser/quota/quota_manager.h" |
| 28 #include "webkit/common/fileapi/file_system_types.h" | 28 #include "storage/common/fileapi/file_system_types.h" |
| 29 #include "webkit/common/fileapi/file_system_util.h" | 29 #include "storage/common/fileapi/file_system_util.h" |
| 30 | 30 |
| 31 using content::BrowserContext; | 31 using content::BrowserContext; |
| 32 using content::BrowserThread; | 32 using content::BrowserThread; |
| 33 using sync_file_system::ConflictResolutionPolicy; | 33 using sync_file_system::ConflictResolutionPolicy; |
| 34 using sync_file_system::SyncFileStatus; | 34 using sync_file_system::SyncFileStatus; |
| 35 using sync_file_system::SyncFileSystemServiceFactory; | 35 using sync_file_system::SyncFileSystemServiceFactory; |
| 36 using sync_file_system::SyncStatusCode; | 36 using sync_file_system::SyncStatusCode; |
| 37 | 37 |
| 38 namespace extensions { | 38 namespace extensions { |
| 39 | 39 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 62 sync_file_system::SyncStatusCodeToString(code), | 62 sync_file_system::SyncStatusCodeToString(code), |
| 63 static_cast<int>(code)); | 63 static_cast<int>(code)); |
| 64 } | 64 } |
| 65 | 65 |
| 66 } // namespace | 66 } // namespace |
| 67 | 67 |
| 68 bool SyncFileSystemDeleteFileSystemFunction::RunAsync() { | 68 bool SyncFileSystemDeleteFileSystemFunction::RunAsync() { |
| 69 std::string url; | 69 std::string url; |
| 70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 70 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 71 | 71 |
| 72 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 72 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 73 BrowserContext::GetStoragePartition(GetProfile(), | 73 BrowserContext::GetStoragePartition(GetProfile(), |
| 74 render_view_host()->GetSiteInstance()) | 74 render_view_host()->GetSiteInstance()) |
| 75 ->GetFileSystemContext(); | 75 ->GetFileSystemContext(); |
| 76 fileapi::FileSystemURL file_system_url( | 76 storage::FileSystemURL file_system_url( |
| 77 file_system_context->CrackURL(GURL(url))); | 77 file_system_context->CrackURL(GURL(url))); |
| 78 | 78 |
| 79 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 80 BrowserThread::IO, | 80 BrowserThread::IO, |
| 81 FROM_HERE, | 81 FROM_HERE, |
| 82 Bind(&fileapi::FileSystemContext::DeleteFileSystem, | 82 Bind(&storage::FileSystemContext::DeleteFileSystem, |
| 83 file_system_context, | 83 file_system_context, |
| 84 source_url().GetOrigin(), | 84 source_url().GetOrigin(), |
| 85 file_system_url.type(), | 85 file_system_url.type(), |
| 86 Bind(&SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem, | 86 Bind(&SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem, |
| 87 this))); | 87 this))); |
| 88 return true; | 88 return true; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem( | 91 void SyncFileSystemDeleteFileSystemFunction::DidDeleteFileSystem( |
| 92 base::File::Error error) { | 92 base::File::Error error) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 114 } | 114 } |
| 115 | 115 |
| 116 bool SyncFileSystemRequestFileSystemFunction::RunAsync() { | 116 bool SyncFileSystemRequestFileSystemFunction::RunAsync() { |
| 117 // SyncFileSystem initialization is done in OpenFileSystem below, but we call | 117 // SyncFileSystem initialization is done in OpenFileSystem below, but we call |
| 118 // GetSyncFileSystemService here too to initialize sync event observer for | 118 // GetSyncFileSystemService here too to initialize sync event observer for |
| 119 // extensions API. | 119 // extensions API. |
| 120 GetSyncFileSystemService(GetProfile()); | 120 GetSyncFileSystemService(GetProfile()); |
| 121 | 121 |
| 122 // Initializes sync context for this extension and continue to open | 122 // Initializes sync context for this extension and continue to open |
| 123 // a new file system. | 123 // a new file system. |
| 124 BrowserThread::PostTask( | 124 BrowserThread::PostTask(BrowserThread::IO, |
| 125 BrowserThread::IO, FROM_HERE, | 125 FROM_HERE, |
| 126 Bind(&fileapi::FileSystemContext::OpenFileSystem, | 126 Bind(&storage::FileSystemContext::OpenFileSystem, |
| 127 GetFileSystemContext(), | 127 GetFileSystemContext(), |
| 128 source_url().GetOrigin(), | 128 source_url().GetOrigin(), |
| 129 fileapi::kFileSystemTypeSyncable, | 129 storage::kFileSystemTypeSyncable, |
| 130 fileapi::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, | 130 storage::OPEN_FILE_SYSTEM_CREATE_IF_NONEXISTENT, |
| 131 base::Bind(&self::DidOpenFileSystem, this))); | 131 base::Bind(&self::DidOpenFileSystem, this))); |
| 132 return true; | 132 return true; |
| 133 } | 133 } |
| 134 | 134 |
| 135 fileapi::FileSystemContext* | 135 storage::FileSystemContext* |
| 136 SyncFileSystemRequestFileSystemFunction::GetFileSystemContext() { | 136 SyncFileSystemRequestFileSystemFunction::GetFileSystemContext() { |
| 137 DCHECK(render_view_host()); | 137 DCHECK(render_view_host()); |
| 138 return BrowserContext::GetStoragePartition( | 138 return BrowserContext::GetStoragePartition( |
| 139 GetProfile(), render_view_host()->GetSiteInstance()) | 139 GetProfile(), render_view_host()->GetSiteInstance()) |
| 140 ->GetFileSystemContext(); | 140 ->GetFileSystemContext(); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem( | 143 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem( |
| 144 const GURL& root_url, | 144 const GURL& root_url, |
| 145 const std::string& file_system_name, | 145 const std::string& file_system_name, |
| (...skipping 19 matching lines...) Expand all Loading... |
| 165 SetResult(dict); | 165 SetResult(dict); |
| 166 dict->SetString("name", file_system_name); | 166 dict->SetString("name", file_system_name); |
| 167 dict->SetString("root", root_url.spec()); | 167 dict->SetString("root", root_url.spec()); |
| 168 SendResponse(true); | 168 SendResponse(true); |
| 169 } | 169 } |
| 170 | 170 |
| 171 bool SyncFileSystemGetFileStatusFunction::RunAsync() { | 171 bool SyncFileSystemGetFileStatusFunction::RunAsync() { |
| 172 std::string url; | 172 std::string url; |
| 173 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 173 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 174 | 174 |
| 175 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 175 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 176 BrowserContext::GetStoragePartition(GetProfile(), | 176 BrowserContext::GetStoragePartition(GetProfile(), |
| 177 render_view_host()->GetSiteInstance()) | 177 render_view_host()->GetSiteInstance()) |
| 178 ->GetFileSystemContext(); | 178 ->GetFileSystemContext(); |
| 179 fileapi::FileSystemURL file_system_url( | 179 storage::FileSystemURL file_system_url( |
| 180 file_system_context->CrackURL(GURL(url))); | 180 file_system_context->CrackURL(GURL(url))); |
| 181 | 181 |
| 182 GetSyncFileSystemService(GetProfile())->GetFileSyncStatus( | 182 GetSyncFileSystemService(GetProfile())->GetFileSyncStatus( |
| 183 file_system_url, | 183 file_system_url, |
| 184 Bind(&SyncFileSystemGetFileStatusFunction::DidGetFileStatus, this)); | 184 Bind(&SyncFileSystemGetFileStatusFunction::DidGetFileStatus, this)); |
| 185 return true; | 185 return true; |
| 186 } | 186 } |
| 187 | 187 |
| 188 void SyncFileSystemGetFileStatusFunction::DidGetFileStatus( | 188 void SyncFileSystemGetFileStatusFunction::DidGetFileStatus( |
| 189 const SyncStatusCode sync_status_code, | 189 const SyncStatusCode sync_status_code, |
| (...skipping 15 matching lines...) Expand all Loading... |
| 205 } | 205 } |
| 206 | 206 |
| 207 SyncFileSystemGetFileStatusesFunction::~SyncFileSystemGetFileStatusesFunction( | 207 SyncFileSystemGetFileStatusesFunction::~SyncFileSystemGetFileStatusesFunction( |
| 208 ) {} | 208 ) {} |
| 209 | 209 |
| 210 bool SyncFileSystemGetFileStatusesFunction::RunAsync() { | 210 bool SyncFileSystemGetFileStatusesFunction::RunAsync() { |
| 211 // All FileEntries converted into array of URL Strings in JS custom bindings. | 211 // All FileEntries converted into array of URL Strings in JS custom bindings. |
| 212 base::ListValue* file_entry_urls = NULL; | 212 base::ListValue* file_entry_urls = NULL; |
| 213 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &file_entry_urls)); | 213 EXTENSION_FUNCTION_VALIDATE(args_->GetList(0, &file_entry_urls)); |
| 214 | 214 |
| 215 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 215 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 216 BrowserContext::GetStoragePartition(GetProfile(), | 216 BrowserContext::GetStoragePartition(GetProfile(), |
| 217 render_view_host()->GetSiteInstance()) | 217 render_view_host()->GetSiteInstance()) |
| 218 ->GetFileSystemContext(); | 218 ->GetFileSystemContext(); |
| 219 | 219 |
| 220 // Map each file path->SyncFileStatus in the callback map. | 220 // Map each file path->SyncFileStatus in the callback map. |
| 221 // TODO(calvinlo): Overload GetFileSyncStatus to take in URL array. | 221 // TODO(calvinlo): Overload GetFileSyncStatus to take in URL array. |
| 222 num_expected_results_ = file_entry_urls->GetSize(); | 222 num_expected_results_ = file_entry_urls->GetSize(); |
| 223 num_results_received_ = 0; | 223 num_results_received_ = 0; |
| 224 file_sync_statuses_.clear(); | 224 file_sync_statuses_.clear(); |
| 225 sync_file_system::SyncFileSystemService* sync_file_system_service = | 225 sync_file_system::SyncFileSystemService* sync_file_system_service = |
| 226 GetSyncFileSystemService(GetProfile()); | 226 GetSyncFileSystemService(GetProfile()); |
| 227 for (unsigned int i = 0; i < num_expected_results_; i++) { | 227 for (unsigned int i = 0; i < num_expected_results_; i++) { |
| 228 std::string url; | 228 std::string url; |
| 229 file_entry_urls->GetString(i, &url); | 229 file_entry_urls->GetString(i, &url); |
| 230 fileapi::FileSystemURL file_system_url( | 230 storage::FileSystemURL file_system_url( |
| 231 file_system_context->CrackURL(GURL(url))); | 231 file_system_context->CrackURL(GURL(url))); |
| 232 | 232 |
| 233 sync_file_system_service->GetFileSyncStatus( | 233 sync_file_system_service->GetFileSyncStatus( |
| 234 file_system_url, | 234 file_system_url, |
| 235 Bind(&SyncFileSystemGetFileStatusesFunction::DidGetFileStatus, | 235 Bind(&SyncFileSystemGetFileStatusesFunction::DidGetFileStatus, |
| 236 this, file_system_url)); | 236 this, file_system_url)); |
| 237 } | 237 } |
| 238 | 238 |
| 239 return true; | 239 return true; |
| 240 } | 240 } |
| 241 | 241 |
| 242 void SyncFileSystemGetFileStatusesFunction::DidGetFileStatus( | 242 void SyncFileSystemGetFileStatusesFunction::DidGetFileStatus( |
| 243 const fileapi::FileSystemURL& file_system_url, | 243 const storage::FileSystemURL& file_system_url, |
| 244 SyncStatusCode sync_status_code, | 244 SyncStatusCode sync_status_code, |
| 245 SyncFileStatus sync_file_status) { | 245 SyncFileStatus sync_file_status) { |
| 246 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 246 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 247 num_results_received_++; | 247 num_results_received_++; |
| 248 DCHECK_LE(num_results_received_, num_expected_results_); | 248 DCHECK_LE(num_results_received_, num_expected_results_); |
| 249 | 249 |
| 250 file_sync_statuses_[file_system_url] = | 250 file_sync_statuses_[file_system_url] = |
| 251 std::make_pair(sync_status_code, sync_file_status); | 251 std::make_pair(sync_status_code, sync_file_status); |
| 252 | 252 |
| 253 // Keep mapping file statuses until all of them have been received. | 253 // Keep mapping file statuses until all of them have been received. |
| 254 // TODO(calvinlo): Get rid of this check when batch version of | 254 // TODO(calvinlo): Get rid of this check when batch version of |
| 255 // GetFileSyncStatus(GURL urls[]); is added. | 255 // GetFileSyncStatus(GURL urls[]); is added. |
| 256 if (num_results_received_ < num_expected_results_) | 256 if (num_results_received_ < num_expected_results_) |
| 257 return; | 257 return; |
| 258 | 258 |
| 259 // All results received. Dump array of statuses into extension enum values. | 259 // All results received. Dump array of statuses into extension enum values. |
| 260 // Note that the enum types need to be set as strings manually as the | 260 // Note that the enum types need to be set as strings manually as the |
| 261 // autogenerated Results::Create function thinks the enum values should be | 261 // autogenerated Results::Create function thinks the enum values should be |
| 262 // returned as int values. | 262 // returned as int values. |
| 263 base::ListValue* status_array = new base::ListValue(); | 263 base::ListValue* status_array = new base::ListValue(); |
| 264 for (URLToStatusMap::iterator it = file_sync_statuses_.begin(); | 264 for (URLToStatusMap::iterator it = file_sync_statuses_.begin(); |
| 265 it != file_sync_statuses_.end(); ++it) { | 265 it != file_sync_statuses_.end(); ++it) { |
| 266 base::DictionaryValue* dict = new base::DictionaryValue(); | 266 base::DictionaryValue* dict = new base::DictionaryValue(); |
| 267 status_array->Append(dict); | 267 status_array->Append(dict); |
| 268 | 268 |
| 269 fileapi::FileSystemURL url = it->first; | 269 storage::FileSystemURL url = it->first; |
| 270 SyncStatusCode file_error = it->second.first; | 270 SyncStatusCode file_error = it->second.first; |
| 271 api::sync_file_system::FileStatus file_status = | 271 api::sync_file_system::FileStatus file_status = |
| 272 SyncFileStatusToExtensionEnum(it->second.second); | 272 SyncFileStatusToExtensionEnum(it->second.second); |
| 273 | 273 |
| 274 dict->Set("entry", CreateDictionaryValueForFileSystemEntry( | 274 dict->Set("entry", CreateDictionaryValueForFileSystemEntry( |
| 275 url, sync_file_system::SYNC_FILE_TYPE_FILE)); | 275 url, sync_file_system::SYNC_FILE_TYPE_FILE)); |
| 276 dict->SetString("status", ToString(file_status)); | 276 dict->SetString("status", ToString(file_status)); |
| 277 | 277 |
| 278 if (file_error == sync_file_system::SYNC_STATUS_OK) | 278 if (file_error == sync_file_system::SYNC_STATUS_OK) |
| 279 continue; | 279 continue; |
| 280 dict->SetString("error", ErrorToString(file_error)); | 280 dict->SetString("error", ErrorToString(file_error)); |
| 281 } | 281 } |
| 282 SetResult(status_array); | 282 SetResult(status_array); |
| 283 | 283 |
| 284 SendResponse(true); | 284 SendResponse(true); |
| 285 } | 285 } |
| 286 | 286 |
| 287 bool SyncFileSystemGetUsageAndQuotaFunction::RunAsync() { | 287 bool SyncFileSystemGetUsageAndQuotaFunction::RunAsync() { |
| 288 std::string url; | 288 std::string url; |
| 289 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); | 289 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &url)); |
| 290 | 290 |
| 291 scoped_refptr<fileapi::FileSystemContext> file_system_context = | 291 scoped_refptr<storage::FileSystemContext> file_system_context = |
| 292 BrowserContext::GetStoragePartition(GetProfile(), | 292 BrowserContext::GetStoragePartition(GetProfile(), |
| 293 render_view_host()->GetSiteInstance()) | 293 render_view_host()->GetSiteInstance()) |
| 294 ->GetFileSystemContext(); | 294 ->GetFileSystemContext(); |
| 295 fileapi::FileSystemURL file_system_url( | 295 storage::FileSystemURL file_system_url( |
| 296 file_system_context->CrackURL(GURL(url))); | 296 file_system_context->CrackURL(GURL(url))); |
| 297 | 297 |
| 298 scoped_refptr<quota::QuotaManager> quota_manager = | 298 scoped_refptr<quota::QuotaManager> quota_manager = |
| 299 BrowserContext::GetStoragePartition(GetProfile(), | 299 BrowserContext::GetStoragePartition(GetProfile(), |
| 300 render_view_host()->GetSiteInstance()) | 300 render_view_host()->GetSiteInstance()) |
| 301 ->GetQuotaManager(); | 301 ->GetQuotaManager(); |
| 302 | 302 |
| 303 BrowserThread::PostTask( | 303 BrowserThread::PostTask( |
| 304 BrowserThread::IO, | 304 BrowserThread::IO, |
| 305 FROM_HERE, | 305 FROM_HERE, |
| 306 Bind("a::QuotaManager::GetUsageAndQuotaForWebApps, | 306 Bind("a::QuotaManager::GetUsageAndQuotaForWebApps, |
| 307 quota_manager, | 307 quota_manager, |
| 308 source_url().GetOrigin(), | 308 source_url().GetOrigin(), |
| 309 fileapi::FileSystemTypeToQuotaStorageType(file_system_url.type()), | 309 storage::FileSystemTypeToQuotaStorageType(file_system_url.type()), |
| 310 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, | 310 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, |
| 311 this))); | 311 this))); |
| 312 | 312 |
| 313 return true; | 313 return true; |
| 314 } | 314 } |
| 315 | 315 |
| 316 void SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota( | 316 void SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota( |
| 317 quota::QuotaStatusCode status, int64 usage, int64 quota) { | 317 quota::QuotaStatusCode status, int64 usage, int64 quota) { |
| 318 // Repost to switch from IO thread to UI thread for SendResponse(). | 318 // Repost to switch from IO thread to UI thread for SendResponse(). |
| 319 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 319 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 362 | 362 |
| 363 bool SyncFileSystemGetServiceStatusFunction::RunSync() { | 363 bool SyncFileSystemGetServiceStatusFunction::RunSync() { |
| 364 sync_file_system::SyncFileSystemService* service = | 364 sync_file_system::SyncFileSystemService* service = |
| 365 GetSyncFileSystemService(GetProfile()); | 365 GetSyncFileSystemService(GetProfile()); |
| 366 results_ = api::sync_file_system::GetServiceStatus::Results::Create( | 366 results_ = api::sync_file_system::GetServiceStatus::Results::Create( |
| 367 SyncServiceStateToExtensionEnum(service->GetSyncServiceState())); | 367 SyncServiceStateToExtensionEnum(service->GetSyncServiceState())); |
| 368 return true; | 368 return true; |
| 369 } | 369 } |
| 370 | 370 |
| 371 } // namespace extensions | 371 } // namespace extensions |
| OLD | NEW |