Chromium Code Reviews| 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 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/guid.h" | 13 #include "base/guid.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/string_split.h" | |
| 17 #include "base/strings/string_util.h" | |
| 16 #include "base/values.h" | 18 #include "base/values.h" |
| 17 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | 19 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| 18 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task .h" | 20 #include "chrome/browser/android/offline_pages/prefetch/prefetch_background_task .h" |
| 19 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | 21 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 22 #include "chrome/browser/profiles/profile.h" |
| 23 #include "chrome/common/channel_info.h" | |
| 24 #include "chrome/common/chrome_content_client.h" | |
| 21 #include "components/offline_pages/core/client_namespace_constants.h" | 25 #include "components/offline_pages/core/client_namespace_constants.h" |
| 22 #include "content/public/browser/web_ui.h" | 26 #include "components/offline_pages/core/prefetch/generate_page_bundle_request.h" |
| 27 #include "components/offline_pages/core/prefetch/get_operation_request.h" | |
| 23 #include "net/base/network_change_notifier.h" | 28 #include "net/base/network_change_notifier.h" |
| 24 | 29 |
| 25 namespace offline_internals { | 30 namespace offline_internals { |
| 26 | 31 |
| 27 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() | 32 namespace { |
| 28 : offline_page_model_(nullptr), | |
| 29 request_coordinator_(nullptr), | |
| 30 weak_ptr_factory_(this) {} | |
| 31 | 33 |
| 32 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} | 34 std::string GetStringFromDeletePageResult( |
| 33 | |
| 34 std::string OfflineInternalsUIMessageHandler::GetStringFromDeletePageResult( | |
| 35 offline_pages::DeletePageResult value) { | 35 offline_pages::DeletePageResult value) { |
| 36 switch (value) { | 36 switch (value) { |
| 37 case offline_pages::DeletePageResult::SUCCESS: | 37 case offline_pages::DeletePageResult::SUCCESS: |
| 38 return "Success"; | 38 return "Success"; |
| 39 case offline_pages::DeletePageResult::CANCELLED: | 39 case offline_pages::DeletePageResult::CANCELLED: |
| 40 return "Cancelled"; | 40 return "Cancelled"; |
| 41 case offline_pages::DeletePageResult::STORE_FAILURE: | 41 case offline_pages::DeletePageResult::STORE_FAILURE: |
| 42 return "Store failure"; | 42 return "Store failure"; |
| 43 case offline_pages::DeletePageResult::DEVICE_FAILURE: | 43 case offline_pages::DeletePageResult::DEVICE_FAILURE: |
| 44 return "Device failure"; | 44 return "Device failure"; |
| 45 case offline_pages::DeletePageResult::NOT_FOUND: | 45 case offline_pages::DeletePageResult::NOT_FOUND: |
| 46 return "Not found"; | 46 return "Not found"; |
| 47 case offline_pages::DeletePageResult::RESULT_COUNT: | 47 case offline_pages::DeletePageResult::RESULT_COUNT: |
| 48 break; | 48 break; |
| 49 } | 49 } |
| 50 NOTREACHED(); | 50 NOTREACHED(); |
| 51 return "Unknown"; | 51 return "Unknown"; |
| 52 } | 52 } |
| 53 | 53 |
| 54 std::string OfflineInternalsUIMessageHandler::GetStringFromDeleteRequestResults( | 54 std::string GetStringFromDeleteRequestResults( |
| 55 const offline_pages::MultipleItemStatuses& results) { | 55 const offline_pages::MultipleItemStatuses& results) { |
| 56 // If any requests failed, return "failure", else "success". | 56 // If any requests failed, return "failure", else "success". |
| 57 for (const auto& result : results) { | 57 for (const auto& result : results) { |
| 58 if (result.second == offline_pages::ItemActionStatus::STORE_ERROR) | 58 if (result.second == offline_pages::ItemActionStatus::STORE_ERROR) |
| 59 return "Store failure, could not delete one or more requests"; | 59 return "Store failure, could not delete one or more requests"; |
| 60 } | 60 } |
| 61 | 61 |
| 62 return "Success"; | 62 return "Success"; |
| 63 } | 63 } |
| 64 | 64 |
| 65 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { | 65 std::string GetStringFromSavePageStatus() { |
| 66 return "Available"; | 66 return "Available"; |
| 67 } | 67 } |
| 68 | 68 |
| 69 std::string GetStringFromPrefetchRequestStatus( | |
| 70 offline_pages::PrefetchRequestStatus status) { | |
| 71 switch (status) { | |
| 72 case offline_pages::PrefetchRequestStatus::SUCCESS: | |
| 73 return "Success"; | |
| 74 case offline_pages::PrefetchRequestStatus::SHOULD_RETRY_WITHOUT_BACKOFF: | |
| 75 return "Retry w/out backoff"; | |
| 76 case offline_pages::PrefetchRequestStatus::SHOULD_RETRY_WITH_BACKOFF: | |
| 77 return "Retry w/ backoff"; | |
| 78 case offline_pages::PrefetchRequestStatus::SHOULD_SUSPEND: | |
| 79 return "Suspend"; | |
| 80 default: | |
| 81 NOTREACHED(); | |
| 82 return "Unknown"; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 std::string GetStringRenderPageInfoList( | |
|
dewittj
2017/06/12 18:31:04
Is it possible to just use text_format.h?
https:/
jianli
2017/06/12 22:40:30
We can't and don't want to use it because:
* text_
| |
| 87 const std::vector<offline_pages::RenderPageInfo>& pages) { | |
| 88 std::string str("["); | |
| 89 bool first = true; | |
| 90 for (const auto& page : pages) { | |
| 91 if (first) | |
| 92 first = false; | |
| 93 else | |
| 94 str += " ,"; | |
| 95 str += "{url:\""; | |
| 96 str += page.url; | |
| 97 str += "\", status:"; | |
| 98 str += base::IntToString(static_cast<int>(page.status)); | |
| 99 str += "}"; | |
| 100 } | |
| 101 str += "]"; | |
| 102 return str; | |
| 103 } | |
| 104 | |
| 105 } // namespace | |
| 106 | |
| 107 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() | |
| 108 : offline_page_model_(nullptr), | |
| 109 request_coordinator_(nullptr), | |
| 110 weak_ptr_factory_(this) {} | |
| 111 | |
| 112 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} | |
| 113 | |
| 69 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( | 114 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( |
| 70 const base::ListValue* args) { | 115 const base::ListValue* args) { |
| 71 std::string callback_id; | 116 std::string callback_id; |
| 72 CHECK(args->GetString(0, &callback_id)); | 117 CHECK(args->GetString(0, &callback_id)); |
| 73 | 118 |
| 74 std::vector<int64_t> offline_ids; | 119 std::vector<int64_t> offline_ids; |
| 75 const base::ListValue* offline_ids_from_arg; | 120 const base::ListValue* offline_ids_from_arg; |
| 76 args->GetList(1, &offline_ids_from_arg); | 121 args->GetList(1, &offline_ids_from_arg); |
| 77 | 122 |
| 78 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { | 123 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... | |
| 245 const base::ListValue* args) { | 290 const base::ListValue* args) { |
| 246 AllowJavascript(); | 291 AllowJavascript(); |
| 247 const base::Value* callback_id; | 292 const base::Value* callback_id; |
| 248 CHECK(args->Get(0, &callback_id)); | 293 CHECK(args->Get(0, &callback_id)); |
| 249 | 294 |
| 250 offline_pages::PrefetchBackgroundTask::Cancel(); | 295 offline_pages::PrefetchBackgroundTask::Cancel(); |
| 251 | 296 |
| 252 ResolveJavascriptCallback(*callback_id, base::Value("Cancelled.")); | 297 ResolveJavascriptCallback(*callback_id, base::Value("Cancelled.")); |
| 253 } | 298 } |
| 254 | 299 |
| 300 void OfflineInternalsUIMessageHandler::HandleGeneratePageBundle( | |
| 301 const base::ListValue* args) { | |
| 302 AllowJavascript(); | |
| 303 std::string callback_id; | |
| 304 CHECK(args->GetString(0, &callback_id)); | |
| 305 | |
| 306 std::string data; | |
| 307 CHECK(args->GetString(1, &data)); | |
| 308 std::vector<std::string> page_urls = base::SplitStringUsingSubstr( | |
| 309 data, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); | |
| 310 | |
| 311 generate_page_bundle_request_.reset( | |
| 312 new offline_pages::GeneratePageBundleRequest( | |
| 313 GetUserAgent(), "GCM ID", 1000000, page_urls, chrome::GetChannel(), | |
| 314 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 315 base::Bind( | |
| 316 &OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback, | |
| 317 weak_ptr_factory_.GetWeakPtr(), callback_id))); | |
| 318 } | |
| 319 | |
| 320 void OfflineInternalsUIMessageHandler::HandleGetOperation( | |
| 321 const base::ListValue* args) { | |
| 322 AllowJavascript(); | |
| 323 std::string callback_id; | |
| 324 CHECK(args->GetString(0, &callback_id)); | |
| 325 | |
| 326 std::string name; | |
| 327 CHECK(args->GetString(1, &name)); | |
| 328 base::TrimWhitespaceASCII(name, base::TRIM_ALL, &name); | |
| 329 | |
| 330 get_operation_request_.reset(new offline_pages::GetOperationRequest( | |
| 331 name, chrome::GetChannel(), | |
| 332 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 333 base::Bind( | |
| 334 &OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback, | |
| 335 weak_ptr_factory_.GetWeakPtr(), callback_id))); | |
| 336 } | |
| 337 | |
| 338 void OfflineInternalsUIMessageHandler::HandlePrefetchRequestCallback( | |
| 339 std::string callback_id, | |
| 340 offline_pages::PrefetchRequestStatus status, | |
| 341 const std::string& operation_name, | |
| 342 const std::vector<offline_pages::RenderPageInfo>& pages) { | |
| 343 ResolveJavascriptCallback( | |
| 344 base::Value(callback_id), | |
| 345 base::Value(GetStringFromPrefetchRequestStatus(status) + " " + | |
| 346 operation_name + " " + GetStringRenderPageInfoList(pages))); | |
| 347 } | |
| 348 | |
| 255 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( | 349 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( |
| 256 const base::ListValue* args) { | 350 const base::ListValue* args) { |
| 257 AllowJavascript(); | 351 AllowJavascript(); |
| 258 bool should_record; | 352 bool should_record; |
| 259 CHECK(args->GetBoolean(0, &should_record)); | 353 CHECK(args->GetBoolean(0, &should_record)); |
| 260 if (request_coordinator_) | 354 if (request_coordinator_) |
| 261 request_coordinator_->GetLogger()->SetIsLogging(should_record); | 355 request_coordinator_->GetLogger()->SetIsLogging(should_record); |
| 262 } | 356 } |
| 263 | 357 |
| 264 void OfflineInternalsUIMessageHandler::HandleGetLoggingState( | 358 void OfflineInternalsUIMessageHandler::HandleGetLoggingState( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 367 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus, | 461 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus, |
| 368 weak_ptr_factory_.GetWeakPtr())); | 462 weak_ptr_factory_.GetWeakPtr())); |
| 369 web_ui()->RegisterMessageCallback( | 463 web_ui()->RegisterMessageCallback( |
| 370 "scheduleNwake", | 464 "scheduleNwake", |
| 371 base::Bind(&OfflineInternalsUIMessageHandler::HandleScheduleNwake, | 465 base::Bind(&OfflineInternalsUIMessageHandler::HandleScheduleNwake, |
| 372 weak_ptr_factory_.GetWeakPtr())); | 466 weak_ptr_factory_.GetWeakPtr())); |
| 373 web_ui()->RegisterMessageCallback( | 467 web_ui()->RegisterMessageCallback( |
| 374 "cancelNwake", | 468 "cancelNwake", |
| 375 base::Bind(&OfflineInternalsUIMessageHandler::HandleCancelNwake, | 469 base::Bind(&OfflineInternalsUIMessageHandler::HandleCancelNwake, |
| 376 weak_ptr_factory_.GetWeakPtr())); | 470 weak_ptr_factory_.GetWeakPtr())); |
| 471 web_ui()->RegisterMessageCallback( | |
| 472 "generatePageBundle", | |
| 473 base::Bind(&OfflineInternalsUIMessageHandler::HandleGeneratePageBundle, | |
| 474 weak_ptr_factory_.GetWeakPtr())); | |
| 475 web_ui()->RegisterMessageCallback( | |
| 476 "getOperation", | |
| 477 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetOperation, | |
| 478 weak_ptr_factory_.GetWeakPtr())); | |
| 377 | 479 |
| 378 // Get the offline page model associated with this web ui. | 480 // Get the offline page model associated with this web ui. |
| 379 Profile* profile = Profile::FromWebUI(web_ui()); | 481 Profile* profile = Profile::FromWebUI(web_ui()); |
| 380 offline_page_model_ = | 482 offline_page_model_ = |
| 381 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | 483 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); |
| 382 request_coordinator_ = | 484 request_coordinator_ = |
| 383 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | 485 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); |
| 384 } | 486 } |
| 385 | 487 |
| 386 } // namespace offline_internals | 488 } // namespace offline_internals |
| OLD | NEW |