Commit de0afdc2 authored by chili's avatar chili Committed by Commit bot

[Offline Pages] Add network status, save request textbox, and offline url link...

[Offline Pages] Add network status, save request textbox, and offline url link to offline internals page

BUG=629880,629879
CQ_INCLUDE_TRYBOTS=master.tryserver.chromium.linux:closure_compilation

Review-Url: https://codereview.chromium.org/2172443004
Cr-Commit-Position: refs/heads/master@{#407332}
parent c6485776
...@@ -23,10 +23,14 @@ th { ...@@ -23,10 +23,14 @@ th {
padding: 5px; padding: 5px;
} }
table {
margin-bottom: 5px;
}
ul { ul {
border: 1px solid; border: 1px solid;
list-style-type: none; list-style-type: none;
margin: 0; margin-top: 5px;
max-height: 300px; max-height: 300px;
overflow: auto; overflow: auto;
padding: 5px; padding: 5px;
...@@ -42,3 +46,7 @@ ul li { ...@@ -42,3 +46,7 @@ ul li {
li:nth-child(2n) { li:nth-child(2n) {
background-color: lavender; background-color: lavender;
} }
#current-status {
font-size: 15px;
}
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
<body> <body>
<h1>Offline Internals</h1> <h1>Offline Internals</h1>
<div> <div>
<div id="current-status"></div> <span id="current-status"></span>
<button id="refresh">Refresh page</button> <button id="refresh">Refresh page</button>
<button id="download">Dump</button> <button id="download">Dump</button>
</div> </div>
...@@ -68,5 +68,8 @@ ...@@ -68,5 +68,8 @@
</thead> </thead>
<tbody id="request-queue"> </tbody> <tbody id="request-queue"> </tbody>
</table> </table>
<input id="url" type="url">
<button id="add-to-queue">Load in background</button>
<div id="save-url-state"></div>
</body> </body>
</html> </html>
...@@ -37,7 +37,10 @@ cr.define('offlineInternals', function() { ...@@ -37,7 +37,10 @@ cr.define('offlineInternals', function() {
row.appendChild(checkboxCell); row.appendChild(checkboxCell);
var cell = document.createElement('td'); var cell = document.createElement('td');
cell.textContent = pages[i].onlineUrl; var link = document.createElement('a');
link.setAttribute('href', pages[i].filePath);
link.textContent = pages[i].onlineUrl;
cell.appendChild(link);
row.appendChild(cell); row.appendChild(cell);
cell = document.createElement('td'); cell = document.createElement('td');
...@@ -106,6 +109,9 @@ cr.define('offlineInternals', function() { ...@@ -106,6 +109,9 @@ cr.define('offlineInternals', function() {
function refreshAll() { function refreshAll() {
browserProxy_.getStoredPages().then(fillStoredPages); browserProxy_.getStoredPages().then(fillStoredPages);
browserProxy_.getRequestQueue().then(fillRequestQueue); browserProxy_.getRequestQueue().then(fillRequestQueue);
browserProxy_.getNetworkStatus().then(function(networkStatus) {
$('current-status').textContent = networkStatus;
});
refreshLog(); refreshLog();
} }
...@@ -173,19 +179,47 @@ cr.define('offlineInternals', function() { ...@@ -173,19 +179,47 @@ cr.define('offlineInternals', function() {
} }
function initialize() { function initialize() {
/**
* @param {!boolean} enabled Whether to enable Logging.
*/
function togglePageModelLog(enabled) {
browserProxy_.setRecordPageModel(enabled);
$('model-status').textContent = enabled ? 'On' : 'Off';
}
/**
* @param {!boolean} enabled Whether to enable Logging.
*/
function toggleRequestQueueLog(enabled) {
browserProxy_.setRecordRequestQueue(enabled);
$('request-status').textContent = enabled ? 'On' : 'Off';
}
$('clear-all').onclick = deleteAllPages; $('clear-all').onclick = deleteAllPages;
$('clear-selected').onclick = deleteSelectedPages; $('clear-selected').onclick = deleteSelectedPages;
$('refresh').onclick = refreshAll; $('refresh').onclick = refreshAll;
$('download').onclick = download; $('download').onclick = download;
$('log-model-on').onclick = $('log-model-on').onclick = togglePageModelLog.bind(this, true);
browserProxy_.setRecordPageModel.bind(browserProxy_, true); $('log-model-off').onclick = togglePageModelLog.bind(this, false);
$('log-model-off').onclick = $('log-request-on').onclick = toggleRequestQueueLog.bind(this, true);
browserProxy_.setRecordPageModel.bind(browserProxy_, false); $('log-request-off').onclick = toggleRequestQueueLog.bind(this, false);
$('log-request-on').onclick =
browserProxy_.setRecordRequestQueue.bind(browserProxy_, true);
$('log-request-off').onclick =
browserProxy_.setRecordRequestQueue.bind(browserProxy_, false);
$('refresh-logs').onclick = refreshLog; $('refresh-logs').onclick = refreshLog;
$('add-to-queue').onclick = function() {
var saveUrl = $('url').value;
browserProxy_.addToRequestQueue(saveUrl)
.then(function(state) {
if (state) {
$('save-url-state').textContent =
saveUrl + ' has been added to queue.';
$('url').value = '';
browserProxy_.getRequestQueue().then(fillRequestQueue);
} else {
$('save-url-state').textContent =
saveUrl + ' failed to be added to queue.';
}
});
};
refreshAll(); refreshAll();
} }
......
...@@ -95,6 +95,21 @@ cr.define('offlineInternals', function() { ...@@ -95,6 +95,21 @@ cr.define('offlineInternals', function() {
* is retrieved. * is retrieved.
*/ */
getLoggingState: function() {}, getLoggingState: function() {},
/**
* Adds the given url to the background loader queue.
* @param {string} url Url of the page to load later.
* @return {!Promise<boolean>} A promise firing after added to queue.
* Promise will return true if url has been successfully added.
*/
addToRequestQueue: function(url) {},
/**
* Gets the current network status in string form.
* @return {!Promise<string>} A promise firing when the network status
* is retrieved.
*/
getNetworkStatus: function() {},
}; };
/** /**
...@@ -143,7 +158,17 @@ cr.define('offlineInternals', function() { ...@@ -143,7 +158,17 @@ cr.define('offlineInternals', function() {
/** @override */ /** @override */
getLoggingState: function() { getLoggingState: function() {
return cr.sendWithPromise('getLoggingState'); return cr.sendWithPromise('getLoggingState');
} },
/** @override */
addToRequestQueue: function(url) {
return cr.sendWithPromise('addToRequestQueue', url);
},
/** @override */
getNetworkStatus: function() {
return cr.sendWithPromise('getNetworkStatus');
},
}; };
return { return {
......
...@@ -19,16 +19,19 @@ ...@@ -19,16 +19,19 @@
#include "chrome/common/url_constants.h" #include "chrome/common/url_constants.h"
#include "components/offline_pages/background/request_coordinator.h" #include "components/offline_pages/background/request_coordinator.h"
#include "components/offline_pages/background/save_page_request.h" #include "components/offline_pages/background/save_page_request.h"
#include "components/offline_pages/client_namespace_constants.h"
#include "components/offline_pages/offline_page_model.h" #include "components/offline_pages/offline_page_model.h"
#include "content/public/browser/web_ui.h" #include "content/public/browser/web_ui.h"
#include "content/public/browser/web_ui_controller.h" #include "content/public/browser/web_ui_controller.h"
#include "content/public/browser/web_ui_data_source.h" #include "content/public/browser/web_ui_data_source.h"
#include "content/public/browser/web_ui_message_handler.h" #include "content/public/browser/web_ui_message_handler.h"
#include "grit/browser_resources.h" #include "grit/browser_resources.h"
#include "net/base/network_change_notifier.h"
namespace { namespace {
// Class acting as a controller of the chrome://offline-internals WebUI. // Class acting as a controller of the chrome://offline-internals WebUI.
// TODO(chili): Should split this file and move to webui/offline_internals/
class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler {
public: public:
OfflineInternalsUIMessageHandler(); OfflineInternalsUIMessageHandler();
...@@ -62,6 +65,12 @@ class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler { ...@@ -62,6 +65,12 @@ class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler {
// Load whether logs are being recorded. // Load whether logs are being recorded.
void HandleGetLoggingState(const base::ListValue* args); void HandleGetLoggingState(const base::ListValue* args);
// Adds a url to the background loader queue.
void HandleAddToRequestQueue(const base::ListValue* args);
// Load whether device is currently offline.
void HandleGetNetworkStatus(const base::ListValue* args);
// Callback for async GetAllPages calls. // Callback for async GetAllPages calls.
void HandleStoredPagesCallback( void HandleStoredPagesCallback(
std::string callback_id, std::string callback_id,
...@@ -197,7 +206,7 @@ void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback( ...@@ -197,7 +206,7 @@ void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback(
offline_page->SetString("namespace", page.client_id.name_space); offline_page->SetString("namespace", page.client_id.name_space);
offline_page->SetDouble("size", page.file_size); offline_page->SetDouble("size", page.file_size);
offline_page->SetString("id", std::to_string(page.offline_id)); offline_page->SetString("id", std::to_string(page.offline_id));
offline_page->SetString("filePath", page.file_path.value()); offline_page->SetString("filePath", page.GetOfflineURL().spec());
offline_page->SetDouble("creationTime", page.creation_time.ToJsTime()); offline_page->SetDouble("creationTime", page.creation_time.ToJsTime());
offline_page->SetDouble("lastAccessTime", offline_page->SetDouble("lastAccessTime",
page.last_access_time.ToJsTime()); page.last_access_time.ToJsTime());
...@@ -270,6 +279,17 @@ void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel( ...@@ -270,6 +279,17 @@ void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel(
offline_page_model_->GetLogger()->SetIsLogging(should_record); offline_page_model_->GetLogger()->SetIsLogging(should_record);
} }
void OfflineInternalsUIMessageHandler::HandleGetNetworkStatus(
const base::ListValue* args) {
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
ResolveJavascriptCallback(
*callback_id,
base::StringValue(
net::NetworkChangeNotifier::IsOffline() ? "Offline" : "Online"));
}
void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue( void OfflineInternalsUIMessageHandler::HandleSetRecordRequestQueue(
const base::ListValue* args) { const base::ListValue* args) {
bool should_record; bool should_record;
...@@ -308,6 +328,27 @@ void OfflineInternalsUIMessageHandler::HandleGetEventLogs( ...@@ -308,6 +328,27 @@ void OfflineInternalsUIMessageHandler::HandleGetEventLogs(
ResolveJavascriptCallback(*callback_id, result); ResolveJavascriptCallback(*callback_id, result);
} }
void OfflineInternalsUIMessageHandler::HandleAddToRequestQueue(
const base::ListValue* args) {
const base::Value* callback_id;
CHECK(args->Get(0, &callback_id));
std::string url;
CHECK(args->GetString(1, &url));
std::ostringstream id_stream;
id_stream << std::rand();
ResolveJavascriptCallback(
*callback_id,
base::FundamentalValue(
request_coordinator_->SavePageLater(
GURL(url),
offline_pages::ClientId(offline_pages::kAsyncNamespace,
id_stream.str()),
true)));
}
void OfflineInternalsUIMessageHandler::RegisterMessages() { void OfflineInternalsUIMessageHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback( web_ui()->RegisterMessageCallback(
"deleteAllPages", "deleteAllPages",
...@@ -341,6 +382,14 @@ void OfflineInternalsUIMessageHandler::RegisterMessages() { ...@@ -341,6 +382,14 @@ void OfflineInternalsUIMessageHandler::RegisterMessages() {
"getLoggingState", "getLoggingState",
base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState, base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"addToRequestQueue",
base::Bind(&OfflineInternalsUIMessageHandler::HandleAddToRequestQueue,
weak_ptr_factory_.GetWeakPtr()));
web_ui()->RegisterMessageCallback(
"getNetworkStatus",
base::Bind(&OfflineInternalsUIMessageHandler::HandleGetNetworkStatus,
weak_ptr_factory_.GetWeakPtr()));
// Get the offline page model associated with this web ui. // Get the offline page model associated with this web ui.
Profile* profile = Profile::FromWebUI(web_ui()); Profile* profile = Profile::FromWebUI(web_ui());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment