OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/offline_internals_ui.h" | 5 #include "chrome/browser/ui/webui/offline_internals_ui.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
14 #include "base/strings/string_number_conversions.h" | 14 #include "base/strings/string_number_conversions.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
18 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
20 #include "components/offline_pages/background/request_coordinator.h" | 20 #include "components/offline_pages/background/request_coordinator.h" |
21 #include "components/offline_pages/background/save_page_request.h" | 21 #include "components/offline_pages/background/save_page_request.h" |
| 22 #include "components/offline_pages/client_namespace_constants.h" |
22 #include "components/offline_pages/offline_page_model.h" | 23 #include "components/offline_pages/offline_page_model.h" |
23 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
24 #include "content/public/browser/web_ui_controller.h" | 25 #include "content/public/browser/web_ui_controller.h" |
25 #include "content/public/browser/web_ui_data_source.h" | 26 #include "content/public/browser/web_ui_data_source.h" |
26 #include "content/public/browser/web_ui_message_handler.h" | 27 #include "content/public/browser/web_ui_message_handler.h" |
27 #include "grit/browser_resources.h" | 28 #include "grit/browser_resources.h" |
| 29 #include "net/base/network_change_notifier.h" |
28 | 30 |
29 namespace { | 31 namespace { |
30 | 32 |
31 // Class acting as a controller of the chrome://offline-internals WebUI. | 33 // Class acting as a controller of the chrome://offline-internals WebUI. |
| 34 // TODO(chili): Should split this file and move to webui/offline_internals/ |
32 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { | 35 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { |
33 public: | 36 public: |
34 OfflineInternalsUIMessageHandler(); | 37 OfflineInternalsUIMessageHandler(); |
35 ~OfflineInternalsUIMessageHandler() override; | 38 ~OfflineInternalsUIMessageHandler() override; |
36 | 39 |
37 // WebUIMessageHandler implementation. | 40 // WebUIMessageHandler implementation. |
38 void RegisterMessages() override; | 41 void RegisterMessages() override; |
39 | 42 |
40 private: | 43 private: |
41 // Deletes all the pages in the store. | 44 // Deletes all the pages in the store. |
(...skipping 13 matching lines...) Expand all Loading... |
55 | 58 |
56 // Set whether to record request queue events. | 59 // Set whether to record request queue events. |
57 void HandleSetRecordRequestQueue(const base::ListValue* args); | 60 void HandleSetRecordRequestQueue(const base::ListValue* args); |
58 | 61 |
59 // Load both Page Model and Request Queue event logs. | 62 // Load both Page Model and Request Queue event logs. |
60 void HandleGetEventLogs(const base::ListValue* args); | 63 void HandleGetEventLogs(const base::ListValue* args); |
61 | 64 |
62 // Load whether logs are being recorded. | 65 // Load whether logs are being recorded. |
63 void HandleGetLoggingState(const base::ListValue* args); | 66 void HandleGetLoggingState(const base::ListValue* args); |
64 | 67 |
| 68 // Adds a url to the background loader queue. |
| 69 void HandleAddToRequestQueue(const base::ListValue* args); |
| 70 |
| 71 // Load whether device is currently offline. |
| 72 void HandleGetNetworkStatus(const base::ListValue* args); |
| 73 |
65 // Callback for async GetAllPages calls. | 74 // Callback for async GetAllPages calls. |
66 void HandleStoredPagesCallback( | 75 void HandleStoredPagesCallback( |
67 std::string callback_id, | 76 std::string callback_id, |
68 const offline_pages::MultipleOfflinePageItemResult& pages); | 77 const offline_pages::MultipleOfflinePageItemResult& pages); |
69 | 78 |
70 // Callback for async GetRequests calls. | 79 // Callback for async GetRequests calls. |
71 void HandleRequestQueueCallback( | 80 void HandleRequestQueueCallback( |
72 std::string callback_id, | 81 std::string callback_id, |
73 offline_pages::RequestQueue::GetRequestsResult result, | 82 offline_pages::RequestQueue::GetRequestsResult result, |
74 const std::vector<offline_pages::SavePageRequest>& requests); | 83 const std::vector<offline_pages::SavePageRequest>& requests); |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 const offline_pages::MultipleOfflinePageItemResult& pages) { | 199 const offline_pages::MultipleOfflinePageItemResult& pages) { |
191 base::ListValue results; | 200 base::ListValue results; |
192 | 201 |
193 for (const auto& page : pages) { | 202 for (const auto& page : pages) { |
194 base::DictionaryValue* offline_page = new base::DictionaryValue(); | 203 base::DictionaryValue* offline_page = new base::DictionaryValue(); |
195 results.Append(offline_page); | 204 results.Append(offline_page); |
196 offline_page->SetString("onlineUrl", page.url.spec()); | 205 offline_page->SetString("onlineUrl", page.url.spec()); |
197 offline_page->SetString("namespace", page.client_id.name_space); | 206 offline_page->SetString("namespace", page.client_id.name_space); |
198 offline_page->SetDouble("size", page.file_size); | 207 offline_page->SetDouble("size", page.file_size); |
199 offline_page->SetString("id", std::to_string(page.offline_id)); | 208 offline_page->SetString("id", std::to_string(page.offline_id)); |
200 offline_page->SetString("filePath", page.file_path.value()); | 209 offline_page->SetString("filePath", page.GetOfflineURL().spec()); |
201 offline_page->SetDouble("creationTime", page.creation_time.ToJsTime()); | 210 offline_page->SetDouble("creationTime", page.creation_time.ToJsTime()); |
202 offline_page->SetDouble("lastAccessTime", | 211 offline_page->SetDouble("lastAccessTime", |
203 page.last_access_time.ToJsTime()); | 212 page.last_access_time.ToJsTime()); |
204 offline_page->SetInteger("accessCount", page.access_count); | 213 offline_page->SetInteger("accessCount", page.access_count); |
205 offline_page->SetString("isExpired", page.IsExpired() ? "Yes" : "No"); | 214 offline_page->SetString("isExpired", page.IsExpired() ? "Yes" : "No"); |
206 } | 215 } |
207 ResolveJavascriptCallback(base::StringValue(callback_id), results); | 216 ResolveJavascriptCallback(base::StringValue(callback_id), results); |
208 } | 217 } |
209 | 218 |
210 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback( | 219 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
263 } | 272 } |
264 } | 273 } |
265 | 274 |
266 void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel( | 275 void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel( |
267 const base::ListValue* args) { | 276 const base::ListValue* args) { |
268 bool should_record; | 277 bool should_record; |
269 CHECK(args->GetBoolean(0, &should_record)); | 278 CHECK(args->GetBoolean(0, &should_record)); |
270 offline_page_model_->GetLogger()->SetIsLogging(should_record); | 279 offline_page_model_->GetLogger()->SetIsLogging(should_record); |
271 } | 280 } |
272 | 281 |
| 282 void OfflineInternalsUIMessageHandler::HandleGetNetworkStatus( |
| 283 const base::ListValue* args) { |
| 284 const base::Value* callback_id; |
| 285 CHECK(args->Get(0, &callback_id)); |
| 286 |
| 287 ResolveJavascriptCallback( |
| 288 *callback_id, |
| 289 base::StringValue( |
| 290 net::NetworkChangeNotifier::IsOffline() ? "Offline" : "Online")); |
| 291 } |
| 292 |
273 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( | 293 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( |
274 const base::ListValue* args) { | 294 const base::ListValue* args) { |
275 bool should_record; | 295 bool should_record; |
276 CHECK(args->GetBoolean(0, &should_record)); | 296 CHECK(args->GetBoolean(0, &should_record)); |
277 request_coordinator_->GetLogger()->SetIsLogging(should_record); | 297 request_coordinator_->GetLogger()->SetIsLogging(should_record); |
278 } | 298 } |
279 | 299 |
280 void OfflineInternalsUIMessageHandler::HandleGetLoggingState( | 300 void OfflineInternalsUIMessageHandler::HandleGetLoggingState( |
281 const base::ListValue* args) { | 301 const base::ListValue* args) { |
282 AllowJavascript(); | 302 AllowJavascript(); |
(...skipping 18 matching lines...) Expand all Loading... |
301 offline_page_model_->GetLogger()->GetLogs(&logs); | 321 offline_page_model_->GetLogger()->GetLogs(&logs); |
302 request_coordinator_->GetLogger()->GetLogs(&logs); | 322 request_coordinator_->GetLogger()->GetLogs(&logs); |
303 std::sort(logs.begin(), logs.end()); | 323 std::sort(logs.begin(), logs.end()); |
304 | 324 |
305 base::ListValue result; | 325 base::ListValue result; |
306 result.AppendStrings(logs); | 326 result.AppendStrings(logs); |
307 | 327 |
308 ResolveJavascriptCallback(*callback_id, result); | 328 ResolveJavascriptCallback(*callback_id, result); |
309 } | 329 } |
310 | 330 |
| 331 void OfflineInternalsUIMessageHandler::HandleAddToRequestQueue( |
| 332 const base::ListValue* args) { |
| 333 const base::Value* callback_id; |
| 334 CHECK(args->Get(0, &callback_id)); |
| 335 |
| 336 std::string url; |
| 337 CHECK(args->GetString(1, &url)); |
| 338 |
| 339 std::ostringstream id_stream; |
| 340 id_stream << std::rand(); |
| 341 |
| 342 ResolveJavascriptCallback( |
| 343 *callback_id, |
| 344 base::FundamentalValue( |
| 345 request_coordinator_->SavePageLater( |
| 346 GURL(url), |
| 347 offline_pages::ClientId(offline_pages::kAsyncNamespace, |
| 348 id_stream.str()), |
| 349 true))); |
| 350 } |
| 351 |
311 void OfflineInternalsUIMessageHandler::RegisterMessages() { | 352 void OfflineInternalsUIMessageHandler::RegisterMessages() { |
312 web_ui()->RegisterMessageCallback( | 353 web_ui()->RegisterMessageCallback( |
313 "deleteAllPages", | 354 "deleteAllPages", |
314 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, | 355 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, |
315 weak_ptr_factory_.GetWeakPtr())); | 356 weak_ptr_factory_.GetWeakPtr())); |
316 web_ui()->RegisterMessageCallback( | 357 web_ui()->RegisterMessageCallback( |
317 "deleteSelectedPages", | 358 "deleteSelectedPages", |
318 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, | 359 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, |
319 weak_ptr_factory_.GetWeakPtr())); | 360 weak_ptr_factory_.GetWeakPtr())); |
320 web_ui()->RegisterMessageCallback( | 361 web_ui()->RegisterMessageCallback( |
(...skipping 13 matching lines...) Expand all Loading... |
334 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue, | 375 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue, |
335 weak_ptr_factory_.GetWeakPtr())); | 376 weak_ptr_factory_.GetWeakPtr())); |
336 web_ui()->RegisterMessageCallback( | 377 web_ui()->RegisterMessageCallback( |
337 "setRecordPageModel", | 378 "setRecordPageModel", |
338 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordPageModel, | 379 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordPageModel, |
339 weak_ptr_factory_.GetWeakPtr())); | 380 weak_ptr_factory_.GetWeakPtr())); |
340 web_ui()->RegisterMessageCallback( | 381 web_ui()->RegisterMessageCallback( |
341 "getLoggingState", | 382 "getLoggingState", |
342 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState, | 383 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState, |
343 weak_ptr_factory_.GetWeakPtr())); | 384 weak_ptr_factory_.GetWeakPtr())); |
| 385 web_ui()->RegisterMessageCallback( |
| 386 "addToRequestQueue", |
| 387 base::Bind(&OfflineInternalsUIMessageHandler::HandleAddToRequestQueue, |
| 388 weak_ptr_factory_.GetWeakPtr())); |
| 389 web_ui()->RegisterMessageCallback( |
| 390 "getNetworkStatus", |
| 391 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus, |
| 392 weak_ptr_factory_.GetWeakPtr())); |
344 | 393 |
345 // Get the offline page model associated with this web ui. | 394 // Get the offline page model associated with this web ui. |
346 Profile* profile = Profile::FromWebUI(web_ui()); | 395 Profile* profile = Profile::FromWebUI(web_ui()); |
347 offline_page_model_ = | 396 offline_page_model_ = |
348 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | 397 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); |
349 request_coordinator_ = | 398 request_coordinator_ = |
350 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | 399 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); |
351 } | 400 } |
352 | 401 |
353 } // namespace | 402 } // namespace |
(...skipping 13 matching lines...) Expand all Loading... |
367 html_source->AddResourcePath("offline_internals_browser_proxy.js", | 416 html_source->AddResourcePath("offline_internals_browser_proxy.js", |
368 IDR_OFFLINE_INTERNALS_BROWSER_PROXY_JS); | 417 IDR_OFFLINE_INTERNALS_BROWSER_PROXY_JS); |
369 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); | 418 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); |
370 | 419 |
371 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 420 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
372 | 421 |
373 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); | 422 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); |
374 } | 423 } |
375 | 424 |
376 OfflineInternalsUI::~OfflineInternalsUI() {} | 425 OfflineInternalsUI::~OfflineInternalsUI() {} |
OLD | NEW |