Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(107)

Side by Side Diff: chrome/browser/ui/webui/offline/offline_internals_ui_message_handler.cc

Issue 2328973003: Adds a delete button to chrome:offline-internals (Closed)
Patch Set: CR feedback per Chili Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 std::string failed_ids;
55 for (const auto& result : results) {
56 if (result.second ==
57 offline_pages::RequestQueue::UpdateRequestResult::STORE_FAILURE) {
58 failed = true;
59 failed_ids += std::to_string(result.first);
60 failed_ids += ", ";
61 }
62 }
63 if (failed)
64 return "Store failure, could not delete urls: " + failed_ids;
chili 2016/09/12 23:09:48 nit: "Store failure, could not delete ids: " ? I'
Pete Williamson 2016/09/13 00:47:56 Good point, the request ID is not that helpful. M
65 return "Success";
66 }
67
50 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { 68 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() {
51 return "Available"; 69 return "Available";
52 } 70 }
53 71
54 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( 72 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages(
55 const base::ListValue* args) { 73 const base::ListValue* args) {
56 std::string callback_id; 74 std::string callback_id;
57 CHECK(args->GetString(0, &callback_id)); 75 CHECK(args->GetString(0, &callback_id));
58 76
59 // Pass back success because ClearAll doesn't return a status. 77 // Pass back success because ClearAll doesn't return a status.
(...skipping 19 matching lines...) Expand all
79 base::StringToInt64(value, &int_value); 97 base::StringToInt64(value, &int_value);
80 offline_ids.push_back(int_value); 98 offline_ids.push_back(int_value);
81 } 99 }
82 100
83 offline_page_model_->DeletePagesByOfflineId( 101 offline_page_model_->DeletePagesByOfflineId(
84 offline_ids, 102 offline_ids,
85 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, 103 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback,
86 weak_ptr_factory_.GetWeakPtr(), callback_id)); 104 weak_ptr_factory_.GetWeakPtr(), callback_id));
87 } 105 }
88 106
107 void OfflineInternalsUIMessageHandler::HandleDeleteAllRequests(
108 const base::ListValue* args) {
109 std::string callback_id;
110 CHECK(args->GetString(0, &callback_id));
111 // First do a get, then in the callback, build a list of IDs, and
112 // call RemoveRequests with that list.
113 if (request_coordinator_) {
114 request_coordinator_->GetAllRequests(
115 base::Bind(&OfflineInternalsUIMessageHandler::
116 HandleGetAllRequestsForDeleteCallback,
117 weak_ptr_factory_.GetWeakPtr(), callback_id));
118 }
119 }
120
121 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests(
122 const base::ListValue* args) {
123 std::string callback_id;
124 CHECK(args->GetString(0, &callback_id));
125
126 std::vector<int64_t> offline_ids;
127 const base::ListValue* offline_ids_from_arg;
128 args->GetList(1, &offline_ids_from_arg);
129
130 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) {
131 std::string value;
132 offline_ids_from_arg->GetString(i, &value);
133 int64_t int_value;
134 base::StringToInt64(value, &int_value);
135 offline_ids.push_back(int_value);
136 }
137
138 // Call RequestCoordinator to delete them
139 if (request_coordinator_) {
140 request_coordinator_->RemoveRequests(
141 offline_ids,
142 base::Bind(
143 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback,
144 weak_ptr_factory_.GetWeakPtr(), callback_id));
145 }
146 }
147
148 void OfflineInternalsUIMessageHandler::HandleGetAllRequestsForDeleteCallback(
149 std::string callback_id,
150 std::vector<std::unique_ptr<offline_pages::SavePageRequest>> requests) {
151 std::vector<int64_t> offline_ids;
152 // Build a list of offline_ids from the requests.
153 for (const auto& request : requests) {
154 offline_ids.push_back(request->request_id());
155 }
156
157 // Call RequestCoordinator to delete them
158 if (request_coordinator_) {
159 request_coordinator_->RemoveRequests(
160 offline_ids,
161 base::Bind(
162 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback,
163 weak_ptr_factory_.GetWeakPtr(), callback_id));
164 }
165 }
166
89 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( 167 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback(
90 std::string callback_id, 168 std::string callback_id,
91 offline_pages::DeletePageResult result) { 169 offline_pages::DeletePageResult result) {
92 ResolveJavascriptCallback( 170 ResolveJavascriptCallback(
93 base::StringValue(callback_id), 171 base::StringValue(callback_id),
94 base::StringValue(GetStringFromDeletePageResult(result))); 172 base::StringValue(GetStringFromDeletePageResult(result)));
95 } 173 }
96 174
175 void OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback(
176 std::string callback_id,
177 const offline_pages::RequestQueue::UpdateMultipleRequestResults& results) {
178 ResolveJavascriptCallback(
179 base::StringValue(callback_id),
180 base::StringValue(GetStringFromDeleteRequestResults(results)));
181 }
182
97 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( 183 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback(
98 std::string callback_id, 184 std::string callback_id,
99 const offline_pages::MultipleOfflinePageItemResult& pages) { 185 const offline_pages::MultipleOfflinePageItemResult& pages) {
100 base::ListValue results; 186 base::ListValue results;
101 187
102 for (const auto& page : pages) { 188 for (const auto& page : pages) {
103 base::DictionaryValue* offline_page = new base::DictionaryValue(); 189 base::DictionaryValue* offline_page = new base::DictionaryValue();
104 results.Append(offline_page); 190 results.Append(offline_page);
105 offline_page->SetString("onlineUrl", page.url.spec()); 191 offline_page->SetString("onlineUrl", page.url.spec());
106 offline_page->SetString("namespace", page.client_id.name_space); 192 offline_page->SetString("namespace", page.client_id.name_space);
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 void OfflineInternalsUIMessageHandler::RegisterMessages() { 348 void OfflineInternalsUIMessageHandler::RegisterMessages() {
263 web_ui()->RegisterMessageCallback( 349 web_ui()->RegisterMessageCallback(
264 "deleteAllPages", 350 "deleteAllPages",
265 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, 351 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages,
266 weak_ptr_factory_.GetWeakPtr())); 352 weak_ptr_factory_.GetWeakPtr()));
267 web_ui()->RegisterMessageCallback( 353 web_ui()->RegisterMessageCallback(
268 "deleteSelectedPages", 354 "deleteSelectedPages",
269 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, 355 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages,
270 weak_ptr_factory_.GetWeakPtr())); 356 weak_ptr_factory_.GetWeakPtr()));
271 web_ui()->RegisterMessageCallback( 357 web_ui()->RegisterMessageCallback(
358 "deleteAllRequests",
359 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllRequests,
360 weak_ptr_factory_.GetWeakPtr()));
361 web_ui()->RegisterMessageCallback(
362 "deleteSelectedRequests",
363 base::Bind(
364 &OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests,
365 weak_ptr_factory_.GetWeakPtr()));
366 web_ui()->RegisterMessageCallback(
272 "getRequestQueue", 367 "getRequestQueue",
273 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue, 368 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue,
274 weak_ptr_factory_.GetWeakPtr())); 369 weak_ptr_factory_.GetWeakPtr()));
275 web_ui()->RegisterMessageCallback( 370 web_ui()->RegisterMessageCallback(
276 "getStoredPages", 371 "getStoredPages",
277 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages, 372 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages,
278 weak_ptr_factory_.GetWeakPtr())); 373 weak_ptr_factory_.GetWeakPtr()));
279 web_ui()->RegisterMessageCallback( 374 web_ui()->RegisterMessageCallback(
280 "getEventLogs", 375 "getEventLogs",
281 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs, 376 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs,
(...skipping 21 matching lines...) Expand all
303 398
304 // Get the offline page model associated with this web ui. 399 // Get the offline page model associated with this web ui.
305 Profile* profile = Profile::FromWebUI(web_ui()); 400 Profile* profile = Profile::FromWebUI(web_ui());
306 offline_page_model_ = 401 offline_page_model_ =
307 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); 402 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile);
308 request_coordinator_ = 403 request_coordinator_ =
309 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); 404 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile);
310 } 405 }
311 406
312 } // namespace offline_internals 407 } // namespace offline_internals
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698