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 {
padding: 5px;
}
table {
margin-bottom: 5px;
}
ul {
border: 1px solid;
list-style-type: none;
margin: 0;
margin-top: 5px;
max-height: 300px;
overflow: auto;
padding: 5px;
......@@ -42,3 +46,7 @@ ul li {
li:nth-child(2n) {
background-color: lavender;
}
#current-status {
font-size: 15px;
}
......@@ -17,7 +17,7 @@
<body>
<h1>Offline Internals</h1>
<div>
<div id="current-status"></div>
<span id="current-status"></span>
<button id="refresh">Refresh page</button>
<button id="download">Dump</button>
</div>
......@@ -68,5 +68,8 @@
</thead>
<tbody id="request-queue"> </tbody>
</table>
<input id="url" type="url">
<button id="add-to-queue">Load in background</button>
<div id="save-url-state"></div>
</body>
</html>
......@@ -37,7 +37,10 @@ cr.define('offlineInternals', function() {
row.appendChild(checkboxCell);
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);
cell = document.createElement('td');
......@@ -106,6 +109,9 @@ cr.define('offlineInternals', function() {
function refreshAll() {
browserProxy_.getStoredPages().then(fillStoredPages);
browserProxy_.getRequestQueue().then(fillRequestQueue);
browserProxy_.getNetworkStatus().then(function(networkStatus) {
$('current-status').textContent = networkStatus;
});
refreshLog();
}
......@@ -173,19 +179,47 @@ cr.define('offlineInternals', function() {
}
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-selected').onclick = deleteSelectedPages;
$('refresh').onclick = refreshAll;
$('download').onclick = download;
$('log-model-on').onclick =
browserProxy_.setRecordPageModel.bind(browserProxy_, true);
$('log-model-off').onclick =
browserProxy_.setRecordPageModel.bind(browserProxy_, false);
$('log-request-on').onclick =
browserProxy_.setRecordRequestQueue.bind(browserProxy_, true);
$('log-request-off').onclick =
browserProxy_.setRecordRequestQueue.bind(browserProxy_, false);
$('log-model-on').onclick = togglePageModelLog.bind(this, true);
$('log-model-off').onclick = togglePageModelLog.bind(this, false);
$('log-request-on').onclick = toggleRequestQueueLog.bind(this, true);
$('log-request-off').onclick = toggleRequestQueueLog.bind(this, false);
$('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();
}
......
......@@ -95,6 +95,21 @@ cr.define('offlineInternals', function() {
* is retrieved.
*/
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() {
/** @override */
getLoggingState: function() {
return cr.sendWithPromise('getLoggingState');
}
},
/** @override */
addToRequestQueue: function(url) {
return cr.sendWithPromise('addToRequestQueue', url);
},
/** @override */
getNetworkStatus: function() {
return cr.sendWithPromise('getNetworkStatus');
},
};
return {
......
......@@ -19,16 +19,19 @@
#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/client_namespace_constants.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"
#include "content/public/browser/web_ui_message_handler.h"
#include "grit/browser_resources.h"
#include "net/base/network_change_notifier.h"
namespace {
// 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 {
public:
OfflineInternalsUIMessageHandler();
......@@ -62,6 +65,12 @@ class OfflineInternalsUIMessageHandler : public content::WebUIMessageHandler {
// Load whether logs are being recorded.
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.
void HandleStoredPagesCallback(
std::string callback_id,
......@@ -197,7 +206,7 @@ void OfflineInternalsUIMessageHandler::HandleStoredPagesCallback(
offline_page->SetString("namespace", page.client_id.name_space);
offline_page->SetDouble("size", page.file_size);
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("lastAccessTime",
page.last_access_time.ToJsTime());
......@@ -270,6 +279,17 @@ void OfflineInternalsUIMessageHandler::HandleSetRecordPageModel(
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(
const base::ListValue* args) {
bool should_record;
......@@ -308,6 +328,27 @@ void OfflineInternalsUIMessageHandler::HandleGetEventLogs(
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() {
web_ui()->RegisterMessageCallback(
"deleteAllPages",
......@@ -341,6 +382,14 @@ void OfflineInternalsUIMessageHandler::RegisterMessages() {
"getLoggingState",
base::Bind(&OfflineInternalsUIMessageHandler::HandleGetLoggingState,
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.
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