Chromium Code Reviews| 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/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 Loading... | |
| 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 void GetFreeDiskSpace(const FilePath& path, | |
|
satorux1
2012/08/17 10:50:49
function comment is missing.
Haruki Sato
2012/08/17 15:08:45
Done.
Thanks.
| |
| 91 base::DictionaryValue* local_storage_summary) { | |
| 92 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
|
satorux1
2012/08/17 10:50:49
let's add
DCHECK(local_storage_summary);
Haruki Sato
2012/08/17 15:08:45
Done.
| |
| 93 | |
| 94 int64 free_space = base::SysInfo::AmountOfFreeDiskSpace(path); | |
|
satorux1
2012/08/17 10:50:49
matter of taste, but you might want to add 'const'
Haruki Sato
2012/08/17 15:08:45
Done.
| |
| 95 local_storage_summary->SetDouble("free_space", free_space); | |
| 96 } | |
| 97 | |
| 86 // Formats |entry| into text. | 98 // Formats |entry| into text. |
| 87 std::string FormatEntry(const FilePath& path, | 99 std::string FormatEntry(const FilePath& path, |
| 88 const gdata::GDataEntryProto& entry) { | 100 const gdata::GDataEntryProto& entry) { |
| 89 using base::StringAppendF; | 101 using base::StringAppendF; |
| 90 using gdata::util::FormatTimeAsString; | 102 using gdata::util::FormatTimeAsString; |
| 91 | 103 |
| 92 std::string out; | 104 std::string out; |
| 93 StringAppendF(&out, "%s\n", path.AsUTF8Unsafe().c_str()); | 105 StringAppendF(&out, "%s\n", path.AsUTF8Unsafe().c_str()); |
| 94 StringAppendF(&out, " title: %s\n", entry.title().c_str()); | 106 StringAppendF(&out, " title: %s\n", entry.title().c_str()); |
| 95 StringAppendF(&out, " resource_id: %s\n", entry.resource_id().c_str()); | 107 StringAppendF(&out, " resource_id: %s\n", entry.resource_id().c_str()); |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 | 184 |
| 173 // Called when GetResourceIdsOfAllFilesOnUIThread() is complete. | 185 // Called when GetResourceIdsOfAllFilesOnUIThread() is complete. |
| 174 void OnGetResourceIdsOfAllFiles( | 186 void OnGetResourceIdsOfAllFiles( |
| 175 const std::vector<std::string>& resource_ids); | 187 const std::vector<std::string>& resource_ids); |
| 176 | 188 |
| 177 // Called when GetCacheEntryOnUIThread() is complete. | 189 // Called when GetCacheEntryOnUIThread() is complete. |
| 178 void OnGetCacheEntry(const std::string& resource_id, | 190 void OnGetCacheEntry(const std::string& resource_id, |
| 179 bool success, | 191 bool success, |
| 180 const gdata::GDataCacheEntry& cache_entry); | 192 const gdata::GDataCacheEntry& cache_entry); |
| 181 | 193 |
| 194 // Called when GetFreeDiskSpace() is complete. | |
| 195 void OnGetFreeDiskSpace(base::DictionaryValue* local_storage_summary); | |
| 196 | |
| 182 // The number of pending ReadDirectoryByPath() calls. | 197 // The number of pending ReadDirectoryByPath() calls. |
| 183 int num_pending_reads_; | 198 int num_pending_reads_; |
| 184 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; | 199 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; |
| 185 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); | 200 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); |
| 186 }; | 201 }; |
| 187 | 202 |
| 188 void DriveInternalsWebUIHandler::RegisterMessages() { | 203 void DriveInternalsWebUIHandler::RegisterMessages() { |
| 189 web_ui()->RegisterMessageCallback( | 204 web_ui()->RegisterMessageCallback( |
| 190 "pageLoaded", | 205 "pageLoaded", |
| 191 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded, | 206 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded, |
| 192 weak_ptr_factory_.GetWeakPtr())); | 207 weak_ptr_factory_.GetWeakPtr())); |
| 193 } | 208 } |
| 194 | 209 |
| 195 gdata::GDataSystemService* DriveInternalsWebUIHandler::GetSystemService() { | 210 gdata::GDataSystemService* DriveInternalsWebUIHandler::GetSystemService() { |
| 196 Profile* profile = Profile::FromWebUI(web_ui()); | 211 Profile* profile = Profile::FromWebUI(web_ui()); |
| 197 return gdata::GDataSystemServiceFactory::GetForProfile(profile); | 212 return gdata::GDataSystemServiceFactory::GetForProfile(profile); |
| 198 } | 213 } |
| 199 | 214 |
| 200 void DriveInternalsWebUIHandler::OnPageLoaded(const base::ListValue* args) { | 215 void DriveInternalsWebUIHandler::OnPageLoaded(const base::ListValue* args) { |
| 216 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 217 | |
| 201 gdata::GDataSystemService* system_service = GetSystemService(); | 218 gdata::GDataSystemService* system_service = GetSystemService(); |
| 202 // |system_service| may be NULL in the guest/incognito mode. | 219 // |system_service| may be NULL in the guest/incognito mode. |
| 203 if (!system_service) | 220 if (!system_service) |
| 204 return; | 221 return; |
| 205 | 222 |
| 206 gdata::DocumentsServiceInterface* documents_service = | 223 gdata::DocumentsServiceInterface* documents_service = |
| 207 system_service->docs_service(); | 224 system_service->docs_service(); |
| 208 DCHECK(documents_service); | 225 DCHECK(documents_service); |
| 209 | 226 |
| 210 // Update the auth status section. | 227 // Update the auth status section. |
| 211 base::DictionaryValue auth_status; | 228 base::DictionaryValue auth_status; |
| 212 auth_status.SetBoolean("has-refresh-token", | 229 auth_status.SetBoolean("has-refresh-token", |
| 213 documents_service->HasRefreshToken()); | 230 documents_service->HasRefreshToken()); |
| 214 auth_status.SetBoolean("has-access-token", | 231 auth_status.SetBoolean("has-access-token", |
| 215 documents_service->HasAccessToken()); | 232 documents_service->HasAccessToken()); |
| 216 web_ui()->CallJavascriptFunction("updateAuthStatus", auth_status); | 233 web_ui()->CallJavascriptFunction("updateAuthStatus", auth_status); |
| 217 | 234 |
| 218 // Start updating the GCache contents section. | 235 // Start updating the GCache contents section. |
| 219 Profile* profile = Profile::FromWebUI(web_ui()); | 236 Profile* profile = Profile::FromWebUI(web_ui()); |
| 220 const FilePath root_path = | 237 const FilePath root_path = |
| 221 gdata::GDataCache::GetCacheRootPath(profile); | 238 gdata::GDataCache::GetCacheRootPath(profile); |
| 222 base::ListValue* gcache_contents = new ListValue; | 239 base::ListValue* gcache_contents = new ListValue; |
| 223 base::DictionaryValue* gcache_summary = new DictionaryValue; | 240 base::DictionaryValue* gcache_summary = new DictionaryValue; |
| 224 content::BrowserThread::PostBlockingPoolTaskAndReply( | 241 BrowserThread::PostBlockingPoolTaskAndReply( |
| 225 FROM_HERE, | 242 FROM_HERE, |
| 226 base::Bind(&GetGCacheContents, | 243 base::Bind(&GetGCacheContents, |
| 227 root_path, | 244 root_path, |
| 228 gcache_contents, | 245 gcache_contents, |
| 229 gcache_summary), | 246 gcache_summary), |
| 230 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, | 247 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, |
| 231 weak_ptr_factory_.GetWeakPtr(), | 248 weak_ptr_factory_.GetWeakPtr(), |
| 232 base::Owned(gcache_contents), | 249 base::Owned(gcache_contents), |
| 233 base::Owned(gcache_summary))); | 250 base::Owned(gcache_summary))); |
| 251 | |
| 252 // Propagate the amount of local free space in bytes. | |
| 253 FilePath path; | |
|
satorux1
2012/08/17 10:50:49
let's make it a bit more descriptive.
path -> hom
Haruki Sato
2012/08/17 15:08:45
Done.
| |
| 254 if (PathService::Get(base::DIR_HOME, &path)) { | |
| 255 base::DictionaryValue* local_storage_summary = new DictionaryValue; | |
| 256 BrowserThread::PostBlockingPoolTaskAndReply( | |
| 257 FROM_HERE, | |
| 258 base::Bind(&GetFreeDiskSpace, path, local_storage_summary), | |
| 259 base::Bind(&DriveInternalsWebUIHandler::OnGetFreeDiskSpace, | |
| 260 weak_ptr_factory_.GetWeakPtr(), | |
| 261 base::Owned(local_storage_summary))); | |
| 262 } else { | |
| 263 LOG(ERROR) << "Home directory not found"; | |
| 264 } | |
| 234 } | 265 } |
| 235 | 266 |
| 236 void DriveInternalsWebUIHandler::OnGetGCacheContents( | 267 void DriveInternalsWebUIHandler::OnGetGCacheContents( |
| 237 base::ListValue* gcache_contents, | 268 base::ListValue* gcache_contents, |
| 238 base::DictionaryValue* gcache_summary) { | 269 base::DictionaryValue* gcache_summary) { |
| 239 DCHECK(gcache_contents); | 270 DCHECK(gcache_contents); |
| 240 DCHECK(gcache_summary); | 271 DCHECK(gcache_summary); |
| 241 web_ui()->CallJavascriptFunction("updateGCacheContents", | 272 web_ui()->CallJavascriptFunction("updateGCacheContents", |
| 242 *gcache_contents, | 273 *gcache_contents, |
| 243 *gcache_summary); | 274 *gcache_summary); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 328 value.SetString("md5", cache_entry.md5()); | 359 value.SetString("md5", cache_entry.md5()); |
| 329 value.SetBoolean("is_present", cache_entry.is_present()); | 360 value.SetBoolean("is_present", cache_entry.is_present()); |
| 330 value.SetBoolean("is_pinned", cache_entry.is_pinned()); | 361 value.SetBoolean("is_pinned", cache_entry.is_pinned()); |
| 331 value.SetBoolean("is_dirty", cache_entry.is_dirty()); | 362 value.SetBoolean("is_dirty", cache_entry.is_dirty()); |
| 332 value.SetBoolean("is_mounted", cache_entry.is_mounted()); | 363 value.SetBoolean("is_mounted", cache_entry.is_mounted()); |
| 333 value.SetBoolean("is_persistent", cache_entry.is_persistent()); | 364 value.SetBoolean("is_persistent", cache_entry.is_persistent()); |
| 334 | 365 |
| 335 web_ui()->CallJavascriptFunction("updateCacheContents", value); | 366 web_ui()->CallJavascriptFunction("updateCacheContents", value); |
| 336 } | 367 } |
| 337 | 368 |
| 369 void DriveInternalsWebUIHandler::OnGetFreeDiskSpace( | |
| 370 base::DictionaryValue* local_storage_summary) { | |
| 371 DCHECK(local_storage_summary); | |
| 372 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
|
satorux1
2012/08/17 10:50:49
nit: reverse the two DCHECKs
Haruki Sato
2012/08/17 15:08:45
Done.
| |
| 373 | |
| 374 web_ui()->CallJavascriptFunction( | |
| 375 "updateLocalStorageUsage", *local_storage_summary); | |
| 376 } | |
| 377 | |
| 338 } // namespace | 378 } // namespace |
| 339 | 379 |
| 340 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) | 380 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) |
| 341 : WebUIController(web_ui) { | 381 : WebUIController(web_ui) { |
| 342 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); | 382 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); |
| 343 | 383 |
| 344 ChromeWebUIDataSource* source = | 384 ChromeWebUIDataSource* source = |
| 345 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); | 385 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); |
| 346 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); | 386 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); |
| 347 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); | 387 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); |
| 348 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); | 388 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); |
| 349 | 389 |
| 350 Profile* profile = Profile::FromWebUI(web_ui); | 390 Profile* profile = Profile::FromWebUI(web_ui); |
| 351 ChromeURLDataManager::AddDataSource(profile, source); | 391 ChromeURLDataManager::AddDataSource(profile, source); |
| 352 } | 392 } |
| 353 | 393 |
| 354 } // namespace chromeos | 394 } // namespace chromeos |
| OLD | NEW |