| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/chromeos/fsp_internals_ui.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/weak_ptr.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | |
| 11 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | |
| 12 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | |
| 13 #include "chrome/browser/chromeos/file_system_provider/service.h" | |
| 14 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "content/public/browser/web_ui.h" | |
| 19 #include "content/public/browser/web_ui_data_source.h" | |
| 20 #include "content/public/browser/web_ui_message_handler.h" | |
| 21 #include "grit/browser_resources.h" | |
| 22 | |
| 23 using content::BrowserThread; | |
| 24 | |
| 25 namespace chromeos { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Class to handle messages from chrome://fsp-internals. | |
| 30 class FSPInternalsWebUIHandler : public content::WebUIMessageHandler { | |
| 31 public: | |
| 32 FSPInternalsWebUIHandler() : weak_ptr_factory_(this) {} | |
| 33 | |
| 34 virtual ~FSPInternalsWebUIHandler() {} | |
| 35 | |
| 36 private: | |
| 37 // content::WebUIMessageHandler overrides. | |
| 38 virtual void RegisterMessages() OVERRIDE; | |
| 39 | |
| 40 // Gets a file system provider service for the current profile. If not found, | |
| 41 // then NULL. | |
| 42 file_system_provider::Service* GetService(); | |
| 43 | |
| 44 // Invoked when updating file system list is requested. | |
| 45 void OnUpdateFileSystems(const base::ListValue* args); | |
| 46 | |
| 47 base::WeakPtrFactory<FSPInternalsWebUIHandler> weak_ptr_factory_; | |
| 48 | |
| 49 DISALLOW_COPY_AND_ASSIGN(FSPInternalsWebUIHandler); | |
| 50 }; | |
| 51 | |
| 52 void FSPInternalsWebUIHandler::RegisterMessages() { | |
| 53 web_ui()->RegisterMessageCallback( | |
| 54 "updateFileSystems", | |
| 55 base::Bind(&FSPInternalsWebUIHandler::OnUpdateFileSystems, | |
| 56 weak_ptr_factory_.GetWeakPtr())); | |
| 57 } | |
| 58 | |
| 59 file_system_provider::Service* FSPInternalsWebUIHandler::GetService() { | |
| 60 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 61 | |
| 62 Profile* const profile = Profile::FromWebUI(web_ui()); | |
| 63 return file_system_provider::ServiceFactory::FindExisting(profile); | |
| 64 } | |
| 65 | |
| 66 void FSPInternalsWebUIHandler::OnUpdateFileSystems( | |
| 67 const base::ListValue* args) { | |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 69 | |
| 70 file_system_provider::Service* const service = GetService(); | |
| 71 if (!service) | |
| 72 return; | |
| 73 | |
| 74 base::ListValue items; | |
| 75 | |
| 76 const std::vector<file_system_provider::ProvidedFileSystemInfo> | |
| 77 file_system_info_list = service->GetProvidedFileSystemInfoList(); | |
| 78 | |
| 79 for (size_t i = 0; i < file_system_info_list.size(); ++i) { | |
| 80 const file_system_provider::ProvidedFileSystemInfo file_system_info = | |
| 81 file_system_info_list[i]; | |
| 82 | |
| 83 file_system_provider::ProvidedFileSystemInterface* const file_system = | |
| 84 service->GetProvidedFileSystem(file_system_info.extension_id(), | |
| 85 file_system_info.file_system_id()); | |
| 86 DCHECK(file_system); | |
| 87 | |
| 88 file_system_provider::RequestManager* const request_manager = | |
| 89 file_system->GetRequestManager(); | |
| 90 DCHECK(request_manager); | |
| 91 | |
| 92 base::DictionaryValue* item_value = new base::DictionaryValue(); | |
| 93 item_value->SetString("id", file_system_info.file_system_id()); | |
| 94 item_value->SetString("name", file_system_info.file_system_name()); | |
| 95 item_value->SetString("extensionId", file_system_info.extension_id()); | |
| 96 item_value->SetString("mountPath", | |
| 97 file_system_info.mount_path().AsUTF8Unsafe()); | |
| 98 item_value->SetInteger("activeRequests", | |
| 99 request_manager->GetActiveRequestsForLogging()); | |
| 100 | |
| 101 items.Append(item_value); | |
| 102 } | |
| 103 | |
| 104 web_ui()->CallJavascriptFunction("updateFileSystems", items); | |
| 105 } | |
| 106 | |
| 107 } // namespace | |
| 108 | |
| 109 FSPInternalsUI::FSPInternalsUI(content::WebUI* web_ui) | |
| 110 : WebUIController(web_ui) { | |
| 111 web_ui->AddMessageHandler(new FSPInternalsWebUIHandler()); | |
| 112 | |
| 113 content::WebUIDataSource* source = | |
| 114 content::WebUIDataSource::Create(chrome::kChromeUIFSPInternalsHost); | |
| 115 source->AddResourcePath("fsp_internals.css", IDR_FSP_INTERNALS_CSS); | |
| 116 source->AddResourcePath("fsp_internals.js", IDR_FSP_INTERNALS_JS); | |
| 117 source->SetDefaultResource(IDR_FSP_INTERNALS_HTML); | |
| 118 | |
| 119 Profile* profile = Profile::FromWebUI(web_ui); | |
| 120 content::WebUIDataSource::Add(profile, source); | |
| 121 } | |
| 122 | |
| 123 } // namespace chromeos | |
| OLD | NEW |