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

Side by Side Diff: chrome/browser/ui/webui/offline_internals_ui.cc

Issue 2172443004: [Offline Pages] Add network status, save request textbox, and offline url link to offline internals… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 5 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_internals_ui.h" 5 #include "chrome/browser/ui/webui/offline_internals_ui.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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/weak_ptr.h" 13 #include "base/memory/weak_ptr.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/values.h" 15 #include "base/values.h"
16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h"
17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" 17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h"
18 #include "chrome/browser/profiles/profile.h" 18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/url_constants.h" 19 #include "chrome/common/url_constants.h"
20 #include "components/offline_pages/background/request_coordinator.h" 20 #include "components/offline_pages/background/request_coordinator.h"
21 #include "components/offline_pages/background/save_page_request.h" 21 #include "components/offline_pages/background/save_page_request.h"
22 #include "components/offline_pages/client_namespace_constants.h"
22 #include "components/offline_pages/offline_page_model.h" 23 #include "components/offline_pages/offline_page_model.h"
23 #include "content/public/browser/web_ui.h" 24 #include "content/public/browser/web_ui.h"
24 #include "content/public/browser/web_ui_controller.h" 25 #include "content/public/browser/web_ui_controller.h"
25 #include "content/public/browser/web_ui_data_source.h" 26 #include "content/public/browser/web_ui_data_source.h"
26 #include "content/public/browser/web_ui_message_handler.h" 27 #include "content/public/browser/web_ui_message_handler.h"
27 #include "grit/browser_resources.h" 28 #include "grit/browser_resources.h"
29 #include "net/base/network_change_notifier.h"
28 30
29 namespace { 31 namespace {
30 32
31 // Class acting as a controller of the chrome://offline-internals WebUI. 33 // Class acting as a controller of the chrome://offline-internals WebUI.
32 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { 34 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler {
dpapad 2016/07/21 18:57:25 Optional suggestion for follow up cleanup CL. - Mo
chili 2016/07/21 23:57:20 Added TODO. Ack. (There's no 'internal cleanup' b
33 public: 35 public:
34 OfflineInternalsUIMessageHandler(); 36 OfflineInternalsUIMessageHandler();
35 ~OfflineInternalsUIMessageHandler() override; 37 ~OfflineInternalsUIMessageHandler() override;
36 38
37 // WebUIMessageHandler implementation. 39 // WebUIMessageHandler implementation.
38 void RegisterMessages() override; 40 void RegisterMessages() override;
39 41
40 private: 42 private:
41 // Deletes all the pages in the store. 43 // Deletes all the pages in the store.
42 void HandleDeleteAllPages(const base::ListValue* args); 44 void HandleDeleteAllPages(const base::ListValue* args);
(...skipping 12 matching lines...) Expand all
55 57
56 // Set whether to record request queue events. 58 // Set whether to record request queue events.
57 void HandleSetRecordRequestQueue(const base::ListValue* args); 59 void HandleSetRecordRequestQueue(const base::ListValue* args);
58 60
59 // Load both Page Model and Request Queue event logs. 61 // Load both Page Model and Request Queue event logs.
60 void HandleGetEventLogs(const base::ListValue* args); 62 void HandleGetEventLogs(const base::ListValue* args);
61 63
62 // Load whether logs are being recorded. 64 // Load whether logs are being recorded.
63 void HandleGetLoggingState(const base::ListValue* args); 65 void HandleGetLoggingState(const base::ListValue* args);
64 66
67 // Adds a url to the background loader queue.
68 void HandleAddToRequestQueue(const base::ListValue* args);
69
70 // Load whether device is currently offline.
71 void HandleGetNetworkStatus(const base::ListValue* args);
72
65 // Callback for async GetAllPages calls. 73 // Callback for async GetAllPages calls.
66 void HandleStoredPagesCallback( 74 void HandleStoredPagesCallback(
67 std::string callback_id, 75 std::string callback_id,
68 const offline_pages::MultipleOfflinePageItemResult& pages); 76 const offline_pages::MultipleOfflinePageItemResult& pages);
69 77
70 // Callback for async GetRequests calls. 78 // Callback for async GetRequests calls.
71 void HandleRequestQueueCallback( 79 void HandleRequestQueueCallback(
72 std::string callback_id, 80 std::string callback_id,
73 offline_pages::RequestQueue::GetRequestsResult result, 81 offline_pages::RequestQueue::GetRequestsResult result,
74 const std::vector<offline_pages::SavePageRequest>& requests); 82 const std::vector<offline_pages::SavePageRequest>& requests);
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 const offline_pages::MultipleOfflinePageItemResult& pages) { 198 const offline_pages::MultipleOfflinePageItemResult& pages) {
191 base::ListValue results; 199 base::ListValue results;
192 200
193 for (const auto& page : pages) { 201 for (const auto& page : pages) {
194 base::DictionaryValue* offline_page = new base::DictionaryValue(); 202 base::DictionaryValue* offline_page = new base::DictionaryValue();
195 results.Append(offline_page); 203 results.Append(offline_page);
196 offline_page->SetString("onlineUrl", page.url.spec()); 204 offline_page->SetString("onlineUrl", page.url.spec());
197 offline_page->SetString("namespace", page.client_id.name_space); 205 offline_page->SetString("namespace", page.client_id.name_space);
198 offline_page->SetDouble("size", page.file_size); 206 offline_page->SetDouble("size", page.file_size);
199 offline_page->SetString("id", std::to_string(page.offline_id)); 207 offline_page->SetString("id", std::to_string(page.offline_id));
200 offline_page->SetString("filePath", page.file_path.value()); 208 offline_page->SetString("filePath", page.GetOfflineURL().spec());
201 offline_page->SetDouble("creationTime", page.creation_time.ToJsTime()); 209 offline_page->SetDouble("creationTime", page.creation_time.ToJsTime());
202 offline_page->SetDouble("lastAccessTime", 210 offline_page->SetDouble("lastAccessTime",
203 page.last_access_time.ToJsTime()); 211 page.last_access_time.ToJsTime());
204 offline_page->SetInteger("accessCount", page.access_count); 212 offline_page->SetInteger("accessCount", page.access_count);
205 offline_page->SetString("isExpired", page.IsExpired() ? "Yes" : "No"); 213 offline_page->SetString("isExpired", page.IsExpired() ? "Yes" : "No");
206 } 214 }
207 ResolveJavascriptCallback(base::StringValue(callback_id), results); 215 ResolveJavascriptCallback(base::StringValue(callback_id), results);
208 } 216 }
209 217
210 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback( 218 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback(
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 } 271 }
264 } 272 }
265 273
266 void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel( 274 void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel(
267 const base::ListValue* args) { 275 const base::ListValue* args) {
268 bool should_record; 276 bool should_record;
269 CHECK(args->GetBoolean(0, &should_record)); 277 CHECK(args->GetBoolean(0, &should_record));
270 offline_page_model_->GetLogger()->SetIsLogging(should_record); 278 offline_page_model_->GetLogger()->SetIsLogging(should_record);
271 } 279 }
272 280
281 void OfflineInternalsUIMessageHandler::HandleGetNetworkStatus(
282 const base::ListValue* args) {
283 const base::Value* callback_id;
284 CHECK(args->Get(0, &callback_id));
285
286 if (net::NetworkChangeNotifier::IsOffline()) {
dpapad 2016/07/21 18:57:25 Nit: I think you can compress this by using the te
chili 2016/07/21 23:57:20 Done.
287 ResolveJavascriptCallback(*callback_id, base::StringValue("Offline"));
288 } else {
289 ResolveJavascriptCallback(*callback_id, base::StringValue("Online"));
290 }
291 }
292
273 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( 293 void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue(
274 const base::ListValue* args) { 294 const base::ListValue* args) {
275 bool should_record; 295 bool should_record;
276 CHECK(args->GetBoolean(0, &should_record)); 296 CHECK(args->GetBoolean(0, &should_record));
277 request_coordinator_->GetLogger()->SetIsLogging(should_record); 297 request_coordinator_->GetLogger()->SetIsLogging(should_record);
278 } 298 }
279 299
280 void OfflineInternalsUIMessageHandler::HandleGetLoggingState( 300 void OfflineInternalsUIMessageHandler::HandleGetLoggingState(
281 const base::ListValue* args) { 301 const base::ListValue* args) {
282 AllowJavascript(); 302 AllowJavascript();
(...skipping 18 matching lines...) Expand all
301 offline_page_model_->GetLogger()->GetLogs(&logs); 321 offline_page_model_->GetLogger()->GetLogs(&logs);
302 request_coordinator_->GetLogger()->GetLogs(&logs); 322 request_coordinator_->GetLogger()->GetLogs(&logs);
303 std::sort(logs.begin(), logs.end()); 323 std::sort(logs.begin(), logs.end());
304 324
305 base::ListValue result; 325 base::ListValue result;
306 result.AppendStrings(logs); 326 result.AppendStrings(logs);
307 327
308 ResolveJavascriptCallback(*callback_id, result); 328 ResolveJavascriptCallback(*callback_id, result);
309 } 329 }
310 330
331 void OfflineInternalsUIMessageHandler::HandleAddToRequestQueue(
332 const base::ListValue* args) {
333 const base::Value* callback_id;
334 CHECK(args->Get(0, &callback_id));
335
336 std::string url;
337 CHECK(args->GetString(1, &url));
338
339 bool result = request_coordinator_->SavePageLater(
340 GURL(url),
341 offline_pages::ClientId(offline_pages::kAsyncNamespace, "foo"));
dpapad 2016/07/21 18:57:25 It is not clear here why "foo" is hardcoded. Pleas
chili 2016/07/21 23:57:20 I changed this in a following version but must've
342
343 ResolveJavascriptCallback(*callback_id, base::FundamentalValue(result));
344 }
345
311 void OfflineInternalsUIMessageHandler::RegisterMessages() { 346 void OfflineInternalsUIMessageHandler::RegisterMessages() {
312 web_ui()->RegisterMessageCallback( 347 web_ui()->RegisterMessageCallback(
313 "deleteAllPages", 348 "deleteAllPages",
314 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, 349 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages,
315 weak_ptr_factory_.GetWeakPtr())); 350 weak_ptr_factory_.GetWeakPtr()));
316 web_ui()->RegisterMessageCallback( 351 web_ui()->RegisterMessageCallback(
317 "deleteSelectedPages", 352 "deleteSelectedPages",
318 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, 353 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages,
319 weak_ptr_factory_.GetWeakPtr())); 354 weak_ptr_factory_.GetWeakPtr()));
320 web_ui()->RegisterMessageCallback( 355 web_ui()->RegisterMessageCallback(
(...skipping 13 matching lines...) Expand all
334 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue, 369 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue,
335 weak_ptr_factory_.GetWeakPtr())); 370 weak_ptr_factory_.GetWeakPtr()));
336 web_ui()->RegisterMessageCallback( 371 web_ui()->RegisterMessageCallback(
337 "setRecordPageModel", 372 "setRecordPageModel",
338 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordPageModel, 373 base::Bind(&OfflineInternalsUIMessageHandler::HandleSetRecordPageModel,
339 weak_ptr_factory_.GetWeakPtr())); 374 weak_ptr_factory_.GetWeakPtr()));
340 web_ui()->RegisterMessageCallback( 375 web_ui()->RegisterMessageCallback(
341 "getLoggingState", 376 "getLoggingState",
342 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState, 377 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState,
343 weak_ptr_factory_.GetWeakPtr())); 378 weak_ptr_factory_.GetWeakPtr()));
379 web_ui()->RegisterMessageCallback(
380 "addToRequestQueue",
381 base::Bind(&OfflineInternalsUIMessageHandler::HandleAddToRequestQueue,
382 weak_ptr_factory_.GetWeakPtr()));
383 web_ui()->RegisterMessageCallback(
384 "getNetworkStatus",
385 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus,
386 weak_ptr_factory_.GetWeakPtr()));
344 387
345 // Get the offline page model associated with this web ui. 388 // Get the offline page model associated with this web ui.
346 Profile* profile = Profile::FromWebUI(web_ui()); 389 Profile* profile = Profile::FromWebUI(web_ui());
347 offline_page_model_ = 390 offline_page_model_ =
348 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); 391 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile);
349 request_coordinator_ = 392 request_coordinator_ =
350 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); 393 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile);
351 } 394 }
352 395
353 } // namespace 396 } // namespace
(...skipping 13 matching lines...) Expand all
367 html_source->AddResourcePath("offline_internals_browser_proxy.js", 410 html_source->AddResourcePath("offline_internals_browser_proxy.js",
368 IDR_OFFLINE_INTERNALS_BROWSER_PROXY_JS); 411 IDR_OFFLINE_INTERNALS_BROWSER_PROXY_JS);
369 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); 412 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML);
370 413
371 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); 414 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source);
372 415
373 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); 416 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler());
374 } 417 }
375 418
376 OfflineInternalsUI::~OfflineInternalsUI() {} 419 OfflineInternalsUI::~OfflineInternalsUI() {}
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698