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 <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 return "Device failure"; | 40 return "Device failure"; |
| 41 case offline_pages::DeletePageResult::NOT_FOUND: | 41 case offline_pages::DeletePageResult::NOT_FOUND: |
| 42 return "Not found"; | 42 return "Not found"; |
| 43 case offline_pages::DeletePageResult::RESULT_COUNT: | 43 case offline_pages::DeletePageResult::RESULT_COUNT: |
| 44 break; | 44 break; |
| 45 } | 45 } |
| 46 NOTREACHED(); | 46 NOTREACHED(); |
| 47 return "Unknown"; | 47 return "Unknown"; |
| 48 } | 48 } |
| 49 | 49 |
| 50 std::string OfflineInternalsUIMessageHandler::GetStringFromDeleteRequestResults( | |
| 51 const offline_pages::RequestQueue::UpdateMultipleRequestResults& results) { | |
| 52 // If any requests failed, return "failure", else "success". | |
| 53 bool failed = false; | |
| 54 for (const auto& result : results) { | |
| 55 if (result.second == | |
| 56 offline_pages::RequestQueue::UpdateRequestResult::STORE_FAILURE) | |
| 57 failed = true; | |
| 58 } | |
| 59 if (failed) | |
| 60 return "Store failure"; | |
|
chili
2016/09/12 20:26:02
should we have slightly better messaging for which
Pete Williamson
2016/09/12 22:46:36
We don't have the URL, but we do have the request
| |
| 61 return "Success"; | |
| 62 } | |
| 63 | |
| 50 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { | 64 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { |
| 51 return "Available"; | 65 return "Available"; |
| 52 } | 66 } |
| 53 | 67 |
| 54 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( | 68 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( |
| 55 const base::ListValue* args) { | 69 const base::ListValue* args) { |
| 56 std::string callback_id; | 70 std::string callback_id; |
| 57 CHECK(args->GetString(0, &callback_id)); | 71 CHECK(args->GetString(0, &callback_id)); |
| 58 | 72 |
| 59 // Pass back success because ClearAll doesn't return a status. | 73 // Pass back success because ClearAll doesn't return a status. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 79 base::StringToInt64(value, &int_value); | 93 base::StringToInt64(value, &int_value); |
| 80 offline_ids.push_back(int_value); | 94 offline_ids.push_back(int_value); |
| 81 } | 95 } |
| 82 | 96 |
| 83 offline_page_model_->DeletePagesByOfflineId( | 97 offline_page_model_->DeletePagesByOfflineId( |
| 84 offline_ids, | 98 offline_ids, |
| 85 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, | 99 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, |
| 86 weak_ptr_factory_.GetWeakPtr(), callback_id)); | 100 weak_ptr_factory_.GetWeakPtr(), callback_id)); |
| 87 } | 101 } |
| 88 | 102 |
| 103 void OfflineInternalsUIMessageHandler::HandleDeleteAllRequests( | |
| 104 const base::ListValue* args) { | |
| 105 std::string callback_id; | |
| 106 CHECK(args->GetString(0, &callback_id)); | |
| 107 // First do a get, then in the callback, build a list of IDs, and | |
| 108 // call RemoveRequests with that list. | |
| 109 if (request_coordinator_) { | |
| 110 request_coordinator_->GetAllRequests( | |
| 111 base::Bind(&OfflineInternalsUIMessageHandler::DeleteAllGetCallback, | |
| 112 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests( | |
| 117 const base::ListValue* args) { | |
| 118 std::string callback_id; | |
| 119 CHECK(args->GetString(0, &callback_id)); | |
| 120 | |
| 121 std::vector<int64_t> offline_ids; | |
| 122 const base::ListValue* offline_ids_from_arg; | |
| 123 args->GetList(1, &offline_ids_from_arg); | |
| 124 | |
| 125 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { | |
| 126 std::string value; | |
| 127 offline_ids_from_arg->GetString(i, &value); | |
| 128 int64_t int_value; | |
| 129 base::StringToInt64(value, &int_value); | |
| 130 offline_ids.push_back(int_value); | |
| 131 } | |
| 132 | |
| 133 // Call RequestCoordinator to delete them | |
| 134 if (request_coordinator_) { | |
| 135 request_coordinator_->RemoveRequests( | |
| 136 offline_ids, | |
| 137 base::Bind( | |
| 138 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback, | |
| 139 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 void OfflineInternalsUIMessageHandler::DeleteAllGetCallback( | |
| 144 std::string callback_id, | |
| 145 std::vector<std::unique_ptr<offline_pages::SavePageRequest>> requests) { | |
| 146 std::vector<int64_t> offline_ids; | |
| 147 // Build a list of offline_ids from the requests. | |
| 148 for (const auto& request : requests) { | |
| 149 offline_ids.push_back(request->request_id()); | |
| 150 } | |
| 151 | |
| 152 // Call RequestCoordinator to delete them | |
| 153 if (request_coordinator_) { | |
| 154 request_coordinator_->RemoveRequests( | |
| 155 offline_ids, | |
| 156 base::Bind( | |
| 157 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback, | |
| 158 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 159 } | |
| 160 } | |
| 161 | |
| 89 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( | 162 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( |
| 90 std::string callback_id, | 163 std::string callback_id, |
| 91 offline_pages::DeletePageResult result) { | 164 offline_pages::DeletePageResult result) { |
| 92 ResolveJavascriptCallback( | 165 ResolveJavascriptCallback( |
| 93 base::StringValue(callback_id), | 166 base::StringValue(callback_id), |
| 94 base::StringValue(GetStringFromDeletePageResult(result))); | 167 base::StringValue(GetStringFromDeletePageResult(result))); |
| 95 } | 168 } |
| 96 | 169 |
| 170 void OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback( | |
| 171 std::string callback_id, | |
| 172 const offline_pages::RequestQueue::UpdateMultipleRequestResults& results) { | |
| 173 ResolveJavascriptCallback( | |
| 174 base::StringValue(callback_id), | |
| 175 base::StringValue(GetStringFromDeleteRequestResults(results))); | |
| 176 } | |
| 177 | |
| 97 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( | 178 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( |
| 98 std::string callback_id, | 179 std::string callback_id, |
| 99 const offline_pages::MultipleOfflinePageItemResult& pages) { | 180 const offline_pages::MultipleOfflinePageItemResult& pages) { |
| 100 base::ListValue results; | 181 base::ListValue results; |
| 101 | 182 |
| 102 for (const auto& page : pages) { | 183 for (const auto& page : pages) { |
| 103 base::DictionaryValue* offline_page = new base::DictionaryValue(); | 184 base::DictionaryValue* offline_page = new base::DictionaryValue(); |
| 104 results.Append(offline_page); | 185 results.Append(offline_page); |
| 105 offline_page->SetString("onlineUrl", page.url.spec()); | 186 offline_page->SetString("onlineUrl", page.url.spec()); |
| 106 offline_page->SetString("namespace", page.client_id.name_space); | 187 offline_page->SetString("namespace", page.client_id.name_space); |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 void OfflineInternalsUIMessageHandler::RegisterMessages() { | 343 void OfflineInternalsUIMessageHandler::RegisterMessages() { |
| 263 web_ui()->RegisterMessageCallback( | 344 web_ui()->RegisterMessageCallback( |
| 264 "deleteAllPages", | 345 "deleteAllPages", |
| 265 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, | 346 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, |
| 266 weak_ptr_factory_.GetWeakPtr())); | 347 weak_ptr_factory_.GetWeakPtr())); |
| 267 web_ui()->RegisterMessageCallback( | 348 web_ui()->RegisterMessageCallback( |
| 268 "deleteSelectedPages", | 349 "deleteSelectedPages", |
| 269 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, | 350 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, |
| 270 weak_ptr_factory_.GetWeakPtr())); | 351 weak_ptr_factory_.GetWeakPtr())); |
| 271 web_ui()->RegisterMessageCallback( | 352 web_ui()->RegisterMessageCallback( |
| 353 "deleteAllRequests", | |
| 354 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllRequests, | |
| 355 weak_ptr_factory_.GetWeakPtr())); | |
| 356 web_ui()->RegisterMessageCallback( | |
| 357 "deleteSelectedRequests", | |
| 358 base::Bind( | |
| 359 &OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests, | |
| 360 weak_ptr_factory_.GetWeakPtr())); | |
| 361 web_ui()->RegisterMessageCallback( | |
| 272 "getRequestQueue", | 362 "getRequestQueue", |
| 273 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue, | 363 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue, |
| 274 weak_ptr_factory_.GetWeakPtr())); | 364 weak_ptr_factory_.GetWeakPtr())); |
| 275 web_ui()->RegisterMessageCallback( | 365 web_ui()->RegisterMessageCallback( |
| 276 "getStoredPages", | 366 "getStoredPages", |
| 277 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages, | 367 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages, |
| 278 weak_ptr_factory_.GetWeakPtr())); | 368 weak_ptr_factory_.GetWeakPtr())); |
| 279 web_ui()->RegisterMessageCallback( | 369 web_ui()->RegisterMessageCallback( |
| 280 "getEventLogs", | 370 "getEventLogs", |
| 281 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs, | 371 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs, |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 303 | 393 |
| 304 // Get the offline page model associated with this web ui. | 394 // Get the offline page model associated with this web ui. |
| 305 Profile* profile = Profile::FromWebUI(web_ui()); | 395 Profile* profile = Profile::FromWebUI(web_ui()); |
| 306 offline_page_model_ = | 396 offline_page_model_ = |
| 307 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | 397 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); |
| 308 request_coordinator_ = | 398 request_coordinator_ = |
| 309 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | 399 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); |
| 310 } | 400 } |
| 311 | 401 |
| 312 } // namespace offline_internals | 402 } // namespace offline_internals |
| OLD | NEW |