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" |
12 #include "base/values.h" | 14 #include "base/values.h" |
15 #include "chrome/browser/android/offline_pages/offline_page_model_factory.h" | |
16 #include "chrome/browser/android/offline_pages/request_coordinator_factory.h" | |
13 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/common/url_constants.h" | 18 #include "chrome/common/url_constants.h" |
19 #include "components/offline_pages/background/request_coordinator.h" | |
20 #include "components/offline_pages/background/save_page_request.h" | |
21 #include "components/offline_pages/offline_page_model.h" | |
15 #include "content/public/browser/web_ui.h" | 22 #include "content/public/browser/web_ui.h" |
16 #include "content/public/browser/web_ui_controller.h" | 23 #include "content/public/browser/web_ui_controller.h" |
17 #include "content/public/browser/web_ui_data_source.h" | 24 #include "content/public/browser/web_ui_data_source.h" |
18 #include "content/public/browser/web_ui_message_handler.h" | 25 #include "content/public/browser/web_ui_message_handler.h" |
19 #include "grit/browser_resources.h" | 26 #include "grit/browser_resources.h" |
20 | 27 |
21 namespace { | 28 namespace { |
dpapad
2016/06/09 23:36:30
Nit: Should this be in offline_pages namespace? Pe
| |
22 | 29 |
23 // Class acting as a controller of the chrome://offline-internals WebUI. | 30 // Class acting as a controller of the chrome://offline-internals WebUI. |
24 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { | 31 class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { |
25 public: | 32 public: |
26 OfflineInternalsUIMessageHandler(); | 33 OfflineInternalsUIMessageHandler(); |
27 ~OfflineInternalsUIMessageHandler() override; | 34 ~OfflineInternalsUIMessageHandler() override; |
28 | 35 |
29 // WebUIMessageHandler implementation. | 36 // WebUIMessageHandler implementation. |
30 void RegisterMessages() override; | 37 void RegisterMessages() override; |
31 | 38 |
32 private: | 39 private: |
33 // Deletes all the pages in the store. | 40 // Deletes all the pages in the store. |
34 void HandleDeleteAllPages(const base::ListValue* args); | 41 void HandleDeleteAllPages(const base::ListValue* args); |
35 | 42 |
36 // Delete selected list of page ids from the store. | 43 // Delete selected list of page ids from the store. |
37 void HandleDeleteSelectedPages(const base::ListValue* args); | 44 void HandleDeleteSelectedPages(const base::ListValue* args); |
38 | 45 |
39 // Load all information. | 46 // Load Request Queue info. |
40 void HandleGetOfflineInternalsInfo(const base::ListValue* args); | 47 void HandleGetRequestQueue(const base::ListValue* args); |
Dan Beam
2016/06/09 23:35:02
typically these are all just named HandleExactName
chili
2016/06/10 01:04:52
Done.
| |
48 | |
49 // Load Stored pages info. | |
50 void HandleGetStoredPages(const base::ListValue* args); | |
51 | |
52 // Callback for async GetAllPages calls. | |
53 void HandleStoredPagesCallback(const base::Value* callback_id, | |
54 const offline_pages::MultipleOfflinePageItemResult& pages); | |
55 | |
56 // Callback for async GetRequests calls. | |
57 void HandleRequestQueueCallback( | |
58 const base::Value* callback_id, | |
59 offline_pages::RequestQueue::GetRequestsResult result, | |
60 const std::vector<offline_pages::SavePageRequest>& requests); | |
61 | |
62 // Callback for DeletePage/ClearAll calls. | |
63 void HandleDeletedPagesCallback(const base::Value* callback_id, | |
64 const offline_pages::DeletePageResult result); | |
65 | |
Dan Beam
2016/06/09 23:35:02
doc comment
chili
2016/06/10 01:04:52
Done.
| |
66 std::string GetStringFromDeletePageResult( | |
67 offline_pages::DeletePageResult value); | |
68 | |
Dan Beam
2016/06/09 23:35:01
doc comment
chili
2016/06/10 01:04:52
Done.
| |
69 std::string GetStringFromSavePageStatus( | |
70 offline_pages::SavePageRequest::Status status); | |
71 | |
72 // Offline page model to call methods on. | |
73 offline_pages::OfflinePageModel* offline_page_model_; | |
Dan Beam
2016/06/09 23:35:02
where is this initialized originally?
Dan Beam
2016/06/09 23:35:02
nit: \n
chili
2016/06/10 01:04:52
initialized in RegisterMessages, because that's wh
chili
2016/06/10 01:04:52
Done.
| |
74 // Request coordinator for background offline actions. | |
75 offline_pages::RequestCoordinator* request_coordinator_; | |
41 | 76 |
42 // Factory for creating references in callbacks. | 77 // Factory for creating references in callbacks. |
43 base::WeakPtrFactory<OfflineInternalsUIMessageHandler> weak_ptr_factory_; | 78 base::WeakPtrFactory<OfflineInternalsUIMessageHandler> weak_ptr_factory_; |
44 | 79 |
45 DISALLOW_COPY_AND_ASSIGN(OfflineInternalsUIMessageHandler); | 80 DISALLOW_COPY_AND_ASSIGN(OfflineInternalsUIMessageHandler); |
46 }; | 81 }; |
47 | 82 |
48 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() | 83 OfflineInternalsUIMessageHandler::OfflineInternalsUIMessageHandler() |
49 : weak_ptr_factory_(this) {} | 84 : weak_ptr_factory_(this) {} |
50 | 85 |
51 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} | 86 OfflineInternalsUIMessageHandler::~OfflineInternalsUIMessageHandler() {} |
52 | 87 |
88 std::string OfflineInternalsUIMessageHandler::GetStringFromDeletePageResult( | |
89 offline_pages::DeletePageResult value) { | |
Dan Beam
2016/06/09 23:35:01
nit: I think this implementation would be more fut
chili
2016/06/10 01:04:52
Done.
| |
90 const std::string kDeletePageResultToString[] = { | |
91 "Not ready", "Pending", "Started", "Failed", "Expired"}; | |
92 int int_value = static_cast<int>(value); | |
93 if (int_value >= 5) | |
94 return "Unknown"; | |
95 else | |
Dan Beam
2016/06/09 23:35:02
no else after return
chili
2016/06/10 01:04:52
Done.
| |
96 return kDeletePageResultToString[int_value]; | |
97 } | |
98 | |
99 std::string OfflineInternalsUIMessageHandler::GetStringFromSavePageStatus( | |
100 offline_pages::SavePageRequest::Status status) { | |
101 const std::string kRequestStatusToString[] = { | |
dpapad
2016/06/09 23:36:30
This constant array is identical with line 91. Sho
chili
2016/06/10 01:04:52
when i copied/pasted due to a previous comment, co
| |
102 "Not ready", "Pending", "Started", "Failed", "Expired"}; | |
103 int int_value = static_cast<int>(status); | |
104 if (int_value >= 5) | |
105 return "Unknown"; | |
106 else | |
Dan Beam
2016/06/09 23:35:02
no else after return
chili
2016/06/10 01:04:52
Done.
| |
107 return kRequestStatusToString[int_value]; | |
108 } | |
109 | |
53 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( | 110 void OfflineInternalsUIMessageHandler::HandleDeleteAllPages( |
54 const base::ListValue* args) { | 111 const base::ListValue* args) { |
55 const base::Value* callback_id; | 112 const base::Value* callback_id; |
Dan Beam
2016/06/09 23:35:02
what's the ownership of |callback_id|? is it safe
chili
2016/06/10 01:04:52
I'm a bit rusty on C++ here. this won't compile w
dpapad
2016/06/10 01:25:09
You can follow this example
https://cs.chromium.or
| |
56 args->Get(0, &callback_id); | 113 args->Get(0, &callback_id); |
dpapad
2016/06/09 23:36:30
CHECK(args->Get(0, &callback_id));
chili
2016/06/10 01:04:52
Done.
| |
57 CallJavascriptFunction("cr.webUIResponse", | 114 |
58 *callback_id, | 115 // Pass back success because ClearAll doesn't return a status. |
59 base::FundamentalValue(true), | 116 offline_page_model_->ClearAll( |
60 base::StringValue("success")); | 117 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, |
118 weak_ptr_factory_.GetWeakPtr(), callback_id, | |
119 offline_pages::DeletePageResult::SUCCESS)); | |
61 } | 120 } |
62 | 121 |
63 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( | 122 void OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages( |
64 const base::ListValue* args) { | 123 const base::ListValue* args) { |
65 const base::Value* callback_id; | 124 const base::Value* callback_id; |
66 args->Get(0, &callback_id); | 125 args->Get(0, &callback_id); |
67 CallJavascriptFunction("cr.webUIResponse", | 126 |
68 *callback_id, | 127 std::vector<int64_t> offline_ids; |
69 base::FundamentalValue(true), | 128 const base::ListValue* offline_ids_from_arg; |
70 base::StringValue("success")); | 129 args->GetList(1, &offline_ids_from_arg); |
130 | |
131 for (size_t i = 0; i < offline_ids_from_arg->GetSize(); i++) { | |
132 std::string value; | |
133 offline_ids_from_arg->GetString(i, &value); | |
134 offline_ids.push_back(atoll(value.c_str())); | |
Dan Beam
2016/06/09 23:35:01
is this different/better than base::StringToInt64(
chili
2016/06/10 01:04:52
ooh, didn't know about StringToInt64. Using that n
| |
135 } | |
136 | |
137 offline_page_model_->DeletePagesByOfflineId( | |
138 offline_ids, | |
139 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback, | |
140 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
71 } | 141 } |
72 | 142 |
73 void OfflineInternalsUIMessageHandler::HandleGetOfflineInternalsInfo( | 143 void OfflineInternalsUIMessageHandler::HandleDeletedPagesCallback( |
144 const base::Value* callback_id, | |
145 offline_pages::DeletePageResult result) { | |
146 | |
dpapad
2016/06/09 23:36:30
Nit: No need for \n here.
chili
2016/06/10 01:04:52
Done.
| |
147 ResolveJavascriptCallback( | |
148 *callback_id, | |
149 base::StringValue(GetStringFromDeletePageResult(result))); | |
150 } | |
151 | |
152 void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( | |
153 const base::Value* callback_id, | |
154 const offline_pages::MultipleOfflinePageItemResult& pages) { | |
155 base::ListValue results; | |
156 | |
157 for (const auto& page : pages) { | |
158 base::DictionaryValue* js_page_object = new base::DictionaryValue(); | |
159 results.Append(js_page_object); | |
160 js_page_object->SetString("onlineUrl", page.url.spec()); | |
161 js_page_object->SetString("namespace", page.client_id.name_space); | |
162 js_page_object->SetDouble("size", page.file_size); | |
163 js_page_object->SetString("id", std::to_string(page.offline_id)); | |
164 js_page_object->SetString("filePath", page.file_path.value()); | |
165 js_page_object->SetDouble("creationTime", page.creation_time.ToJsTime()); | |
166 js_page_object->SetDouble("lastAccessTime", | |
167 page.last_access_time.ToJsTime()); | |
168 js_page_object->SetInteger("accessCount", page.access_count); | |
169 } | |
170 ResolveJavascriptCallback(*callback_id, results); | |
171 } | |
172 | |
173 void OfflineInternalsUIMessageHandler::HandleRequestQueueCallback( | |
174 const base::Value* callback_id, | |
175 offline_pages::RequestQueue::GetRequestsResult result, | |
176 const std::vector<offline_pages::SavePageRequest>& requests) { | |
177 base::ListValue js_requests; | |
178 if (result == offline_pages::RequestQueue::GetRequestsResult::kSuccess) { | |
179 for (const auto& request : requests) { | |
180 base::DictionaryValue* js_request_object = new base::DictionaryValue(); | |
181 js_requests.Append(js_request_object); | |
182 js_request_object->SetString("onlineUrl", request.url().spec()); | |
183 js_request_object->SetDouble("creationTime", | |
184 request.creation_time().ToJsTime()); | |
185 js_request_object->SetString( | |
186 "status", | |
187 GetStringFromSavePageStatus(request.GetStatus(base::Time::Now()))); | |
188 js_request_object->SetString("namespace", request.client_id().name_space); | |
189 js_request_object->SetDouble("lastAttempt", | |
190 request.last_attempt_time().ToJsTime()); | |
191 js_request_object->SetString("id", std::to_string(request.request_id())); | |
192 } | |
193 } | |
194 ResolveJavascriptCallback(*callback_id, js_requests); | |
195 } | |
196 | |
197 void OfflineInternalsUIMessageHandler::HandleGetRequestQueue( | |
74 const base::ListValue* args) { | 198 const base::ListValue* args) { |
75 AllowJavascript(); | 199 AllowJavascript(); |
76 const base::Value* callback_id; | 200 const base::Value* callback_id; |
77 args->Get(0, &callback_id); | 201 args->Get(0, &callback_id); |
78 base::DictionaryValue results; | 202 base::ListValue results; |
79 results.Set("AllPages", new base::ListValue()); | |
80 results.Set("Queue", new base::ListValue()); | |
81 | 203 |
82 CallJavascriptFunction("cr.webUIResponse", | 204 if (request_coordinator_ != nullptr) |
Dan Beam
2016/06/09 23:35:01
nit: curlies around 2+ line ifs
Dan Beam
2016/06/09 23:35:02
nit: if (request_coordinator_) seems equivalent bu
dpapad
2016/06/09 23:36:30
The Promise that JS holds is never fulfilled (reso
chili
2016/06/10 01:04:52
Done.
chili
2016/06/10 01:04:52
Done.
chili
2016/06/10 01:04:52
Done.
| |
83 *callback_id, | 205 request_coordinator_->queue()->GetRequests( |
dpapad
2016/06/09 23:36:30
Nit: Perhaps more readable to use curly braces for
chili
2016/06/10 01:04:52
Done.
| |
84 base::FundamentalValue(true), | 206 base::Bind( |
85 results); | 207 &OfflineInternalsUIMessageHandler::HandleRequestQueueCallback, |
208 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
209 } | |
210 | |
211 void OfflineInternalsUIMessageHandler::HandleGetStoredPages( | |
212 const base::ListValue* args) { | |
213 AllowJavascript(); | |
214 const base::Value* callback_id; | |
215 args->Get(0, &callback_id); | |
216 base::ListValue results; | |
217 | |
218 if (offline_page_model_ != nullptr) | |
219 offline_page_model_->GetAllPages( | |
220 base::Bind(&OfflineInternalsUIMessageHandler::HandleStoredPagesCallback, | |
221 weak_ptr_factory_.GetWeakPtr(), callback_id)); | |
dpapad
2016/06/09 23:36:30
Same here, what happens to the Promise?
chili
2016/06/10 01:04:52
Done.
| |
86 } | 222 } |
87 | 223 |
88 void OfflineInternalsUIMessageHandler::RegisterMessages() { | 224 void OfflineInternalsUIMessageHandler::RegisterMessages() { |
89 web_ui()->RegisterMessageCallback( | 225 web_ui()->RegisterMessageCallback( |
90 "deleteAllPages", | 226 "deleteAllPages", |
91 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, | 227 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteAllPages, |
92 weak_ptr_factory_.GetWeakPtr())); | 228 weak_ptr_factory_.GetWeakPtr())); |
93 web_ui()->RegisterMessageCallback( | 229 web_ui()->RegisterMessageCallback( |
94 "deleteSelectedPages", | 230 "deleteSelectedPages", |
95 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, | 231 base::Bind(&OfflineInternalsUIMessageHandler::HandleDeleteSelectedPages, |
96 weak_ptr_factory_.GetWeakPtr())); | 232 weak_ptr_factory_.GetWeakPtr())); |
97 web_ui()->RegisterMessageCallback( | 233 web_ui()->RegisterMessageCallback( |
98 "getOfflineInternalsInfo", | 234 "getRequestQueueInfo", |
Dan Beam
2016/06/09 23:35:02
nit: could this be just "getRequestQueue"?
chili
2016/06/10 01:04:52
Renamed the method, so keeping as is
| |
99 base::Bind( | 235 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetRequestQueue, |
100 &OfflineInternalsUIMessageHandler::HandleGetOfflineInternalsInfo, | 236 weak_ptr_factory_.GetWeakPtr())); |
101 weak_ptr_factory_.GetWeakPtr())); | 237 web_ui()->RegisterMessageCallback( |
238 "getStoredPagesInfo", | |
Dan Beam
2016/06/09 23:35:02
could this just be "getStoredPages"?
chili
2016/06/10 01:04:52
Acknowledged.
| |
239 base::Bind(&OfflineInternalsUIMessageHandler::HandleGetStoredPages, | |
240 weak_ptr_factory_.GetWeakPtr())); | |
241 | |
242 // Get the offline page model associated with this web ui. | |
243 Profile* profile = Profile::FromWebUI(web_ui()); | |
244 offline_page_model_ = | |
245 offline_pages::OfflinePageModelFactory::GetForBrowserContext(profile); | |
246 request_coordinator_ = | |
247 offline_pages::RequestCoordinatorFactory::GetForBrowserContext(profile); | |
102 } | 248 } |
103 | 249 |
104 } // namespace | 250 } // namespace |
105 | 251 |
106 OfflineInternalsUI::OfflineInternalsUI(content::WebUI* web_ui) | 252 OfflineInternalsUI::OfflineInternalsUI(content::WebUI* web_ui) |
107 : content::WebUIController(web_ui) { | 253 : content::WebUIController(web_ui) { |
108 // chrome://offline-internals source. | 254 // chrome://offline-internals source. |
109 content::WebUIDataSource* html_source = | 255 content::WebUIDataSource* html_source = |
110 content::WebUIDataSource::Create(chrome::kChromeUIOfflineInternalsHost); | 256 content::WebUIDataSource::Create(chrome::kChromeUIOfflineInternalsHost); |
111 | 257 |
112 // Required resources. | 258 // Required resources. |
113 html_source->SetJsonPath("strings.js"); | 259 html_source->SetJsonPath("strings.js"); |
114 html_source->AddResourcePath("offline_internals.css", | 260 html_source->AddResourcePath("offline_internals.css", |
115 IDR_OFFLINE_INTERNALS_CSS); | 261 IDR_OFFLINE_INTERNALS_CSS); |
116 html_source->AddResourcePath("offline_internals.js", | 262 html_source->AddResourcePath("offline_internals.js", |
117 IDR_OFFLINE_INTERNALS_JS); | 263 IDR_OFFLINE_INTERNALS_JS); |
118 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); | 264 html_source->SetDefaultResource(IDR_OFFLINE_INTERNALS_HTML); |
119 | 265 |
120 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); | 266 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), html_source); |
121 | 267 |
122 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); | 268 web_ui->AddMessageHandler(new OfflineInternalsUIMessageHandler()); |
123 } | 269 } |
124 | 270 |
125 OfflineInternalsUI::~OfflineInternalsUI() {} | 271 OfflineInternalsUI::~OfflineInternalsUI() {} |
OLD | NEW |