Chromium Code Reviews| Index: chrome/browser/ui/webui/offline_internals_ui.cc |
| diff --git a/chrome/browser/ui/webui/offline_internals_ui.cc b/chrome/browser/ui/webui/offline_internals_ui.cc |
| index 946eaa0c25eb6fe6b88f8f63417f25a2b45e66f0..0b185ecbe03c431791e72eff5e28e50f84650df9 100644 |
| --- a/chrome/browser/ui/webui/offline_internals_ui.cc |
| +++ b/chrome/browser/ui/webui/offline_internals_ui.cc |
| @@ -4,14 +4,21 @@ |
| #include "chrome/browser/ui/webui/offline_internals_ui.h" |
| -#include <string> |
| +#include <stdint.h> |
| +#include <stdlib.h> |
| +#include <vector> |
| #include "base/bind.h" |
| #include "base/macros.h" |
| #include "base/memory/weak_ptr.h" |
| #include "base/values.h" |
| +#include "chrome/browser/android/offline_pages/offline_page_model_factory.h" |
| +#include "chrome/browser/android/offline_pages/request_coordinator_factory.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/common/url_constants.h" |
| +#include "components/offline_pages/background/request_coordinator.h" |
| +#include "components/offline_pages/background/save_page_request.h" |
| +#include "components/offline_pages/offline_page_model.h" |
| #include "content/public/browser/web_ui.h" |
| #include "content/public/browser/web_ui_controller.h" |
| #include "content/public/browser/web_ui_data_source.h" |
| @@ -36,8 +43,53 @@ class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { |
| // Delete selected list of page ids from the store. |
| void DeleteSelectedPages(const base::ListValue* args); |
| - // Load all information. |
| - void GetOfflineInternalsInfo(const base::ListValue* args); |
| + // Load Request Queue info. |
| + void GetRequestQueue(const base::ListValue* args); |
| + |
| + // Load Stored pages info. |
| + void GetStoredPages(const base::ListValue* args); |
| + |
| + // Callback for async GetAllPages calls. |
| + void HandleStoredPages( |
| + std::string callback_id, |
| + const offline_pages::MultipleOfflinePageItemResult& pages); |
| + |
| + // Callback for async GetRequests calls. |
| + void HandleRequestQueue( |
| + std::string callback_id, |
| + offline_pages::RequestQueue::GetRequestsResult result, |
| + const std::vector<offline_pages::SavePageRequest>& requests); |
| + |
| + // Callback for DeletePage/ClearAll calls. |
| + void HandleDeletedPages(std::string callback_id, |
| + const offline_pages::DeletePageResult result); |
| + |
| + std::string GetStringFromDeletePageResult( |
|
dewittj
2016/06/03 23:12:06
Please define out of line.
This might be better d
chili
2016/06/09 22:29:16
I was hesitating between putting this here vs offl
dewittj
2016/06/09 23:41:25
Not everyone is going to necessarily use this page
|
| + offline_pages::DeletePageResult value) { |
| + const std::string kDeletePageResultToString[] = { |
| + "Success", "Canceled", "Store failure", "Device failure", "Not found"}; |
| + int int_value = static_cast<int>(value); |
| + if (int_value >= 5) |
| + return "Unknown"; |
| + else |
| + return kDeletePageResultToString[int_value]; |
| + } |
| + |
| + std::string GetStringFromSavePageStatus( |
| + offline_pages::SavePageRequest::Status status) { |
| + const std::string kRequestStatusToString[] = { |
| + "Not ready", "Pending", "Started", "Failed", "Expired"}; |
| + int int_value = static_cast<int>(status); |
| + if (int_value >= 5) |
| + return "Unknown"; |
| + else |
| + return kRequestStatusToString[int_value]; |
| + } |
| + |
| + // Offline page model to call methods on. |
| + offline_pages::OfflinePageModel* offline_page_model_; |
| + // Request coordinator for background offline actions. |
| + offline_pages::RequestCoordinator* request_coordinator_; |
| // Factory for creating references in callbacks. |
| base::WeakPtrFactory<OfflineInternalsUIMessageHandler> weak_ptr_factory_; |
| @@ -54,34 +106,114 @@ void OfflineInternalsUIMessageHandler::DeleteAllPages( |
| const base::ListValue* args) { |
| std::string callback_id; |
| args->GetString(0, &callback_id); |
| - web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| - base::StringValue(callback_id), |
| - base::FundamentalValue(true), |
| - base::StringValue("success")); |
| + |
| + offline_page_model_->ClearAll( |
| + base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPages, |
| + weak_ptr_factory_.GetWeakPtr(), callback_id, |
| + offline_pages::DeletePageResult::SUCCESS)); |
|
dewittj
2016/06/03 23:12:06
Please add a comment that this is here because Cle
chili
2016/06/09 22:29:16
Done.
|
| } |
| void OfflineInternalsUIMessageHandler::DeleteSelectedPages( |
| const base::ListValue* args) { |
| std::string callback_id; |
| args->GetString(0, &callback_id); |
| - web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| - base::StringValue(callback_id), |
| + |
| + std::vector<int64_t> offline_ids; |
| + const base::ListValue* offline_ids_from_arg; |
| + args->GetList(1, &offline_ids_from_arg); |
| + |
| + for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { |
| + std::string value; |
| + offline_ids_from_arg->GetString(i, &value); |
| + offline_ids.push_back(atoll(value.c_str())); |
|
dewittj
2016/06/03 23:12:06
not sure if there is some function in Chromium to
chili
2016/06/09 22:29:16
you can set base in strtoll, but I didn't think th
|
| + } |
| + |
| + offline_page_model_->DeletePagesByOfflineId( |
| + offline_ids, |
| + base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPages, |
| + weak_ptr_factory_.GetWeakPtr(), callback_id)); |
| +} |
| + |
| +void OfflineInternalsUIMessageHandler::HandleDeletedPages( |
| + std::string callback_id, |
| + offline_pages::DeletePageResult result) { |
| + web_ui()->CallJavascriptFunction( |
| + "cr.webUIResponse", base::StringValue(callback_id), |
| base::FundamentalValue(true), |
| - base::StringValue("success")); |
| + base::StringValue(GetStringFromDeletePageResult(result))); |
| +} |
| + |
| +void OfflineInternalsUIMessageHandler::HandleStoredPages( |
| + std::string callback_id, |
| + const offline_pages::MultipleOfflinePageItemResult& pages) { |
| + base::ListValue results; |
| + |
| + for (const auto& page : pages) { |
| + base::DictionaryValue* js_page_object = new base::DictionaryValue(); |
| + results.Append(js_page_object); |
| + js_page_object->SetString("onlineUrl", page.url.spec()); |
|
dewittj
2016/06/03 23:12:06
I wish we didn't have so many hard-coded strings a
chili
2016/06/09 22:29:16
I do too, but I chose to hard-code them here rathe
|
| + js_page_object->SetString("namespace", page.client_id.name_space); |
| + js_page_object->SetDouble("size", page.file_size); |
| + js_page_object->SetString("id", std::to_string(page.offline_id)); |
| + js_page_object->SetString("filePath", page.file_path.value()); |
| + js_page_object->SetDouble("creationTime", page.creation_time.ToJsTime()); |
| + js_page_object->SetDouble("lastAccessedTime", |
| + page.last_access_time.ToJsTime()); |
| + js_page_object->SetInteger("accessCount", page.access_count); |
| + } |
| + web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| + base::StringValue(callback_id), |
| + base::FundamentalValue(true), results); |
| } |
| -void OfflineInternalsUIMessageHandler::GetOfflineInternalsInfo( |
| +void OfflineInternalsUIMessageHandler::HandleRequestQueue( |
| + std::string callback_id, |
| + offline_pages::RequestQueue::GetRequestsResult result, |
| + const std::vector<offline_pages::SavePageRequest>& requests) { |
| + base::ListValue js_requests; |
| + if (result == offline_pages::RequestQueue::GetRequestsResult::kSuccess) { |
| + for (const auto& request : requests) { |
| + base::DictionaryValue* js_request_object = new base::DictionaryValue(); |
| + js_requests.Append(js_request_object); |
| + js_request_object->SetString("onlineUrl", request.url().spec()); |
| + js_request_object->SetDouble("creationTime", |
| + request.creation_time().ToJsTime()); |
| + js_request_object->SetString( |
| + "status", |
| + GetStringFromSavePageStatus(request.GetStatus(base::Time::Now()))); |
| + js_request_object->SetString("namespace", request.client_id().name_space); |
| + js_request_object->SetDouble("lastAttempt", |
| + request.last_attempt_time().ToJsTime()); |
| + js_request_object->SetString("id", std::to_string(request.request_id())); |
| + } |
| + } |
| + web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| + base::StringValue(callback_id), |
| + base::FundamentalValue(true), js_requests); |
| +} |
| + |
| +void OfflineInternalsUIMessageHandler::GetRequestQueue( |
| const base::ListValue* args) { |
| std::string callback_id; |
| args->GetString(0, &callback_id); |
| - base::DictionaryValue results; |
| - results.Set("AllPages", new base::ListValue()); |
| - results.Set("Queue", new base::ListValue()); |
| + base::ListValue results; |
| - web_ui()->CallJavascriptFunction("cr.webUIResponse", |
| - base::StringValue(callback_id), |
| - base::FundamentalValue(true), |
| - results); |
| + if (request_coordinator_ != nullptr) |
| + request_coordinator_->queue()->GetRequests( |
| + base::Bind(&OfflineInternalsUIMessageHandler::HandleRequestQueue, |
| + weak_ptr_factory_.GetWeakPtr(), callback_id)); |
| +} |
| + |
| +void OfflineInternalsUIMessageHandler::GetStoredPages( |
| + const base::ListValue* args) { |
| + std::string callback_id; |
| + args->GetString(0, &callback_id); |
| + base::ListValue results; |
| + |
| + if (offline_page_model_ != nullptr) |
| + offline_page_model_->GetAllPages( |
| + base::Bind(&OfflineInternalsUIMessageHandler::HandleStoredPages, |
| + weak_ptr_factory_.GetWeakPtr(), callback_id)); |
| } |
| void OfflineInternalsUIMessageHandler::RegisterMessages() { |
| @@ -94,9 +226,20 @@ void OfflineInternalsUIMessageHandler::RegisterMessages() { |
| base::Bind(&OfflineInternalsUIMessageHandler::DeleteSelectedPages, |
| weak_ptr_factory_.GetWeakPtr())); |
| web_ui()->RegisterMessageCallback( |
| - "getOfflineInternalsInfo", |
| - base::Bind(&OfflineInternalsUIMessageHandler::GetOfflineInternalsInfo, |
| + "getRequestQueueInfo", |
| + base::Bind(&OfflineInternalsUIMessageHandler::GetRequestQueue, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + web_ui()->RegisterMessageCallback( |
| + "getStoredPagesInfo", |
| + base::Bind(&OfflineInternalsUIMessageHandler::GetStoredPages, |
| weak_ptr_factory_.GetWeakPtr())); |
| + |
| + // Get the offline page model associated with this web ui. |
| + Profile* profile = Profile::FromWebUI(web_ui()); |
| + offline_page_model_ = |
| + offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); |
| + request_coordinator_ = |
| + offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); |
| } |
| } // namespace |