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_internals_ui.h" | 5 #include "chrome/browser/ui/webui/offline_internals_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <stdint.h> |
| 8 #include <stdlib.h> | |
| 9 #include <vector> | |
| 8 | 10 |
| 9 #include "base/bind.h" | 11 #include "base/bind.h" |
| 10 #include "base/macros.h" | 12 #include "base/macros.h" |
| 11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/strings/string_number_conversions.h" | |
| 12 #include "base/values.h" | 15 #include "base/values.h" |
| 16 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
| 17 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/common/url_constants.h" | 19 #include "chrome/common/url_constants.h" |
| 20 #include "components/offline_pages/background/request_coordinator.h" | |
| 21 #include "components/offline_pages/background/save_page_request.h" | |
| 22 #include "components/offline_pages/offline_page_model.h" | |
| 15 #include "content/public/browser/web_ui.h" | 23 #include "content/public/browser/web_ui.h" |
| 16 #include "content/public/browser/web_ui_controller.h" | 24 #include "content/public/browser/web_ui_controller.h" |
| 17 #include "content/public/browser/web_ui_data_source.h" | 25 #include "content/public/browser/web_ui_data_source.h" |
| 18 #include "content/public/browser/web_ui_message_handler.h" | 26 #include "content/public/browser/web_ui_message_handler.h" |
| 19 #include "grit/browser_resources.h" | 27 #include "grit/browser_resources.h" |
| 20 | 28 |
| 21 namespace { | 29 namespace { |
| 22 | 30 |
| 23 // Class acting as a controller of the chrome://offline-internals WebUI. | 31 // Class acting as a controller of the chrome://offline-internals WebUI. |
| 24 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { | 32 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { |
| 25 public: | 33 public: |
| 26 OfflineInternalsUIMessageHandler(); | 34 OfflineInternalsUIMessageHandler(); |
| 27 ~OfflineInternalsUIMessageHandler() override; | 35 ~OfflineInternalsUIMessageHandler() override; |
| 28 | 36 |
| 29 // WebUIMessageHandler implementation. | 37 // WebUIMessageHandler implementation. |
| 30 void RegisterMessages() override; | 38 void RegisterMessages() override; |
| 31 | 39 |
| 32 private: | 40 private: |
| 33 // Deletes all the pages in the store. | 41 // Deletes all the pages in the store. |
| 34 void HandleDeleteAllPages(const base::ListValue* args); | 42 void HandleDeleteAllPages(const base::ListValue* args); |
| 35 | 43 |
| 36 // Delete selected list of page ids from the store. | 44 // Delete selected list of page ids from the store. |
| 37 void HandleDeleteSelectedPages(const base::ListValue* args); | 45 void HandleDeleteSelectedPages(const base::ListValue* args); |
| 38 | 46 |
| 39 // Load all information. | 47 // Load Request Queue info. |
| 40 void HandleGetOfflineInternalsInfo(const base::ListValue* args); | 48 void HandleGetRequestQueueInfo(const base::ListValue* args); |
| 49 | |
| 50 // Load Stored pages info. | |
| 51 void HandleGetStoredPagesInfo(const base::ListValue* args); | |
| 52 | |
| 53 // Callback for async GetAllPages calls. | |
| 54 void HandleStoredPagesCallback(const base::Value* callback_id, | |
| 55 const offline_pages::MultipleOfflinePageItemResult& pages); | |
| 56 | |
| 57 // Callback for async GetRequests calls. | |
| 58 void HandleRequestQueueCallback( | |
| 59 const base::Value* callback_id, | |
| 60 offline_pages::RequestQueue::GetRequestsResult result, | |
| 61 const std::vector<offline_pages::SavePageRequest>& requests); | |
| 62 | |
| 63 // Callback for DeletePage/ClearAll calls. | |
| 64 void HandleDeletedPagesCallback(const base::Value* callback_id, | |
| 65 const offline_pages::DeletePageResult result); | |
| 66 | |
| 67 // Turns a DeletePageResult enum into logical string. | |
| 68 std::string GetStringFromDeletePageResult( | |
| 69 offline_pages::DeletePageResult value); | |
| 70 | |
| 71 // Turns a SavePageRequest::Status into logical string. | |
| 72 std::string GetStringFromSavePageStatus( | |
| 73 offline_pages::SavePageRequest::Status status); | |
| 74 | |
| 75 // Offline page model to call methods on. | |
| 76 offline_pages::OfflinePageModel* offline_page_model_; | |
| 77 | |
| 78 // Request coordinator for background offline actions. | |
| 79 offline_pages::RequestCoordinator* request_coordinator_; | |
| 41 | 80 |
| 42 // Factory for creating references in callbacks. | 81 // Factory for creating references in callbacks. |
| 43 base::WeakPtrFactory<OfflineInternalsUIMessageHandler> weak_ptr_factory_; | 82 base::WeakPtrFactory<OfflineInternalsUIMessageHandler> weak_ptr_factory_; |
| 44 | 83 |
| 45 DISALLOW_COPY_AND_ASSIGN(OfflineInternalsUIMessageHandler); | 84 DISALLOW_COPY_AND_ASSIGN(OfflineInternalsUIMessageHandler); |
| 46 }; | 85 }; |
| 47 | 86 |
| 48 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() | 87 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() |
| 49 : weak_ptr_factory_(this) {} | 88 : weak_ptr_factory_(this) {} |
| 50 | 89 |
| 51 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} | 90 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} |
| 52 | 91 |
| 92 std::string OfflineInternalsUIMessageHandler::GetStringFromDeletePageResult( | |
| 93 offline_pages::DeletePageResult value) { | |
| 94 switch (value) { | |
| 95 case offline_pages::DeletePageResult::SUCCESS: | |
| 96 return "Success"; | |
| 97 case offline_pages::DeletePageResult::CANCELLED: | |
| 98 return "Cancelled"; | |
| 99 case offline_pages::DeletePageResult::STORE_FAILURE: | |
| 100 return "Store failure"; | |
| 101 case offline_pages::DeletePageResult::DEVICE_FAILURE: | |
| 102 return "Device failure"; | |
| 103 case offline_pages::DeletePageResult::NOT_FOUND: | |
| 104 return "Not found"; | |
| 105 case offline_pages::DeletePageResult::RESULT_COUNT: | |
| 106 return "Moooooooo"; | |
|
dewittj
2016/06/10 18:14:33
Probably should also NOTREACHED() in this branch h
chili
2016/06/10 20:21:03
Done.
| |
| 107 } | |
| 108 NOTREACHED(); | |
| 109 return "Unknown"; | |
| 110 } | |
| 111 | |
| 112 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus( | |
| 113 offline_pages::SavePageRequest::Status status) { | |
| 114 switch (status) { | |
| 115 case offline_pages::SavePageRequest::Status::kNotReady: | |
| 116 return "Not ready"; | |
| 117 case offline_pages::SavePageRequest::Status::kPending: | |
| 118 return "Pending"; | |
| 119 case offline_pages::SavePageRequest::Status::kStarted: | |
| 120 return "Started"; | |
| 121 case offline_pages::SavePageRequest::Status::kFailed: | |
| 122 return "Failed"; | |
| 123 case offline_pages::SavePageRequest::Status::kExpired: | |
| 124 return "Expired"; | |
| 125 } | |
| 126 NOTREACHED(); | |
| 127 return "Unknown"; | |
| 128 } | |
| 129 | |
| 53 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( | 130 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( |
| 54 const base::ListValue* args) { | 131 const base::ListValue* args) { |
| 55 const base::Value* callback_id; | 132 const base::Value* callback_id; |
| 56 args->Get(0, &callback_id); | 133 CHECK(args->Get(0, &callback_id)); |
|
dewittj
2016/06/10 18:14:33
right, |callback_id| is owned by |args| (per the d
chili
2016/06/10 20:21:03
Done.
| |
| 57 CallJavascriptFunction("cr.webUIResponse", | 134 |
| 58 *callback_id, | 135 // Pass back success because ClearAll doesn't return a status. |
| 59 base::FundamentalValue(true), | 136 offline_page_model_->ClearAll( |
| 60 base::StringValue("success")); | 137 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, |
| 138 weak_ptr_factory_.GetWeakPtr(), callback_id, | |
| 139 offline_pages::DeletePageResult::SUCCESS)); | |
| 61 } | 140 } |
| 62 | 141 |
| 63 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( | 142 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( |
| 64 const base::ListValue* args) { | 143 const base::ListValue* args) { |
| 65 const base::Value* callback_id; | 144 const base::Value* callback_id; |
| 66 args->Get(0, &callback_id); | 145 CHECK(args->Get(0, &callback_id)); |
| 67 CallJavascriptFunction("cr.webUIResponse", | 146 |
| 68 *callback_id, | 147 std::vector<int64_t> offline_ids; |
| 69 base::FundamentalValue(true), | 148 const base::ListValue* offline_ids_from_arg; |
| 70 base::StringValue("success")); | 149 args->GetList(1, &offline_ids_from_arg); |
| 150 | |
| 151 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { | |
| 152 std::string value; | |
| 153 offline_ids_from_arg->GetString(i, &value); | |
| 154 int64_t intValue; | |
| 155 base::StringToInt64(value, &intValue); | |
| 156 offline_ids.push_back(intValue); | |
| 157 } | |
| 158 | |
| 159 offline_page_model_->DeletePagesByOfflineId( | |
| 160 offline_ids, | |
| 161 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, | |
| 162 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 71 } | 163 } |
| 72 | 164 |
| 73 void OfflineInternalsUIMessageHandler::HandleGetOfflineInternalsInfo( | 165 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( |
| 166 const base::Value* callback_id, | |
| 167 offline_pages::DeletePageResult result) { | |
| 168 ResolveJavascriptCallback( | |
| 169 *callback_id, | |
| 170 base::StringValue(GetStringFromDeletePageResult(result))); | |
| 171 } | |
| 172 | |
| 173 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( | |
| 174 const base::Value* callback_id, | |
| 175 const offline_pages::MultipleOfflinePageItemResult& pages) { | |
| 176 base::ListValue results; | |
| 177 | |
| 178 for (const auto& page : pages) { | |
| 179 base::DictionaryValue* js_page_object = new base::DictionaryValue(); | |
| 180 results.Append(js_page_object); | |
| 181 js_page_object->SetString("onlineUrl", page.url.spec()); | |
| 182 js_page_object->SetString("namespace", page.client_id.name_space); | |
| 183 js_page_object->SetDouble("size", page.file_size); | |
| 184 js_page_object->SetString("id", std::to_string(page.offline_id)); | |
| 185 js_page_object->SetString("filePath", page.file_path.value()); | |
| 186 js_page_object->SetDouble("creationTime", page.creation_time.ToJsTime()); | |
| 187 js_page_object->SetDouble("lastAccessTime", | |
| 188 page.last_access_time.ToJsTime()); | |
| 189 js_page_object->SetInteger("accessCount", page.access_count); | |
| 190 } | |
| 191 ResolveJavascriptCallback(*callback_id, results); | |
| 192 } | |
| 193 | |
| 194 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback( | |
| 195 const base::Value* callback_id, | |
| 196 offline_pages::RequestQueue::GetRequestsResult result, | |
| 197 const std::vector<offline_pages::SavePageRequest>& requests) { | |
| 198 base::ListValue js_requests; | |
| 199 if (result == offline_pages::RequestQueue::GetRequestsResult::kSuccess) { | |
| 200 for (const auto& request : requests) { | |
| 201 base::DictionaryValue* js_request_object = new base::DictionaryValue(); | |
| 202 js_requests.Append(js_request_object); | |
| 203 js_request_object->SetString("onlineUrl", request.url().spec()); | |
| 204 js_request_object->SetDouble("creationTime", | |
| 205 request.creation_time().ToJsTime()); | |
| 206 js_request_object->SetString( | |
| 207 "status", | |
| 208 GetStringFromSavePageStatus(request.GetStatus(base::Time::Now()))); | |
| 209 js_request_object->SetString("namespace", request.client_id().name_space); | |
| 210 js_request_object->SetDouble("lastAttempt", | |
| 211 request.last_attempt_time().ToJsTime()); | |
| 212 js_request_object->SetString("id", std::to_string(request.request_id())); | |
| 213 } | |
| 214 } | |
| 215 ResolveJavascriptCallback(*callback_id, js_requests); | |
| 216 } | |
| 217 | |
| 218 void OfflineInternalsUIMessageHandler::HandleGetRequestQueueInfo( | |
| 74 const base::ListValue* args) { | 219 const base::ListValue* args) { |
| 75 AllowJavascript(); | 220 AllowJavascript(); |
| 76 const base::Value* callback_id; | 221 const base::Value* callback_id; |
| 77 args->Get(0, &callback_id); | 222 CHECK(args->Get(0, &callback_id)); |
| 78 base::DictionaryValue results; | |
| 79 results.Set("AllPages", new base::ListValue()); | |
| 80 results.Set("Queue", new base::ListValue()); | |
| 81 | 223 |
| 82 CallJavascriptFunction("cr.webUIResponse", | 224 if (request_coordinator_) { |
| 83 *callback_id, | 225 request_coordinator_->queue()->GetRequests( |
| 84 base::FundamentalValue(true), | 226 base::Bind( |
| 85 results); | 227 &OfflineInternalsUIMessageHandler::HandleRequestQueueCallback, |
| 228 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 229 } else { | |
| 230 base::ListValue results; | |
| 231 ResolveJavascriptCallback(*callback_id, results); | |
|
dewittj
2016/06/10 18:14:33
Possibly RejectJavascriptCallback?
chili
2016/06/10 20:21:03
I think it could go either way. Perhaps there sho
| |
| 232 } | |
| 233 } | |
| 234 | |
| 235 void OfflineInternalsUIMessageHandler::HandleGetStoredPagesInfo( | |
| 236 const base::ListValue* args) { | |
| 237 AllowJavascript(); | |
| 238 const base::Value* callback_id; | |
| 239 CHECK(args->Get(0, &callback_id)); | |
| 240 | |
| 241 if (offline_page_model_) { | |
| 242 offline_page_model_->GetAllPages( | |
| 243 base::Bind(&OfflineInternalsUIMessageHandler::HandleStoredPagesCallback, | |
| 244 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
| 245 } else { | |
| 246 base::ListValue results; | |
| 247 ResolveJavascriptCallback(*callback_id, results); | |
| 248 } | |
| 86 } | 249 } |
| 87 | 250 |
| 88 void OfflineInternalsUIMessageHandler::RegisterMessages() { | 251 void OfflineInternalsUIMessageHandler::RegisterMessages() { |
| 89 web_ui()->RegisterMessageCallback( | 252 web_ui()->RegisterMessageCallback( |
| 90 "deleteAllPages", | 253 "deleteAllPages", |
| 91 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, | 254 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, |
| 92 weak_ptr_factory_.GetWeakPtr())); | 255 weak_ptr_factory_.GetWeakPtr())); |
| 93 web_ui()->RegisterMessageCallback( | 256 web_ui()->RegisterMessageCallback( |
| 94 "deleteSelectedPages", | 257 "deleteSelectedPages", |
| 95 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, | 258 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, |
| 96 weak_ptr_factory_.GetWeakPtr())); | 259 weak_ptr_factory_.GetWeakPtr())); |
| 97 web_ui()->RegisterMessageCallback( | 260 web_ui()->RegisterMessageCallback( |
| 98 "getOfflineInternalsInfo", | 261 "getRequestQueueInfo", |
| 99 base::Bind( | 262 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueueInfo, |
| 100 &OfflineInternalsUIMessageHandler::HandleGetOfflineInternalsInfo, | 263 weak_ptr_factory_.GetWeakPtr())); |
| 101 weak_ptr_factory_.GetWeakPtr())); | 264 web_ui()->RegisterMessageCallback( |
| 265 "getStoredPagesInfo", | |
| 266 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPagesInfo, | |
| 267 weak_ptr_factory_.GetWeakPtr())); | |
| 268 | |
| 269 // Get the offline page model associated with this web ui. | |
| 270 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 271 offline_page_model_ = | |
| 272 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | |
| 273 request_coordinator_ = | |
| 274 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | |
| 102 } | 275 } |
| 103 | 276 |
| 104 } // namespace | 277 } // namespace |
| 105 | 278 |
| 106 OfflineInternalsUI::OfflineInternalsUI(content::WebUI* web_ui) | 279 OfflineInternalsUI::OfflineInternalsUI(content::WebUI* web_ui) |
| 107 : content::WebUIController(web_ui) { | 280 : content::WebUIController(web_ui) { |
| 108 // chrome://offline-internals source. | 281 // chrome://offline-internals source. |
| 109 content::WebUIDataSource* html_source = | 282 content::WebUIDataSource* html_source = |
| 110 content::WebUIDataSource::Create(chrome::kChromeUIOfflineInternalsHost); | 283 content::WebUIDataSource::Create(chrome::kChromeUIOfflineInternalsHost); |
| 111 | 284 |
| 112 // Required resources. | 285 // Required resources. |
| 113 html_source->SetJsonPath("strings.js"); | 286 html_source->SetJsonPath("strings.js"); |
| 114 html_source->AddResourcePath("offline_internals.css", | 287 html_source->AddResourcePath("offline_internals.css", |
| 115 IDR_OFFLINE_INTERNALS_CSS); | 288 IDR_OFFLINE_INTERNALS_CSS); |
| 116 html_source->AddResourcePath("offline_internals.js", | 289 html_source->AddResourcePath("offline_internals.js", |
| 117 IDR_OFFLINE_INTERNALS_JS); | 290 IDR_OFFLINE_INTERNALS_JS); |
| 118 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); | 291 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); |
| 119 | 292 |
| 120 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 293 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
| 121 | 294 |
| 122 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); | 295 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); |
| 123 } | 296 } |
| 124 | 297 |
| 125 OfflineInternalsUI::~OfflineInternalsUI() {} | 298 OfflineInternalsUI::~OfflineInternalsUI() {} |
| OLD | NEW |