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

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: More 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
« no previous file with comments | « chrome/browser/ui/webui/offline/offline_internals_ui_message_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 for (const auto& result : results) {
55 if (result.second ==
56 offline_pages::RequestQueue::UpdateRequestResult::STORE_FAILURE) {
57 failed = true;
Bernhard Bauer 2016/09/13 08:29:05 You could return here right away.
Pete Williamson 2016/09/13 17:03:26 Done.
58 }
59 }
60 if (failed)
61 return "Store failure, could not delete one or more requests";
62 return "Success";
63 }
64
50 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() { 65 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus() {
51 return "Available"; 66 return "Available";
52 } 67 }
53 68
54 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( 69 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages(
55 const base::ListValue* args) { 70 const base::ListValue* args) {
56 std::string callback_id; 71 std::string callback_id;
57 CHECK(args->GetString(0, &callback_id)); 72 CHECK(args->GetString(0, &callback_id));
58 73
59 // Pass back success because ClearAll doesn't return a status. 74 // Pass back success because ClearAll doesn't return a status.
(...skipping 19 matching lines...) Expand all
79 base::StringToInt64(value, &int_value); 94 base::StringToInt64(value, &int_value);
80 offline_ids.push_back(int_value); 95 offline_ids.push_back(int_value);
81 } 96 }
82 97
83 offline_page_model_->DeletePagesByOfflineId( 98 offline_page_model_->DeletePagesByOfflineId(
84 offline_ids, 99 offline_ids,
85 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, 100 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback,
86 weak_ptr_factory_.GetWeakPtr(), callback_id)); 101 weak_ptr_factory_.GetWeakPtr(), callback_id));
87 } 102 }
88 103
104 void OfflineInternalsUIMessageHandler::HandleDeleteAllRequests(
105 const base::ListValue* args) {
106 std::string callback_id;
107 CHECK(args->GetString(0, &callback_id));
108 // First do a get, then in the callback, build a list of IDs, and
109 // call RemoveRequests with that list.
110 if (request_coordinator_) {
111 request_coordinator_->GetAllRequests(
112 base::Bind(&OfflineInternalsUIMessageHandler::
113 HandleGetAllRequestsForDeleteCallback,
114 weak_ptr_factory_.GetWeakPtr(), callback_id));
115 }
116 }
117
118 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests(
119 const base::ListValue* args) {
120 std::string callback_id;
121 CHECK(args->GetString(0, &callback_id));
122
123 std::vector<int64_t> offline_ids;
124 const base::ListValue* offline_ids_from_arg;
125 args->GetList(1, &offline_ids_from_arg);
Bernhard Bauer 2016/09/13 08:29:05 If GetList() fails, you'll access an uninitialized
Pete Williamson 2016/09/13 17:03:27 Done.
126
127 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) {
128 std::string value;
129 offline_ids_from_arg->GetString(i, &value);
130 int64_t int_value;
131 base::StringToInt64(value, &int_value);
132 offline_ids.push_back(int_value);
133 }
134
135 // Call RequestCoordinator to delete them
136 if (request_coordinator_) {
137 request_coordinator_->RemoveRequests(
138 offline_ids,
139 base::Bind(
140 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback,
141 weak_ptr_factory_.GetWeakPtr(), callback_id));
142 }
143 }
144
145 void OfflineInternalsUIMessageHandler::HandleGetAllRequestsForDeleteCallback(
146 std::string callback_id,
147 std::vector<std::unique_ptr<offline_pages::SavePageRequest>> requests) {
148 std::vector<int64_t> offline_ids;
149 // Build a list of offline_ids from the requests.
150 for (const auto& request : requests) {
151 offline_ids.push_back(request->request_id());
152 }
153
154 // Call RequestCoordinator to delete them
155 if (request_coordinator_) {
156 request_coordinator_->RemoveRequests(
157 offline_ids,
158 base::Bind(
159 &OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback,
160 weak_ptr_factory_.GetWeakPtr(), callback_id));
161 }
162 }
163
89 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( 164 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback(
90 std::string callback_id, 165 std::string callback_id,
91 offline_pages::DeletePageResult result) { 166 offline_pages::DeletePageResult result) {
92 ResolveJavascriptCallback( 167 ResolveJavascriptCallback(
93 base::StringValue(callback_id), 168 base::StringValue(callback_id),
94 base::StringValue(GetStringFromDeletePageResult(result))); 169 base::StringValue(GetStringFromDeletePageResult(result)));
95 } 170 }
96 171
172 void OfflineInternalsUIMessageHandler::HandleDeletedRequestsCallback(
173 std::string callback_id,
174 const offline_pages::RequestQueue::UpdateMultipleRequestResults& results) {
175 ResolveJavascriptCallback(
176 base::StringValue(callback_id),
177 base::StringValue(GetStringFromDeleteRequestResults(results)));
178 }
179
97 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( 180 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback(
98 std::string callback_id, 181 std::string callback_id,
99 const offline_pages::MultipleOfflinePageItemResult& pages) { 182 const offline_pages::MultipleOfflinePageItemResult& pages) {
100 base::ListValue results; 183 base::ListValue results;
101 184
102 for (const auto& page : pages) { 185 for (const auto& page : pages) {
103 base::DictionaryValue* offline_page = new base::DictionaryValue(); 186 base::DictionaryValue* offline_page = new base::DictionaryValue();
104 results.Append(offline_page); 187 results.Append(offline_page);
105 offline_page->SetString("onlineUrl", page.url.spec()); 188 offline_page->SetString("onlineUrl", page.url.spec());
106 offline_page->SetString("namespace", page.client_id.name_space); 189 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() { 345 void OfflineInternalsUIMessageHandler::RegisterMessages() {
263 web_ui()->RegisterMessageCallback( 346 web_ui()->RegisterMessageCallback(
264 "deleteAllPages", 347 "deleteAllPages",
265 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, 348 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages,
266 weak_ptr_factory_.GetWeakPtr())); 349 weak_ptr_factory_.GetWeakPtr()));
267 web_ui()->RegisterMessageCallback( 350 web_ui()->RegisterMessageCallback(
268 "deleteSelectedPages", 351 "deleteSelectedPages",
269 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, 352 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages,
270 weak_ptr_factory_.GetWeakPtr())); 353 weak_ptr_factory_.GetWeakPtr()));
271 web_ui()->RegisterMessageCallback( 354 web_ui()->RegisterMessageCallback(
355 "deleteAllRequests",
356 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllRequests,
357 weak_ptr_factory_.GetWeakPtr()));
358 web_ui()->RegisterMessageCallback(
359 "deleteSelectedRequests",
360 base::Bind(
361 &OfflineInternalsUIMessageHandler::HandleDeleteSelectedRequests,
362 weak_ptr_factory_.GetWeakPtr()));
363 web_ui()->RegisterMessageCallback(
272 "getRequestQueue", 364 "getRequestQueue",
273 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue, 365 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue,
274 weak_ptr_factory_.GetWeakPtr())); 366 weak_ptr_factory_.GetWeakPtr()));
275 web_ui()->RegisterMessageCallback( 367 web_ui()->RegisterMessageCallback(
276 "getStoredPages", 368 "getStoredPages",
277 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages, 369 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages,
278 weak_ptr_factory_.GetWeakPtr())); 370 weak_ptr_factory_.GetWeakPtr()));
279 web_ui()->RegisterMessageCallback( 371 web_ui()->RegisterMessageCallback(
280 "getEventLogs", 372 "getEventLogs",
281 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs, 373 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetEventLogs,
(...skipping 21 matching lines...) Expand all
303 395
304 // Get the offline page model associated with this web ui. 396 // Get the offline page model associated with this web ui.
305 Profile* profile = Profile::FromWebUI(web_ui()); 397 Profile* profile = Profile::FromWebUI(web_ui());
306 offline_page_model_ = 398 offline_page_model_ =
307 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); 399 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile);
308 request_coordinator_ = 400 request_coordinator_ =
309 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); 401 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile);
310 } 402 }
311 403
312 } // namespace offline_internals 404 } // namespace offline_internals
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/offline/offline_internals_ui_message_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698