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/offline_internals_ui_message_handler.h
" | 5 #include "chrome/browser/ui/webui/offline/offline_internals_ui_message_handler.h
" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/guid.h" | 14 #include "base/guid.h" |
15 #include "base/memory/ptr_util.h" | 15 #include "base/memory/ptr_util.h" |
16 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 17 #include "base/strings/string_split.h" |
| 18 #include "base/strings/string_util.h" |
| 19 #include "base/strings/stringprintf.h" |
17 #include "base/values.h" | 20 #include "base/values.h" |
18 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 21 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
19 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task
.h" | 22 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task
.h" |
20 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 23 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
21 #include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h" | 24 #include "chrome/browser/offline_pages/prefetch/prefetch_service_factory.h" |
22 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/common/channel_info.h" |
| 27 #include "chrome/common/chrome_content_client.h" |
23 #include "components/offline_pages/core/client_namespace_constants.h" | 28 #include "components/offline_pages/core/client_namespace_constants.h" |
24 #include "components/offline_pages/core/offline_page_feature.h" | 29 #include "components/offline_pages/core/offline_page_feature.h" |
| 30 #include "components/offline_pages/core/prefetch/generate_page_bundle_request.h" |
| 31 #include "components/offline_pages/core/prefetch/get_operation_request.h" |
25 #include "components/offline_pages/core/prefetch/prefetch_service.h" | 32 #include "components/offline_pages/core/prefetch/prefetch_service.h" |
26 #include "content/public/browser/web_ui.h" | 33 #include "content/public/browser/web_ui.h" |
27 #include "net/base/network_change_notifier.h" | 34 #include "net/base/network_change_notifier.h" |
28 | 35 |
29 namespace offline_internals { | 36 namespace offline_internals { |
30 | 37 |
31 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() | 38 namespace { |
32 : offline_page_model_(nullptr), | |
33 request_coordinator_(nullptr), | |
34 prefetch_service_(nullptr), | |
35 weak_ptr_factory_(this) {} | |
36 | 39 |
37 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} | 40 std::string GetStringFromDeletePageResult( |
38 | |
39 std::string OfflineInternalsUIMessageHandler::GetStringFromDeletePageResult( | |
40 offline_pages::DeletePageResult value) { | 41 offline_pages::DeletePageResult value) { |
41 switch (value) { | 42 switch (value) { |
42 case offline_pages::DeletePageResult::SUCCESS: | 43 case offline_pages::DeletePageResult::SUCCESS: |
43 return "Success"; | 44 return "Success"; |
44 case offline_pages::DeletePageResult::CANCELLED: | 45 case offline_pages::DeletePageResult::CANCELLED: |
45 return "Cancelled"; | 46 return "Cancelled"; |
46 case offline_pages::DeletePageResult::STORE_FAILURE: | 47 case offline_pages::DeletePageResult::STORE_FAILURE: |
47 return "Store failure"; | 48 return "Store failure"; |
48 case offline_pages::DeletePageResult::DEVICE_FAILURE: | 49 case offline_pages::DeletePageResult::DEVICE_FAILURE: |
49 return "Device failure"; | 50 return "Device failure"; |
50 case offline_pages::DeletePageResult::NOT_FOUND: | 51 case offline_pages::DeletePageResult::NOT_FOUND: |
51 return "Not found"; | 52 return "Not found"; |
52 case offline_pages::DeletePageResult::RESULT_COUNT: | 53 case offline_pages::DeletePageResult::RESULT_COUNT: |
53 break; | 54 break; |
54 } | 55 } |
55 NOTREACHED(); | 56 NOTREACHED(); |
56 return "Unknown"; | 57 return "Unknown"; |
57 } | 58 } |
58 | 59 |
59 std::string OfflineInternalsUIMessageHandler::GetStringFromDeleteRequestResults( | 60 std::string GetStringFromDeleteRequestResults( |
60 const offline_pages::MultipleItemStatuses& results) { | 61 const offline_pages::MultipleItemStatuses& results) { |
61 // If any requests failed, return "failure", else "success". | 62 // If any requests failed, return "failure", else "success". |
62 for (const auto& result : results) { | 63 for (const auto& result : results) { |
63 if (result.second == offline_pages::ItemActionStatus::STORE_ERROR) | 64 if (result.second == offline_pages::ItemActionStatus::STORE_ERROR) |
64 return "Store failure, could not delete one or more requests"; | 65 return "Store failure, could not delete one or more requests"; |
65 } | 66 } |
66 | 67 |
67 return "Success"; | 68 return "Success"; |
68 } | 69 } |
69 | 70 |
70 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { | 71 std::string GetStringFromSavePageStatus() { |
71 return "Available"; | 72 return "Available"; |
72 } | 73 } |
73 | 74 |
| 75 std::string GetStringFromPrefetchRequestStatus( |
| 76 offline_pages::PrefetchRequestStatus status) { |
| 77 switch (status) { |
| 78 case offline_pages::PrefetchRequestStatus::SUCCESS: |
| 79 return "Success"; |
| 80 case offline_pages::PrefetchRequestStatus::SHOULD_RETRY_WITHOUT_BACKOFF: |
| 81 return "Retry w/out backoff"; |
| 82 case offline_pages::PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF: |
| 83 return "Retry w/ backoff"; |
| 84 case offline_pages::PrefetchRequestStatus::SHOULD_SUSPEND: |
| 85 return "Suspend"; |
| 86 default: |
| 87 NOTREACHED(); |
| 88 return "Unknown"; |
| 89 } |
| 90 } |
| 91 |
| 92 std::string GetStringRenderPageInfoList( |
| 93 const std::vector<offline_pages::RenderPageInfo>& pages) { |
| 94 std::string str("[\n"); |
| 95 bool first = true; |
| 96 for (const auto& page : pages) { |
| 97 if (first) |
| 98 first = false; |
| 99 else |
| 100 str += ",\n"; |
| 101 str += base::StringPrintf( |
| 102 " {\n" |
| 103 " url: \"%s\",\n" |
| 104 " redirect_url: \"%s\",\n" |
| 105 " status: %d,\n" |
| 106 " body_name: \"%s\",\n" |
| 107 " body_length: %lld\n" |
| 108 " }", |
| 109 page.url.c_str(), page.redirect_url.c_str(), |
| 110 static_cast<int>(page.status), page.body_name.c_str(), |
| 111 static_cast<long long>(page.body_length)); |
| 112 } |
| 113 str += "\n]"; |
| 114 return str; |
| 115 } |
| 116 |
| 117 } // namespace |
| 118 |
| 119 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() |
| 120 : offline_page_model_(nullptr), |
| 121 request_coordinator_(nullptr), |
| 122 prefetch_service_(nullptr), |
| 123 weak_ptr_factory_(this) {} |
| 124 |
| 125 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} |
| 126 |
74 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( | 127 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( |
75 const base::ListValue* args) { | 128 const base::ListValue* args) { |
76 std::string callback_id; | 129 std::string callback_id; |
77 CHECK(args->GetString(0, &callback_id)); | 130 CHECK(args->GetString(0, &callback_id)); |
78 | 131 |
79 std::vector<int64_t> offline_ids; | 132 std::vector<int64_t> offline_ids; |
80 const base::ListValue* offline_ids_from_arg; | 133 const base::ListValue* offline_ids_from_arg; |
81 args->GetList(1, &offline_ids_from_arg); | 134 args->GetList(1, &offline_ids_from_arg); |
82 | 135 |
83 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { | 136 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 const base::ListValue* args) { | 303 const base::ListValue* args) { |
251 AllowJavascript(); | 304 AllowJavascript(); |
252 const base::Value* callback_id; | 305 const base::Value* callback_id; |
253 CHECK(args->Get(0, &callback_id)); | 306 CHECK(args->Get(0, &callback_id)); |
254 | 307 |
255 offline_pages::PrefetchBackgroundTask::Cancel(); | 308 offline_pages::PrefetchBackgroundTask::Cancel(); |
256 | 309 |
257 ResolveJavascriptCallback(*callback_id, base::Value("Cancelled.")); | 310 ResolveJavascriptCallback(*callback_id, base::Value("Cancelled.")); |
258 } | 311 } |
259 | 312 |
| 313 void OfflineInternalsUIMessageHandler::HandleGeneratePageBundle( |
| 314 const base::ListValue* args) { |
| 315 AllowJavascript(); |
| 316 std::string callback_id; |
| 317 CHECK(args->GetString(0, &callback_id)); |
| 318 |
| 319 std::string data; |
| 320 CHECK(args->GetString(1, &data)); |
| 321 std::vector<std::string> page_urls = base::SplitStringUsingSubstr( |
| 322 data, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); |
| 323 |
| 324 generate_page_bundle_request_.reset( |
| 325 new offline_pages::GeneratePageBundleRequest( |
| 326 GetUserAgent(), "GCM ID", 1000000, page_urls, chrome::GetChannel(), |
| 327 Profile::FromWebUI(web_ui())->GetRequestContext(), |
| 328 base::Bind( |
| 329 &OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback, |
| 330 weak_ptr_factory_.GetWeakPtr(), callback_id))); |
| 331 } |
| 332 |
| 333 void OfflineInternalsUIMessageHandler::HandleGetOperation( |
| 334 const base::ListValue* args) { |
| 335 AllowJavascript(); |
| 336 std::string callback_id; |
| 337 CHECK(args->GetString(0, &callback_id)); |
| 338 |
| 339 std::string name; |
| 340 CHECK(args->GetString(1, &name)); |
| 341 base::TrimWhitespaceASCII(name, base::TRIM_ALL, &name); |
| 342 |
| 343 get_operation_request_.reset(new offline_pages::GetOperationRequest( |
| 344 name, chrome::GetChannel(), |
| 345 Profile::FromWebUI(web_ui())->GetRequestContext(), |
| 346 base::Bind( |
| 347 &OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback, |
| 348 weak_ptr_factory_.GetWeakPtr(), callback_id))); |
| 349 } |
| 350 |
| 351 void OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback( |
| 352 std::string callback_id, |
| 353 offline_pages::PrefetchRequestStatus status, |
| 354 const std::string& operation_name, |
| 355 const std::vector<offline_pages::RenderPageInfo>& pages) { |
| 356 ResolveJavascriptCallback( |
| 357 base::Value(callback_id), |
| 358 base::Value(GetStringFromPrefetchRequestStatus(status) + "\n" + |
| 359 operation_name + "\n" + GetStringRenderPageInfoList(pages))); |
| 360 } |
| 361 |
260 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( | 362 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( |
261 const base::ListValue* args) { | 363 const base::ListValue* args) { |
262 AllowJavascript(); | 364 AllowJavascript(); |
263 bool should_record; | 365 bool should_record; |
264 CHECK(args->GetBoolean(0, &should_record)); | 366 CHECK(args->GetBoolean(0, &should_record)); |
265 if (request_coordinator_) | 367 if (request_coordinator_) |
266 request_coordinator_->GetLogger()->SetIsLogging(should_record); | 368 request_coordinator_->GetLogger()->SetIsLogging(should_record); |
267 } | 369 } |
268 | 370 |
269 void OfflineInternalsUIMessageHandler::HandleSetRecordPrefetchService( | 371 void OfflineInternalsUIMessageHandler::HandleSetRecordPrefetchService( |
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus, | 495 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus, |
394 weak_ptr_factory_.GetWeakPtr())); | 496 weak_ptr_factory_.GetWeakPtr())); |
395 web_ui()->RegisterMessageCallback( | 497 web_ui()->RegisterMessageCallback( |
396 "scheduleNwake", | 498 "scheduleNwake", |
397 base::Bind(&OfflineInternalsUIMessageHandler::HandleScheduleNwake, | 499 base::Bind(&OfflineInternalsUIMessageHandler::HandleScheduleNwake, |
398 weak_ptr_factory_.GetWeakPtr())); | 500 weak_ptr_factory_.GetWeakPtr())); |
399 web_ui()->RegisterMessageCallback( | 501 web_ui()->RegisterMessageCallback( |
400 "cancelNwake", | 502 "cancelNwake", |
401 base::Bind(&OfflineInternalsUIMessageHandler::HandleCancelNwake, | 503 base::Bind(&OfflineInternalsUIMessageHandler::HandleCancelNwake, |
402 weak_ptr_factory_.GetWeakPtr())); | 504 weak_ptr_factory_.GetWeakPtr())); |
| 505 web_ui()->RegisterMessageCallback( |
| 506 "generatePageBundle", |
| 507 base::Bind(&OfflineInternalsUIMessageHandler::HandleGeneratePageBundle, |
| 508 weak_ptr_factory_.GetWeakPtr())); |
| 509 web_ui()->RegisterMessageCallback( |
| 510 "getOperation", |
| 511 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetOperation, |
| 512 weak_ptr_factory_.GetWeakPtr())); |
403 | 513 |
404 // Get the offline page model associated with this web ui. | 514 // Get the offline page model associated with this web ui. |
405 Profile* profile = Profile::FromWebUI(web_ui()); | 515 Profile* profile = Profile::FromWebUI(web_ui()); |
406 offline_page_model_ = | 516 offline_page_model_ = |
407 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | 517 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); |
408 request_coordinator_ = | 518 request_coordinator_ = |
409 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | 519 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); |
410 prefetch_service_ = | 520 prefetch_service_ = |
411 offline_pages::PrefetchServiceFactory::GetForBrowserContext(profile); | 521 offline_pages::PrefetchServiceFactory::GetForBrowserContext(profile); |
412 } | 522 } |
413 | 523 |
414 } // namespace offline_internals | 524 } // namespace offline_internals |
OLD | NEW |