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/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
9 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" | 10 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" |
| 11 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
10 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" | 12 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" |
11 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
| 14 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
12 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
14 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
16 #include "content/public/browser/web_ui_message_handler.h" | 20 #include "content/public/browser/web_ui_message_handler.h" |
17 #include "grit/browser_resources.h" | 21 #include "grit/browser_resources.h" |
18 | 22 |
19 namespace chromeos { | 23 namespace chromeos { |
20 | 24 |
21 namespace { | 25 namespace { |
22 | 26 |
| 27 // Gets metadata of all files and directories in |root_path| |
| 28 // recursively. Stores the result as a list of dictionaries like: |
| 29 // |
| 30 // [{ path: 'GCache/v1/tmp/<resource_id>', |
| 31 // size: 12345, |
| 32 // is_directory: false, |
| 33 // last_modified: '2005-08-09T09:57:00-08:00', |
| 34 // },...] |
| 35 // |
| 36 // The list is sorted by the path. |
| 37 void GetGCacheContents(const FilePath& root_path, |
| 38 base::ListValue* gcache_contents) { |
| 39 using file_util::FileEnumerator; |
| 40 // Use this map to sort the result list by the path. |
| 41 std::map<FilePath, DictionaryValue*> files; |
| 42 |
| 43 const int options = (file_util::FileEnumerator::FILES | |
| 44 file_util::FileEnumerator::DIRECTORIES | |
| 45 file_util::FileEnumerator::SHOW_SYM_LINKS); |
| 46 FileEnumerator enumerator( |
| 47 root_path, |
| 48 true, // recursive |
| 49 static_cast<FileEnumerator::FileType>(options)); |
| 50 |
| 51 for (FilePath current = enumerator.Next(); !current.empty(); |
| 52 current = enumerator.Next()) { |
| 53 FileEnumerator::FindInfo find_info; |
| 54 enumerator.GetFindInfo(&find_info); |
| 55 int64 size = FileEnumerator::GetFilesize(find_info); |
| 56 const bool is_directory = FileEnumerator::IsDirectory(find_info); |
| 57 const bool is_symbolic_link = FileEnumerator::IsLink(find_info); |
| 58 const base::Time last_modified = |
| 59 FileEnumerator::GetLastModifiedTime(find_info); |
| 60 |
| 61 base::DictionaryValue* entry = new base::DictionaryValue; |
| 62 entry->SetString("path", current.value()); |
| 63 // Use double instead of integer for large files. |
| 64 entry->SetDouble("size", size); |
| 65 entry->SetBoolean("is_directory", is_directory); |
| 66 entry->SetBoolean("is_symbolic_link", is_symbolic_link); |
| 67 entry->SetString("last_modified", |
| 68 gdata::util::FormatTimeAsString(last_modified)); |
| 69 |
| 70 files[current] = entry; |
| 71 } |
| 72 |
| 73 // Convert |files| into |gcache_contents|. |
| 74 for (std::map<FilePath, DictionaryValue*>::const_iterator |
| 75 iter = files.begin(); iter != files.end(); ++iter) { |
| 76 gcache_contents->Append(iter->second); |
| 77 } |
| 78 } |
| 79 |
23 // Class to handle messages from chrome://drive-internals. | 80 // Class to handle messages from chrome://drive-internals. |
24 class DriveInternalsWebUIHandler : public content::WebUIMessageHandler { | 81 class DriveInternalsWebUIHandler : public content::WebUIMessageHandler { |
25 public: | 82 public: |
26 DriveInternalsWebUIHandler() | 83 DriveInternalsWebUIHandler() |
27 : weak_ptr_factory_(this) { | 84 : weak_ptr_factory_(this) { |
28 } | 85 } |
29 | 86 |
30 virtual ~DriveInternalsWebUIHandler() { | 87 virtual ~DriveInternalsWebUIHandler() { |
31 } | 88 } |
32 | 89 |
(...skipping 19 matching lines...) Expand all Loading... |
52 system_service->docs_service(); | 109 system_service->docs_service(); |
53 DCHECK(documents_service); | 110 DCHECK(documents_service); |
54 | 111 |
55 // Update the auth status section. | 112 // Update the auth status section. |
56 base::DictionaryValue auth_status; | 113 base::DictionaryValue auth_status; |
57 auth_status.SetBoolean("has-refresh-token", | 114 auth_status.SetBoolean("has-refresh-token", |
58 documents_service->HasRefreshToken()); | 115 documents_service->HasRefreshToken()); |
59 auth_status.SetBoolean("has-access-token", | 116 auth_status.SetBoolean("has-access-token", |
60 documents_service->HasAccessToken()); | 117 documents_service->HasAccessToken()); |
61 web_ui()->CallJavascriptFunction("UpdateAuthStatus", auth_status); | 118 web_ui()->CallJavascriptFunction("UpdateAuthStatus", auth_status); |
| 119 |
| 120 // Start updating the GCache contents section. |
| 121 const FilePath root_path = |
| 122 gdata::GDataCache::GetCacheRootPath(profile); |
| 123 base::ListValue* gcache_contents = new ListValue; |
| 124 content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 125 FROM_HERE, |
| 126 base::Bind(&GetGCacheContents, root_path, gcache_contents), |
| 127 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, |
| 128 weak_ptr_factory_.GetWeakPtr(), |
| 129 base::Owned(gcache_contents))); |
| 130 } |
| 131 |
| 132 // Called when GetGCacheContents() is complete. |
| 133 void OnGetGCacheContents(base::ListValue* gcache_contents) { |
| 134 DCHECK(gcache_contents); |
| 135 web_ui()->CallJavascriptFunction("UpdateGCacheContents", *gcache_contents); |
62 } | 136 } |
63 | 137 |
64 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; | 138 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; |
65 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); | 139 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); |
66 }; | 140 }; |
67 | 141 |
68 } // namespace | 142 } // namespace |
69 | 143 |
70 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) | 144 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) |
71 : WebUIController(web_ui) { | 145 : WebUIController(web_ui) { |
72 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); | 146 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); |
73 | 147 |
74 ChromeWebUIDataSource* source = | 148 ChromeWebUIDataSource* source = |
75 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); | 149 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); |
| 150 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); |
76 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); | 151 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); |
77 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); | 152 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); |
78 | 153 |
79 Profile* profile = Profile::FromWebUI(web_ui); | 154 Profile* profile = Profile::FromWebUI(web_ui); |
80 ChromeURLDataManager::AddDataSource(profile, source); | 155 ChromeURLDataManager::AddDataSource(profile, source); |
81 } | 156 } |
82 | 157 |
83 } // namespace chromeos | 158 } // namespace chromeos |
OLD | NEW |