Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: chrome/browser/extensions/api/sync_file_system/sync_file_system_api.cc

Issue 11146034: Add GetUsageAndQuota() to Syncable API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Mihai Review #2 Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/stringprintf.h" 11 #include "base/stringprintf.h"
12 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/sync_file_system/sync_file_system_service.h" 13 #include "chrome/browser/sync_file_system/sync_file_system_service.h"
14 #include "chrome/common/extensions/api/sync_file_system.h"
14 #include "content/public/browser/browser_context.h" 15 #include "content/public/browser/browser_context.h"
15 #include "content/public/browser/render_view_host.h" 16 #include "content/public/browser/render_view_host.h"
16 #include "content/public/browser/storage_partition.h" 17 #include "content/public/browser/storage_partition.h"
17 #include "content/public/common/content_client.h" 18 #include "content/public/common/content_client.h"
18 #include "webkit/fileapi/file_system_context.h" 19 #include "webkit/fileapi/file_system_context.h"
19 #include "webkit/fileapi/file_system_types.h" 20 #include "webkit/fileapi/file_system_types.h"
21 #include "webkit/quota/quota_manager.h"
20 22
21 using content::BrowserContext; 23 using content::BrowserContext;
22 using content::BrowserThread; 24 using content::BrowserThread;
23 using sync_file_system::SyncFileSystemServiceFactory; 25 using sync_file_system::SyncFileSystemServiceFactory;
24 26
25 namespace extensions { 27 namespace extensions {
26 28
27 namespace { 29 namespace {
28 30
29 // This is the only supported cloud backend service for now. 31 // This is the only supported cloud backend service for now.
30 const char kDriveCloudService[] = "drive"; 32 const char kDriveCloudService[] = "drive";
31 33
32 // Error messages. 34 // Error messages.
33 const char kNotSupportedService[] = "Cloud service %s not supported."; 35 const char kNotSupportedService[] = "Cloud service %s not supported.";
34 const char kFileError[] = "File error %d."; 36 const char kFileError[] = "File error %d.";
35 37 const char kQuotaError[] = "Quota error %d.";
36 const BrowserThread::ID kControlThread = BrowserThread::UI;
37 38
38 } // namespace 39 } // namespace
39 40
40 bool SyncFileSystemRequestFileSystemFunction::RunImpl() { 41 bool SyncFileSystemRequestFileSystemFunction::RunImpl() {
41 std::string service_name; 42 std::string service_name;
42 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &service_name)); 43 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &service_name));
43 44
44 if (service_name != std::string(kDriveCloudService)) { 45 if (service_name != std::string(kDriveCloudService)) {
45 error_ = base::StringPrintf(kNotSupportedService, service_name.c_str()); 46 error_ = base::StringPrintf(kNotSupportedService, service_name.c_str());
46 return false; 47 return false;
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
82 source_url(), 83 source_url(),
83 fileapi::kFileSystemTypeSyncable, 84 fileapi::kFileSystemTypeSyncable,
84 true, /* create */ 85 true, /* create */
85 base::Bind(&self::DidOpenFileSystem, this)); 86 base::Bind(&self::DidOpenFileSystem, this));
86 } 87 }
87 88
88 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem( 89 void SyncFileSystemRequestFileSystemFunction::DidOpenFileSystem(
89 base::PlatformFileError error, 90 base::PlatformFileError error,
90 const std::string& file_system_name, 91 const std::string& file_system_name,
91 const GURL& root_url) { 92 const GURL& root_url) {
92 DCHECK(BrowserThread::CurrentlyOn(kControlThread)); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
93 if (error != base::PLATFORM_FILE_OK) { 94 if (error != base::PLATFORM_FILE_OK) {
94 error_ = base::StringPrintf(kFileError, static_cast<int>(error)); 95 error_ = base::StringPrintf(kFileError, static_cast<int>(error));
95 SendResponse(false); 96 SendResponse(false);
96 return; 97 return;
97 } 98 }
98 99
99 DictionaryValue* dict = new DictionaryValue(); 100 DictionaryValue* dict = new DictionaryValue();
100 SetResult(dict); 101 SetResult(dict);
101 dict->SetString("name", file_system_name); 102 dict->SetString("name", file_system_name);
102 dict->SetString("root", root_url.spec()); 103 dict->SetString("root", root_url.spec());
103 SendResponse(true); 104 SendResponse(true);
104 } 105 }
105 106
107 bool SyncFileSystemGetUsageAndQuotaFunction::RunImpl() {
108 std::string service_name;
109 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &service_name));
110
111 // TODO(calvinlo): For now only gDrive cloud service is supported.
112 if (service_name != std::string(kDriveCloudService)) {
113 error_ = base::StringPrintf(kNotSupportedService, service_name.c_str());
114 return false;
115 }
116
117 scoped_refptr<quota::QuotaManager> quota_manager =
118 BrowserContext::GetStoragePartition(
119 profile(),
120 render_view_host()->GetSiteInstance())->GetQuotaManager();
121
122 BrowserThread::PostTask(
123 BrowserThread::IO,
124 FROM_HERE,
125 Bind(&quota::QuotaManager::GetUsageAndQuota,
126 quota_manager,
127 source_url(),
128 quota::kStorageTypeSyncable,
129 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota,
130 this)));
131
132 return true;
133 }
134
135 void SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota(
136 quota::QuotaStatusCode status, int64 usage, int64 quota) {
137 // Repost to switch from IO thread to UI thread for SendResponse().
138 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
140 BrowserThread::PostTask(
141 BrowserThread::UI,
142 FROM_HERE,
143 Bind(&SyncFileSystemGetUsageAndQuotaFunction::DidGetUsageAndQuota, this,
144 status, usage, quota));
145 return;
146 }
147
148 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
149 // TODO(calvinlo): Convert QuotaStatusCode to error string
150 // (http://crbug.com/156791).
151 if (status != quota::kQuotaStatusOk) {
152 error_ = base::StringPrintf(kQuotaError, static_cast<int>(status));
153 SendResponse(false);
154 return;
155 }
156
157 api::sync_file_system::StorageInfo info;
158 info.usage_bytes = usage;
159 info.quota_bytes = quota;
160 results_ = api::sync_file_system::GetUsageAndQuota::Results::Create(info);
161 SendResponse(true);
162 }
163
106 } // namespace extensions 164 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698