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