OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/provided_file_systems_ui.h" | 5 #include "chrome/browser/ui/webui/chromeos/provided_file_systems_ui.h" |
6 | 6 |
| 7 #include <string> |
7 #include <vector> | 8 #include <vector> |
8 | 9 |
9 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/files/file.h" |
| 12 #include "base/memory/scoped_ptr.h" |
10 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
11 #include "base/values.h" | 14 #include "base/values.h" |
12 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" | 15 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_info
.h" |
13 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" | 16 #include "chrome/browser/chromeos/file_system_provider/provided_file_system_inte
rface.h" |
14 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" | 17 #include "chrome/browser/chromeos/file_system_provider/request_manager.h" |
15 #include "chrome/browser/chromeos/file_system_provider/service.h" | 18 #include "chrome/browser/chromeos/file_system_provider/service.h" |
16 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" | 19 #include "chrome/browser/chromeos/file_system_provider/service_factory.h" |
17 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/url_constants.h" | 21 #include "chrome/common/url_constants.h" |
19 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
21 #include "content/public/browser/web_ui_data_source.h" | 24 #include "content/public/browser/web_ui_data_source.h" |
22 #include "content/public/browser/web_ui_message_handler.h" | 25 #include "content/public/browser/web_ui_message_handler.h" |
23 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
24 | 27 |
25 using content::BrowserThread; | 28 using content::BrowserThread; |
26 | 29 |
27 namespace chromeos { | 30 namespace chromeos { |
28 | 31 |
29 namespace { | 32 namespace { |
30 | 33 |
| 34 const char kKeyId[] = "id"; |
| 35 const char kKeyEventType[] = "eventType"; |
| 36 const char kKeyRequestType[] = "requestType"; |
| 37 const char kKeyTime[] = "time"; |
| 38 const char kKeyHasMore[] = "hasMore"; |
| 39 const char kKeyError[] = "error"; |
| 40 |
| 41 const char kKeyName[] = "name"; |
| 42 const char kKeyExtensionId[] = "extensionId"; |
| 43 const char kKeyMountPath[] = "mountPath"; |
| 44 const char kKeyActiveRequests[] = "activeRequests"; |
| 45 |
| 46 const char kRequestCreated[] = "created"; |
| 47 const char kRequestDestroyed[] = "destroyed"; |
| 48 const char kRequestExecuted[] = "executed"; |
| 49 const char kRequestFulfilled[] = "fulfilled"; |
| 50 const char kRequestRejected[] = "rejected"; |
| 51 const char kRequestTimeouted[] = "timeouted"; |
| 52 |
| 53 const char kFunctionOnRequestEvent[] = "onRequestEvent"; |
| 54 const char kFunctionUpdateFileSystems[] = "updateFileSystems"; |
| 55 const char kFunctionSelectFileSystem[] = "selectFileSystem"; |
| 56 |
| 57 // Creates a dictionary holding common fields for the onRequest* events. |
| 58 scoped_ptr<base::DictionaryValue> CreateRequestEvent(const std::string& type, |
| 59 int request_id) { |
| 60 scoped_ptr<base::DictionaryValue> event(new base::DictionaryValue); |
| 61 event->SetInteger(kKeyId, request_id); |
| 62 event->SetString(kKeyEventType, type); |
| 63 event->SetDouble(kKeyTime, base::Time::Now().ToJsTime()); |
| 64 return event.Pass(); |
| 65 } |
| 66 |
31 // Class to handle messages from chrome://provided-file-systems. | 67 // Class to handle messages from chrome://provided-file-systems. |
32 class ProvidedFileSystemsWebUIHandler : public content::WebUIMessageHandler { | 68 class ProvidedFileSystemsWebUIHandler |
| 69 : public content::WebUIMessageHandler, |
| 70 public file_system_provider::RequestManager::Observer { |
33 public: | 71 public: |
34 ProvidedFileSystemsWebUIHandler() : weak_ptr_factory_(this) {} | 72 ProvidedFileSystemsWebUIHandler() : weak_ptr_factory_(this) {} |
35 | 73 |
36 virtual ~ProvidedFileSystemsWebUIHandler() {} | 74 virtual ~ProvidedFileSystemsWebUIHandler(); |
| 75 |
| 76 // RequestManager::Observer overrides. |
| 77 virtual void OnRequestCreated( |
| 78 int request_id, |
| 79 file_system_provider::RequestType type) OVERRIDE; |
| 80 virtual void OnRequestDestroyed(int request_id) OVERRIDE; |
| 81 virtual void OnRequestExecuted(int request_id) OVERRIDE; |
| 82 virtual void OnRequestFulfilled(int request_id, bool has_more) OVERRIDE; |
| 83 virtual void OnRequestRejected(int request_id, |
| 84 base::File::Error error) OVERRIDE; |
| 85 virtual void OnRequestTimeouted(int request_id) OVERRIDE; |
37 | 86 |
38 private: | 87 private: |
39 // content::WebUIMessageHandler overrides. | 88 // content::WebUIMessageHandler overrides. |
40 virtual void RegisterMessages() OVERRIDE; | 89 virtual void RegisterMessages() OVERRIDE; |
41 | 90 |
42 // Gets a file system provider service for the current profile. If not found, | 91 // Gets a file system provider service for the current profile. If not found, |
43 // then NULL. | 92 // then NULL. |
44 file_system_provider::Service* GetService(); | 93 file_system_provider::Service* GetService(); |
45 | 94 |
46 // Invoked when updating file system list is requested. | 95 // Invoked when updating file system list is requested. |
47 void OnUpdateFileSystems(const base::ListValue* args); | 96 void UpdateFileSystems(const base::ListValue* args); |
48 | 97 |
| 98 // Invoked when a file system is selected from the list. |
| 99 void SelectFileSystem(const base::ListValue* args); |
| 100 |
| 101 std::string selected_extension_id; |
| 102 std::string selected_file_system_id; |
49 base::WeakPtrFactory<ProvidedFileSystemsWebUIHandler> weak_ptr_factory_; | 103 base::WeakPtrFactory<ProvidedFileSystemsWebUIHandler> weak_ptr_factory_; |
50 | 104 |
51 DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystemsWebUIHandler); | 105 DISALLOW_COPY_AND_ASSIGN(ProvidedFileSystemsWebUIHandler); |
52 }; | 106 }; |
53 | 107 |
| 108 ProvidedFileSystemsWebUIHandler::~ProvidedFileSystemsWebUIHandler() { |
| 109 // Stop observing the currently selected file system. |
| 110 file_system_provider::Service* const service = GetService(); |
| 111 if (!service) |
| 112 return; |
| 113 |
| 114 file_system_provider::ProvidedFileSystemInterface* const file_system = |
| 115 service->GetProvidedFileSystem(selected_extension_id, |
| 116 selected_file_system_id); |
| 117 |
| 118 if (file_system) { |
| 119 file_system_provider::RequestManager* const request_manager = |
| 120 file_system->GetRequestManager(); |
| 121 DCHECK(request_manager); |
| 122 request_manager->RemoveObserver(this); |
| 123 } |
| 124 } |
| 125 |
| 126 void ProvidedFileSystemsWebUIHandler::OnRequestCreated( |
| 127 int request_id, |
| 128 file_system_provider::RequestType type) { |
| 129 scoped_ptr<base::DictionaryValue> const event = |
| 130 CreateRequestEvent(kRequestCreated, request_id); |
| 131 event->SetString(kKeyRequestType, |
| 132 file_system_provider::RequestTypeToString(type)); |
| 133 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 134 } |
| 135 |
| 136 void ProvidedFileSystemsWebUIHandler::OnRequestDestroyed(int request_id) { |
| 137 scoped_ptr<base::DictionaryValue> const event = |
| 138 CreateRequestEvent(kRequestDestroyed, request_id); |
| 139 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 140 } |
| 141 |
| 142 void ProvidedFileSystemsWebUIHandler::OnRequestExecuted(int request_id) { |
| 143 scoped_ptr<base::DictionaryValue> const event = |
| 144 CreateRequestEvent(kRequestExecuted, request_id); |
| 145 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 146 } |
| 147 |
| 148 void ProvidedFileSystemsWebUIHandler::OnRequestFulfilled(int request_id, |
| 149 bool has_more) { |
| 150 scoped_ptr<base::DictionaryValue> const event = |
| 151 CreateRequestEvent(kRequestFulfilled, request_id); |
| 152 event->SetBoolean(kKeyHasMore, has_more); |
| 153 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 154 } |
| 155 |
| 156 void ProvidedFileSystemsWebUIHandler::OnRequestRejected( |
| 157 int request_id, |
| 158 base::File::Error error) { |
| 159 scoped_ptr<base::DictionaryValue> const event = |
| 160 CreateRequestEvent(kRequestRejected, request_id); |
| 161 event->SetString(kKeyError, base::File::ErrorToString(error)); |
| 162 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 163 } |
| 164 |
| 165 void ProvidedFileSystemsWebUIHandler::OnRequestTimeouted(int request_id) { |
| 166 scoped_ptr<base::DictionaryValue> const event = |
| 167 CreateRequestEvent(kRequestTimeouted, request_id); |
| 168 web_ui()->CallJavascriptFunction(kFunctionOnRequestEvent, *event); |
| 169 } |
| 170 |
54 void ProvidedFileSystemsWebUIHandler::RegisterMessages() { | 171 void ProvidedFileSystemsWebUIHandler::RegisterMessages() { |
55 web_ui()->RegisterMessageCallback( | 172 web_ui()->RegisterMessageCallback( |
56 "updateFileSystems", | 173 kFunctionUpdateFileSystems, |
57 base::Bind(&ProvidedFileSystemsWebUIHandler::OnUpdateFileSystems, | 174 base::Bind(&ProvidedFileSystemsWebUIHandler::UpdateFileSystems, |
| 175 weak_ptr_factory_.GetWeakPtr())); |
| 176 web_ui()->RegisterMessageCallback( |
| 177 kFunctionSelectFileSystem, |
| 178 base::Bind(&ProvidedFileSystemsWebUIHandler::SelectFileSystem, |
58 weak_ptr_factory_.GetWeakPtr())); | 179 weak_ptr_factory_.GetWeakPtr())); |
59 } | 180 } |
60 | 181 |
61 file_system_provider::Service* ProvidedFileSystemsWebUIHandler::GetService() { | 182 file_system_provider::Service* ProvidedFileSystemsWebUIHandler::GetService() { |
62 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 183 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
63 | 184 |
64 Profile* const profile = Profile::FromWebUI(web_ui()); | 185 Profile* const profile = Profile::FromWebUI(web_ui()); |
65 return file_system_provider::ServiceFactory::FindExisting(profile); | 186 return file_system_provider::ServiceFactory::FindExisting(profile); |
66 } | 187 } |
67 | 188 |
68 void ProvidedFileSystemsWebUIHandler::OnUpdateFileSystems( | 189 void ProvidedFileSystemsWebUIHandler::UpdateFileSystems( |
69 const base::ListValue* args) { | 190 const base::ListValue* args) { |
70 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 191 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
71 | 192 |
72 file_system_provider::Service* const service = GetService(); | 193 file_system_provider::Service* const service = GetService(); |
73 if (!service) | 194 if (!service) |
74 return; | 195 return; |
75 | 196 |
76 base::ListValue items; | 197 base::ListValue items; |
77 | 198 |
78 const std::vector<file_system_provider::ProvidedFileSystemInfo> | 199 const std::vector<file_system_provider::ProvidedFileSystemInfo> |
79 file_system_info_list = service->GetProvidedFileSystemInfoList(); | 200 file_system_info_list = service->GetProvidedFileSystemInfoList(); |
80 | 201 |
81 for (size_t i = 0; i < file_system_info_list.size(); ++i) { | 202 for (size_t i = 0; i < file_system_info_list.size(); ++i) { |
82 const file_system_provider::ProvidedFileSystemInfo file_system_info = | 203 const file_system_provider::ProvidedFileSystemInfo file_system_info = |
83 file_system_info_list[i]; | 204 file_system_info_list[i]; |
84 | 205 |
85 file_system_provider::ProvidedFileSystemInterface* const file_system = | 206 file_system_provider::ProvidedFileSystemInterface* const file_system = |
86 service->GetProvidedFileSystem(file_system_info.extension_id(), | 207 service->GetProvidedFileSystem(file_system_info.extension_id(), |
87 file_system_info.file_system_id()); | 208 file_system_info.file_system_id()); |
88 DCHECK(file_system); | 209 DCHECK(file_system); |
89 | 210 |
90 file_system_provider::RequestManager* const request_manager = | 211 file_system_provider::RequestManager* const request_manager = |
91 file_system->GetRequestManager(); | 212 file_system->GetRequestManager(); |
92 DCHECK(request_manager); | 213 DCHECK(request_manager); |
93 | 214 |
94 base::DictionaryValue* item_value = new base::DictionaryValue(); | 215 base::DictionaryValue* item = new base::DictionaryValue(); |
95 item_value->SetString("id", file_system_info.file_system_id()); | 216 item->SetString(kKeyId, file_system_info.file_system_id()); |
96 item_value->SetString("name", file_system_info.file_system_name()); | 217 item->SetString(kKeyName, file_system_info.file_system_name()); |
97 item_value->SetString("extensionId", file_system_info.extension_id()); | 218 item->SetString(kKeyExtensionId, file_system_info.extension_id()); |
98 item_value->SetString("mountPath", | 219 item->SetString(kKeyMountPath, |
99 file_system_info.mount_path().AsUTF8Unsafe()); | 220 file_system_info.mount_path().AsUTF8Unsafe()); |
100 item_value->SetInteger("activeRequests", | 221 item->SetInteger(kKeyActiveRequests, |
101 request_manager->GetActiveRequestsForLogging()); | 222 request_manager->GetActiveRequestsForLogging()); |
102 | 223 |
103 items.Append(item_value); | 224 items.Append(item); |
104 } | 225 } |
105 | 226 |
106 web_ui()->CallJavascriptFunction("updateFileSystems", items); | 227 web_ui()->CallJavascriptFunction(kFunctionUpdateFileSystems, items); |
| 228 } |
| 229 |
| 230 void ProvidedFileSystemsWebUIHandler::SelectFileSystem( |
| 231 const base::ListValue* args) { |
| 232 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 233 |
| 234 file_system_provider::Service* const service = GetService(); |
| 235 if (!service) |
| 236 return; |
| 237 |
| 238 std::string extension_id; |
| 239 if (!args->GetString(0, &extension_id)) |
| 240 return; |
| 241 |
| 242 std::string file_system_id; |
| 243 if (!args->GetString(1, &file_system_id)) |
| 244 return; |
| 245 |
| 246 // Stop observing the previously selected request manager. |
| 247 { |
| 248 file_system_provider::ProvidedFileSystemInterface* const file_system = |
| 249 service->GetProvidedFileSystem(selected_extension_id, |
| 250 selected_file_system_id); |
| 251 if (file_system) { |
| 252 file_system_provider::RequestManager* const request_manager = |
| 253 file_system->GetRequestManager(); |
| 254 DCHECK(request_manager); |
| 255 request_manager->RemoveObserver(this); |
| 256 } |
| 257 } |
| 258 |
| 259 // Observe the selected file system. |
| 260 file_system_provider::ProvidedFileSystemInterface* const file_system = |
| 261 service->GetProvidedFileSystem(extension_id, file_system_id); |
| 262 if (!file_system) |
| 263 return; |
| 264 |
| 265 file_system_provider::RequestManager* const request_manager = |
| 266 file_system->GetRequestManager(); |
| 267 DCHECK(request_manager); |
| 268 |
| 269 request_manager->AddObserver(this); |
| 270 selected_extension_id = extension_id; |
| 271 selected_file_system_id = file_system_id; |
107 } | 272 } |
108 | 273 |
109 } // namespace | 274 } // namespace |
110 | 275 |
111 ProvidedFileSystemsUI::ProvidedFileSystemsUI(content::WebUI* web_ui) | 276 ProvidedFileSystemsUI::ProvidedFileSystemsUI(content::WebUI* web_ui) |
112 : WebUIController(web_ui) { | 277 : WebUIController(web_ui) { |
113 web_ui->AddMessageHandler(new ProvidedFileSystemsWebUIHandler()); | 278 web_ui->AddMessageHandler(new ProvidedFileSystemsWebUIHandler()); |
114 | 279 |
115 content::WebUIDataSource* source = content::WebUIDataSource::Create( | 280 content::WebUIDataSource* source = content::WebUIDataSource::Create( |
116 chrome::kChromeUIProvidedFileSystemsHost); | 281 chrome::kChromeUIProvidedFileSystemsHost); |
117 source->AddResourcePath("provided_file_systems.css", | 282 source->AddResourcePath("provided_file_systems.css", |
118 IDR_PROVIDED_FILE_SYSTEMS_CSS); | 283 IDR_PROVIDED_FILE_SYSTEMS_CSS); |
119 source->AddResourcePath("provided_file_systems.js", | 284 source->AddResourcePath("provided_file_systems.js", |
120 IDR_PROVIDED_FILE_SYSTEMS_JS); | 285 IDR_PROVIDED_FILE_SYSTEMS_JS); |
121 source->SetDefaultResource(IDR_PROVIDED_FILE_SYSTEMS_HTML); | 286 source->SetDefaultResource(IDR_PROVIDED_FILE_SYSTEMS_HTML); |
122 | 287 |
123 Profile* profile = Profile::FromWebUI(web_ui); | 288 Profile* profile = Profile::FromWebUI(web_ui); |
124 content::WebUIDataSource::Add(profile, source); | 289 content::WebUIDataSource::Add(profile, source); |
125 } | 290 } |
126 | 291 |
127 } // namespace chromeos | 292 } // namespace chromeos |
OLD | NEW |