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

Side by Side Diff: chrome/browser/ui/webui/chromeos/drive_internals_ui.cc

Issue 10827292: Propagate the result of AmountOfFreeSpace to JS and show it in chrome://drive-internals. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: style fixes, small improvements Created 8 years, 4 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
« no previous file with comments | « chrome/browser/resources/chromeos/drive_internals.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ui/webui/chromeos/drive_internals_ui.h" 5 #include "chrome/browser/ui/webui/chromeos/drive_internals_ui.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/file_util.h" 8 #include "base/file_util.h"
9 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/stringprintf.h" 10 #include "base/stringprintf.h"
11 #include "base/memory/weak_ptr.h" 11 #include "base/memory/weak_ptr.h"
12 #include "base/path_service.h"
13 #include "base/sys_info.h"
12 #include "chrome/browser/chromeos/gdata/gdata.pb.h" 14 #include "chrome/browser/chromeos/gdata/gdata.pb.h"
13 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" 15 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h"
14 #include "chrome/browser/chromeos/gdata/gdata_cache.h" 16 #include "chrome/browser/chromeos/gdata/gdata_cache.h"
15 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" 17 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h"
16 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" 18 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h"
17 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" 19 #include "chrome/browser/chromeos/gdata/gdata_system_service.h"
18 #include "chrome/browser/chromeos/gdata/gdata_util.h" 20 #include "chrome/browser/chromeos/gdata/gdata_util.h"
19 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" 22 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
21 #include "chrome/common/url_constants.h" 23 #include "chrome/common/url_constants.h"
22 #include "content/public/browser/browser_thread.h" 24 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/web_ui.h" 25 #include "content/public/browser/web_ui.h"
24 #include "content/public/browser/web_ui_message_handler.h" 26 #include "content/public/browser/web_ui_message_handler.h"
25 #include "grit/browser_resources.h" 27 #include "grit/browser_resources.h"
26 28
29 using content::BrowserThread;
30
27 namespace chromeos { 31 namespace chromeos {
28 32
29 namespace { 33 namespace {
30 34
31 // Gets metadata of all files and directories in |root_path| 35 // Gets metadata of all files and directories in |root_path|
32 // recursively. Stores the result as a list of dictionaries like: 36 // recursively. Stores the result as a list of dictionaries like:
33 // 37 //
34 // [{ path: 'GCache/v1/tmp/<resource_id>', 38 // [{ path: 'GCache/v1/tmp/<resource_id>',
35 // size: 12345, 39 // size: 12345,
36 // is_directory: false, 40 // is_directory: false,
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 80
77 // Convert |files| into |gcache_contents|. 81 // Convert |files| into |gcache_contents|.
78 for (std::map<FilePath, DictionaryValue*>::const_iterator 82 for (std::map<FilePath, DictionaryValue*>::const_iterator
79 iter = files.begin(); iter != files.end(); ++iter) { 83 iter = files.begin(); iter != files.end(); ++iter) {
80 gcache_contents->Append(iter->second); 84 gcache_contents->Append(iter->second);
81 } 85 }
82 86
83 gcache_summary->SetDouble("total_size", total_size); 87 gcache_summary->SetDouble("total_size", total_size);
84 } 88 }
85 89
90 // Gets the available disk space for the path |home_path|.
91 void GetFreeDiskSpace(const FilePath& home_path,
92 base::DictionaryValue* local_storage_summary) {
93 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI));
94 DCHECK(local_storage_summary);
95
96 const int64 free_space = base::SysInfo::AmountOfFreeDiskSpace(home_path);
97 local_storage_summary->SetDouble("free_space", free_space);
98 }
99
86 // Formats |entry| into text. 100 // Formats |entry| into text.
87 std::string FormatEntry(const FilePath& path, 101 std::string FormatEntry(const FilePath& path,
88 const gdata::GDataEntryProto& entry) { 102 const gdata::GDataEntryProto& entry) {
89 using base::StringAppendF; 103 using base::StringAppendF;
90 using gdata::util::FormatTimeAsString; 104 using gdata::util::FormatTimeAsString;
91 105
92 std::string out; 106 std::string out;
93 StringAppendF(&out, "%s\n", path.AsUTF8Unsafe().c_str()); 107 StringAppendF(&out, "%s\n", path.AsUTF8Unsafe().c_str());
94 StringAppendF(&out, " title: %s\n", entry.title().c_str()); 108 StringAppendF(&out, " title: %s\n", entry.title().c_str());
95 StringAppendF(&out, " resource_id: %s\n", entry.resource_id().c_str()); 109 StringAppendF(&out, " resource_id: %s\n", entry.resource_id().c_str());
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
172 186
173 // Called when GetResourceIdsOfAllFilesOnUIThread() is complete. 187 // Called when GetResourceIdsOfAllFilesOnUIThread() is complete.
174 void OnGetResourceIdsOfAllFiles( 188 void OnGetResourceIdsOfAllFiles(
175 const std::vector<std::string>& resource_ids); 189 const std::vector<std::string>& resource_ids);
176 190
177 // Called when GetCacheEntryOnUIThread() is complete. 191 // Called when GetCacheEntryOnUIThread() is complete.
178 void OnGetCacheEntry(const std::string& resource_id, 192 void OnGetCacheEntry(const std::string& resource_id,
179 bool success, 193 bool success,
180 const gdata::GDataCacheEntry& cache_entry); 194 const gdata::GDataCacheEntry& cache_entry);
181 195
196 // Called when GetFreeDiskSpace() is complete.
197 void OnGetFreeDiskSpace(base::DictionaryValue* local_storage_summary);
198
182 // The number of pending ReadDirectoryByPath() calls. 199 // The number of pending ReadDirectoryByPath() calls.
183 int num_pending_reads_; 200 int num_pending_reads_;
184 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; 201 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_;
185 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); 202 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler);
186 }; 203 };
187 204
188 void DriveInternalsWebUIHandler::RegisterMessages() { 205 void DriveInternalsWebUIHandler::RegisterMessages() {
189 web_ui()->RegisterMessageCallback( 206 web_ui()->RegisterMessageCallback(
190 "pageLoaded", 207 "pageLoaded",
191 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded, 208 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded,
192 weak_ptr_factory_.GetWeakPtr())); 209 weak_ptr_factory_.GetWeakPtr()));
193 } 210 }
194 211
195 gdata::GDataSystemService* DriveInternalsWebUIHandler::GetSystemService() { 212 gdata::GDataSystemService* DriveInternalsWebUIHandler::GetSystemService() {
196 Profile* profile = Profile::FromWebUI(web_ui()); 213 Profile* profile = Profile::FromWebUI(web_ui());
197 return gdata::GDataSystemServiceFactory::GetForProfile(profile); 214 return gdata::GDataSystemServiceFactory::GetForProfile(profile);
198 } 215 }
199 216
200 void DriveInternalsWebUIHandler::OnPageLoaded(const base::ListValue* args) { 217 void DriveInternalsWebUIHandler::OnPageLoaded(const base::ListValue* args) {
218 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
219
201 gdata::GDataSystemService* system_service = GetSystemService(); 220 gdata::GDataSystemService* system_service = GetSystemService();
202 // |system_service| may be NULL in the guest/incognito mode. 221 // |system_service| may be NULL in the guest/incognito mode.
203 if (!system_service) 222 if (!system_service)
204 return; 223 return;
205 224
206 gdata::DocumentsServiceInterface* documents_service = 225 gdata::DocumentsServiceInterface* documents_service =
207 system_service->docs_service(); 226 system_service->docs_service();
208 DCHECK(documents_service); 227 DCHECK(documents_service);
209 228
210 // Update the auth status section. 229 // Update the auth status section.
211 base::DictionaryValue auth_status; 230 base::DictionaryValue auth_status;
212 auth_status.SetBoolean("has-refresh-token", 231 auth_status.SetBoolean("has-refresh-token",
213 documents_service->HasRefreshToken()); 232 documents_service->HasRefreshToken());
214 auth_status.SetBoolean("has-access-token", 233 auth_status.SetBoolean("has-access-token",
215 documents_service->HasAccessToken()); 234 documents_service->HasAccessToken());
216 web_ui()->CallJavascriptFunction("updateAuthStatus", auth_status); 235 web_ui()->CallJavascriptFunction("updateAuthStatus", auth_status);
217 236
218 // Start updating the GCache contents section. 237 // Start updating the GCache contents section.
219 Profile* profile = Profile::FromWebUI(web_ui()); 238 Profile* profile = Profile::FromWebUI(web_ui());
220 const FilePath root_path = 239 const FilePath root_path =
221 gdata::GDataCache::GetCacheRootPath(profile); 240 gdata::GDataCache::GetCacheRootPath(profile);
222 base::ListValue* gcache_contents = new ListValue; 241 base::ListValue* gcache_contents = new ListValue;
223 base::DictionaryValue* gcache_summary = new DictionaryValue; 242 base::DictionaryValue* gcache_summary = new DictionaryValue;
224 content::BrowserThread::PostBlockingPoolTaskAndReply( 243 BrowserThread::PostBlockingPoolTaskAndReply(
225 FROM_HERE, 244 FROM_HERE,
226 base::Bind(&GetGCacheContents, 245 base::Bind(&GetGCacheContents,
227 root_path, 246 root_path,
228 gcache_contents, 247 gcache_contents,
229 gcache_summary), 248 gcache_summary),
230 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, 249 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents,
231 weak_ptr_factory_.GetWeakPtr(), 250 weak_ptr_factory_.GetWeakPtr(),
232 base::Owned(gcache_contents), 251 base::Owned(gcache_contents),
233 base::Owned(gcache_summary))); 252 base::Owned(gcache_summary)));
253
254 // Propagate the amount of local free space in bytes.
255 FilePath home_path;
256 if (PathService::Get(base::DIR_HOME, &home_path)) {
257 base::DictionaryValue* local_storage_summary = new DictionaryValue;
258 BrowserThread::PostBlockingPoolTaskAndReply(
259 FROM_HERE,
260 base::Bind(&GetFreeDiskSpace, home_path, local_storage_summary),
261 base::Bind(&DriveInternalsWebUIHandler::OnGetFreeDiskSpace,
262 weak_ptr_factory_.GetWeakPtr(),
263 base::Owned(local_storage_summary)));
264 } else {
265 LOG(ERROR) << "Home directory not found";
266 }
234 } 267 }
235 268
236 void DriveInternalsWebUIHandler::OnGetGCacheContents( 269 void DriveInternalsWebUIHandler::OnGetGCacheContents(
237 base::ListValue* gcache_contents, 270 base::ListValue* gcache_contents,
238 base::DictionaryValue* gcache_summary) { 271 base::DictionaryValue* gcache_summary) {
239 DCHECK(gcache_contents); 272 DCHECK(gcache_contents);
240 DCHECK(gcache_summary); 273 DCHECK(gcache_summary);
241 web_ui()->CallJavascriptFunction("updateGCacheContents", 274 web_ui()->CallJavascriptFunction("updateGCacheContents",
242 *gcache_contents, 275 *gcache_contents,
243 *gcache_summary); 276 *gcache_summary);
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
328 value.SetString("md5", cache_entry.md5()); 361 value.SetString("md5", cache_entry.md5());
329 value.SetBoolean("is_present", cache_entry.is_present()); 362 value.SetBoolean("is_present", cache_entry.is_present());
330 value.SetBoolean("is_pinned", cache_entry.is_pinned()); 363 value.SetBoolean("is_pinned", cache_entry.is_pinned());
331 value.SetBoolean("is_dirty", cache_entry.is_dirty()); 364 value.SetBoolean("is_dirty", cache_entry.is_dirty());
332 value.SetBoolean("is_mounted", cache_entry.is_mounted()); 365 value.SetBoolean("is_mounted", cache_entry.is_mounted());
333 value.SetBoolean("is_persistent", cache_entry.is_persistent()); 366 value.SetBoolean("is_persistent", cache_entry.is_persistent());
334 367
335 web_ui()->CallJavascriptFunction("updateCacheContents", value); 368 web_ui()->CallJavascriptFunction("updateCacheContents", value);
336 } 369 }
337 370
371 void DriveInternalsWebUIHandler::OnGetFreeDiskSpace(
372 base::DictionaryValue* local_storage_summary) {
373 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
374 DCHECK(local_storage_summary);
375
376 web_ui()->CallJavascriptFunction(
377 "updateLocalStorageUsage", *local_storage_summary);
378 }
379
338 } // namespace 380 } // namespace
339 381
340 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) 382 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui)
341 : WebUIController(web_ui) { 383 : WebUIController(web_ui) {
342 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); 384 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler());
343 385
344 ChromeWebUIDataSource* source = 386 ChromeWebUIDataSource* source =
345 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); 387 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost);
346 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); 388 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS);
347 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); 389 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS);
348 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); 390 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML);
349 391
350 Profile* profile = Profile::FromWebUI(web_ui); 392 Profile* profile = Profile::FromWebUI(web_ui);
351 ChromeURLDataManager::AddDataSource(profile, source); 393 ChromeURLDataManager::AddDataSource(profile, source);
352 } 394 }
353 395
354 } // namespace chromeos 396 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/resources/chromeos/drive_internals.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698