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" | |
| 10 #include "base/stringprintf.h" | |
| 9 #include "base/memory/weak_ptr.h" | 11 #include "base/memory/weak_ptr.h" |
| 12 #include "chrome/browser/chromeos/gdata/gdata.pb.h" | |
| 10 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" | 13 #include "chrome/browser/chromeos/gdata/gdata_auth_service.h" |
| 11 #include "chrome/browser/chromeos/gdata/gdata_cache.h" | 14 #include "chrome/browser/chromeos/gdata/gdata_cache.h" |
| 12 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" | 15 #include "chrome/browser/chromeos/gdata/gdata_documents_service.h" |
| 16 #include "chrome/browser/chromeos/gdata/gdata_file_system_interface.h" | |
| 13 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 17 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
| 14 #include "chrome/browser/chromeos/gdata/gdata_util.h" | 18 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 20 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
| 17 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
| 20 #include "content/public/browser/web_ui_message_handler.h" | 24 #include "content/public/browser/web_ui_message_handler.h" |
| 21 #include "grit/browser_resources.h" | 25 #include "grit/browser_resources.h" |
| 22 | 26 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 | 93 |
| 90 private: | 94 private: |
| 91 // WebUIMessageHandler override. | 95 // WebUIMessageHandler override. |
| 92 virtual void RegisterMessages() OVERRIDE { | 96 virtual void RegisterMessages() OVERRIDE { |
| 93 web_ui()->RegisterMessageCallback( | 97 web_ui()->RegisterMessageCallback( |
| 94 "pageLoaded", | 98 "pageLoaded", |
| 95 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded, | 99 base::Bind(&DriveInternalsWebUIHandler::OnPageLoaded, |
| 96 weak_ptr_factory_.GetWeakPtr())); | 100 weak_ptr_factory_.GetWeakPtr())); |
| 97 } | 101 } |
| 98 | 102 |
| 103 // Returns a GDataSystemService. | |
| 104 gdata::GDataSystemService* GetSystemService() { | |
| 105 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 106 return gdata::GDataSystemServiceFactory::GetForProfile(profile); | |
| 107 } | |
| 108 | |
| 99 // Called when the page is first loaded. | 109 // Called when the page is first loaded. |
| 100 void OnPageLoaded(const base::ListValue* args) { | 110 void OnPageLoaded(const base::ListValue* args) { |
| 101 Profile* profile = Profile::FromWebUI(web_ui()); | 111 gdata::GDataSystemService* system_service = GetSystemService(); |
| 102 gdata::GDataSystemService* system_service = | |
| 103 gdata::GDataSystemServiceFactory::GetForProfile(profile); | |
| 104 // |system_service| may be NULL in the guest/incognito mode. | 112 // |system_service| may be NULL in the guest/incognito mode. |
| 105 if (!system_service) | 113 if (!system_service) |
| 106 return; | 114 return; |
| 107 | 115 |
| 108 gdata::DocumentsServiceInterface* documents_service = | 116 gdata::DocumentsServiceInterface* documents_service = |
| 109 system_service->docs_service(); | 117 system_service->docs_service(); |
| 110 DCHECK(documents_service); | 118 DCHECK(documents_service); |
| 111 | 119 |
| 112 // Update the auth status section. | 120 // Update the auth status section. |
| 113 base::DictionaryValue auth_status; | 121 base::DictionaryValue auth_status; |
| 114 auth_status.SetBoolean("has-refresh-token", | 122 auth_status.SetBoolean("has-refresh-token", |
| 115 documents_service->HasRefreshToken()); | 123 documents_service->HasRefreshToken()); |
| 116 auth_status.SetBoolean("has-access-token", | 124 auth_status.SetBoolean("has-access-token", |
| 117 documents_service->HasAccessToken()); | 125 documents_service->HasAccessToken()); |
| 118 web_ui()->CallJavascriptFunction("UpdateAuthStatus", auth_status); | 126 web_ui()->CallJavascriptFunction("UpdateAuthStatus", auth_status); |
| 119 | 127 |
| 120 // Start updating the GCache contents section. | 128 // Start updating the GCache contents section. |
| 129 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 121 const FilePath root_path = | 130 const FilePath root_path = |
| 122 gdata::GDataCache::GetCacheRootPath(profile); | 131 gdata::GDataCache::GetCacheRootPath(profile); |
| 123 base::ListValue* gcache_contents = new ListValue; | 132 base::ListValue* gcache_contents = new ListValue; |
| 124 content::BrowserThread::PostBlockingPoolTaskAndReply( | 133 content::BrowserThread::PostBlockingPoolTaskAndReply( |
| 125 FROM_HERE, | 134 FROM_HERE, |
| 126 base::Bind(&GetGCacheContents, root_path, gcache_contents), | 135 base::Bind(&GetGCacheContents, root_path, gcache_contents), |
| 127 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, | 136 base::Bind(&DriveInternalsWebUIHandler::OnGetGCacheContents, |
| 128 weak_ptr_factory_.GetWeakPtr(), | 137 weak_ptr_factory_.GetWeakPtr(), |
| 129 base::Owned(gcache_contents))); | 138 base::Owned(gcache_contents))); |
| 130 } | 139 } |
| 131 | 140 |
| 132 // Called when GetGCacheContents() is complete. | 141 // Called when GetGCacheContents() is complete. |
| 133 void OnGetGCacheContents(base::ListValue* gcache_contents) { | 142 void OnGetGCacheContents(base::ListValue* gcache_contents) { |
| 134 DCHECK(gcache_contents); | 143 DCHECK(gcache_contents); |
| 135 web_ui()->CallJavascriptFunction("UpdateGCacheContents", *gcache_contents); | 144 web_ui()->CallJavascriptFunction("UpdateGCacheContents", *gcache_contents); |
| 145 | |
| 146 // Start updating the file system tree section, if we have access token. | |
| 147 gdata::GDataSystemService* system_service = GetSystemService(); | |
| 148 if (!system_service->docs_service()->HasAccessToken()) | |
| 149 return; | |
| 150 | |
| 151 // Start rendering the file system tree as text. | |
| 152 const FilePath root_path = FilePath(gdata::kGDataRootDirectory); | |
| 153 system_service->file_system()->ReadDirectoryByPath( | |
| 154 root_path, | |
| 155 base::Bind(&DriveInternalsWebUIHandler::OnReadDirectoryByPath, | |
| 156 weak_ptr_factory_.GetWeakPtr(), | |
| 157 root_path)); | |
| 136 } | 158 } |
| 137 | 159 |
| 160 // Called when ReadDirectoryByPath() is complete. | |
| 161 void OnReadDirectoryByPath( | |
|
achuithb
2012/07/26 23:00:23
I think it's more readable to split the definition
satorux1
2012/07/27 16:13:44
Done.
| |
| 162 const FilePath& parent_path, | |
| 163 gdata::GDataFileError error, | |
| 164 bool hide_hosted_documents, | |
| 165 scoped_ptr<gdata::GDataEntryProtoVector> entries) { | |
| 166 if (error != gdata::GDATA_FILE_OK) | |
| 167 return; | |
| 168 | |
| 169 DCHECK(entries.get()); | |
| 170 for (size_t i = 0; i < entries->size(); ++i) { | |
| 171 const gdata::GDataEntryProto& entry = (*entries)[i]; | |
| 172 const FilePath current_path = parent_path.Append( | |
| 173 FilePath::FromUTF8Unsafe(entry.base_name())); | |
| 174 | |
| 175 file_system_as_text_.append(FormatEntry(current_path, entry) + "\n"); | |
| 176 | |
| 177 if (entry.file_info().is_directory()) { | |
| 178 GetSystemService()->file_system()->ReadDirectoryByPath( | |
| 179 current_path, | |
| 180 base::Bind(&DriveInternalsWebUIHandler::OnReadDirectoryByPath, | |
| 181 weak_ptr_factory_.GetWeakPtr(), | |
| 182 current_path)); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 // There may be pending ReadDirectoryByPath() calls, but we can update | |
| 187 // the page with what we have now. This results in progressive | |
| 188 // updates, which is good for a large file system. | |
| 189 const base::StringValue value(file_system_as_text_); | |
| 190 web_ui()->CallJavascriptFunction("UpdateFileSystemContents", value); | |
| 191 } | |
| 192 | |
| 193 // Formats |entry| into text. | |
| 194 std::string FormatEntry(const FilePath& path, | |
|
satorux1
2012/07/27 16:13:44
moved this out of the class.
| |
| 195 const gdata::GDataEntryProto& entry) { | |
| 196 using base::StringAppendF; | |
| 197 using gdata::util::FormatTimeAsString; | |
| 198 | |
| 199 std::string out; | |
| 200 StringAppendF(&out, "%s\n", path.AsUTF8Unsafe().c_str()); | |
| 201 StringAppendF(&out, " title: %s\n", entry.title().c_str()); | |
| 202 StringAppendF(&out, " resource_id: %s\n", entry.resource_id().c_str()); | |
| 203 StringAppendF(&out, " edit_url: %s\n", entry.edit_url().c_str()); | |
| 204 StringAppendF(&out, " content_url: %s\n", entry.content_url().c_str()); | |
| 205 StringAppendF(&out, " parent_resource_id: %s\n", | |
| 206 entry.parent_resource_id().c_str()); | |
| 207 StringAppendF(&out, " upload_url: %s\n", entry.upload_url().c_str()); | |
| 208 | |
| 209 const gdata::PlatformFileInfoProto& file_info = entry.file_info(); | |
| 210 StringAppendF(&out, " file_info\n"); | |
| 211 StringAppendF(&out, " size: %"PRId64"\n", file_info.size()); | |
| 212 StringAppendF(&out, " is_directory: %d\n", file_info.is_directory()); | |
| 213 StringAppendF(&out, " is_symbolic_link: %d\n", | |
| 214 file_info.is_symbolic_link()); | |
| 215 | |
| 216 const base::Time last_modified = base::Time::FromInternalValue( | |
| 217 file_info.last_modified()); | |
| 218 const base::Time last_accessed = base::Time::FromInternalValue( | |
| 219 file_info.last_accessed()); | |
| 220 const base::Time creation_time = base::Time::FromInternalValue( | |
| 221 file_info.creation_time()); | |
| 222 StringAppendF(&out, " last_modified: %s\n", | |
| 223 FormatTimeAsString(last_modified).c_str()); | |
| 224 StringAppendF(&out, " last_accessed: %s\n", | |
| 225 FormatTimeAsString(last_accessed).c_str()); | |
| 226 StringAppendF(&out, " creation_time: %s\n", | |
| 227 FormatTimeAsString(creation_time).c_str()); | |
| 228 | |
| 229 if (entry.has_file_specific_info()) { | |
| 230 const gdata::GDataFileSpecificInfo& file_specific_info = | |
| 231 entry.file_specific_info(); | |
| 232 StringAppendF(&out, " thumbnail_url: %s\n", | |
| 233 file_specific_info.thumbnail_url().c_str()); | |
| 234 StringAppendF(&out, " alternate_url: %s\n", | |
| 235 file_specific_info.alternate_url().c_str()); | |
| 236 StringAppendF(&out, " content_mime_type: %s\n", | |
| 237 file_specific_info.content_mime_type().c_str()); | |
| 238 StringAppendF(&out, " file_md5: %s\n", | |
| 239 file_specific_info.file_md5().c_str()); | |
| 240 StringAppendF(&out, " document_extension: %s\n", | |
| 241 file_specific_info.document_extension().c_str()); | |
| 242 StringAppendF(&out, " is_hosted_document: %d\n", | |
| 243 file_specific_info.is_hosted_document()); | |
| 244 } | |
| 245 | |
| 246 return out; | |
| 247 } | |
| 248 | |
| 249 std::string file_system_as_text_; | |
|
satorux1
2012/07/27 16:13:44
removed this. instead of sending the entire file
| |
| 138 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; | 250 base::WeakPtrFactory<DriveInternalsWebUIHandler> weak_ptr_factory_; |
| 139 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); | 251 DISALLOW_COPY_AND_ASSIGN(DriveInternalsWebUIHandler); |
| 140 }; | 252 }; |
| 141 | 253 |
| 142 } // namespace | 254 } // namespace |
| 143 | 255 |
| 144 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) | 256 DriveInternalsUI::DriveInternalsUI(content::WebUI* web_ui) |
| 145 : WebUIController(web_ui) { | 257 : WebUIController(web_ui) { |
| 146 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); | 258 web_ui->AddMessageHandler(new DriveInternalsWebUIHandler()); |
| 147 | 259 |
| 148 ChromeWebUIDataSource* source = | 260 ChromeWebUIDataSource* source = |
| 149 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); | 261 new ChromeWebUIDataSource(chrome::kChromeUIDriveInternalsHost); |
| 150 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); | 262 source->add_resource_path("drive_internals.css", IDR_DRIVE_INTERNALS_CSS); |
| 151 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); | 263 source->add_resource_path("drive_internals.js", IDR_DRIVE_INTERNALS_JS); |
| 152 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); | 264 source->set_default_resource(IDR_DRIVE_INTERNALS_HTML); |
| 153 | 265 |
| 154 Profile* profile = Profile::FromWebUI(web_ui); | 266 Profile* profile = Profile::FromWebUI(web_ui); |
| 155 ChromeURLDataManager::AddDataSource(profile, source); | 267 ChromeURLDataManager::AddDataSource(profile, source); |
| 156 } | 268 } |
| 157 | 269 |
| 158 } // namespace chromeos | 270 } // namespace chromeos |
| OLD | NEW |