2017-06-20 23:41:27 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
const NetworkManager = require('./NetworkManager');
|
|
|
|
|
|
|
|
class NavigatorWatcher {
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Connection} client
|
2017-07-19 01:54:24 +00:00
|
|
|
* @param {!NetworkManager} networkManager
|
2017-06-21 20:58:49 +00:00
|
|
|
* @param {!Object=} options
|
|
|
|
*/
|
2017-07-19 01:54:24 +00:00
|
|
|
constructor(client, networkManager, options = {}) {
|
2017-06-21 20:51:06 +00:00
|
|
|
this._client = client;
|
2017-07-19 01:54:24 +00:00
|
|
|
this._networkManager = networkManager;
|
2017-07-22 23:25:00 +00:00
|
|
|
this._timeout = typeof options['timeout'] === 'number' ? options['timeout'] : 30000;
|
2017-07-07 22:43:17 +00:00
|
|
|
this._idleTime = typeof options['networkIdleTimeout'] === 'number' ? options['networkIdleTimeout'] : 1000;
|
|
|
|
this._idleInflight = typeof options['networkIdleInflight'] === 'number' ? options['networkIdleInflight'] : 2;
|
2017-07-14 20:59:36 +00:00
|
|
|
this._waitUntil = typeof options['waitUntil'] === 'string' ? options['waitUntil'] : 'load';
|
|
|
|
console.assert(this._waitUntil === 'load' || this._waitUntil === 'networkidle', 'Unknown value for options.waitUntil: ' + this._waitUntil);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
/**
|
2017-07-19 01:54:24 +00:00
|
|
|
* @return {!Promise<!Map<string, !Response>>}
|
2017-06-21 20:58:49 +00:00
|
|
|
*/
|
2017-07-19 01:54:24 +00:00
|
|
|
async waitForNavigation() {
|
2017-07-07 22:43:17 +00:00
|
|
|
this._init();
|
2017-07-19 01:54:24 +00:00
|
|
|
|
2017-07-10 22:09:52 +00:00
|
|
|
let certificateError = new Promise(fulfill => this._client.once('Security.certificateError', fulfill))
|
|
|
|
.then(error => new Error('SSL Certiciate error: ' + error.errorType));
|
|
|
|
let networkIdle = new Promise(fulfill => this._networkIdleCallback = fulfill).then(() => null);
|
|
|
|
let loadEventFired = new Promise(fulfill => this._client.once('Page.loadEventFired', fulfill)).then(() => null);
|
2017-07-22 23:25:00 +00:00
|
|
|
let watchdog = new Promise(fulfill => this._maximumTimer = setTimeout(fulfill, this._timeout)).then(() => new Error('Navigation Timeout Exceeded: ' + this._timeout + 'ms exceeded'));
|
2017-06-28 21:39:37 +00:00
|
|
|
|
2017-06-21 20:51:06 +00:00
|
|
|
try {
|
2017-07-17 19:15:06 +00:00
|
|
|
// Await for the command to throw exception in case of illegal arguments.
|
|
|
|
const error = await Promise.race([certificateError, watchdog, this._waitUntil === 'load' ? loadEventFired : networkIdle]);
|
|
|
|
if (error)
|
|
|
|
throw error;
|
2017-07-19 01:54:24 +00:00
|
|
|
return this._responses;
|
2017-07-17 19:15:06 +00:00
|
|
|
} finally {
|
2017-07-07 22:43:17 +00:00
|
|
|
this._cleanup();
|
2017-06-20 23:41:27 +00:00
|
|
|
}
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
cancel() {
|
|
|
|
this._cleanup();
|
|
|
|
}
|
|
|
|
|
2017-06-28 21:39:37 +00:00
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
|
|
|
_onLoadingStarted(event) {
|
|
|
|
this._requestIds.add(event.requestId);
|
|
|
|
if (!event.redirectResponse)
|
|
|
|
++this._inflightRequests;
|
|
|
|
if (this._inflightRequests > this._idleInflight) {
|
|
|
|
clearTimeout(this._idleTimer);
|
|
|
|
this._idleTimer = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} event
|
|
|
|
*/
|
2017-06-21 20:51:06 +00:00
|
|
|
_onLoadingCompleted(event) {
|
2017-06-28 21:39:37 +00:00
|
|
|
if (!this._requestIds.has(event.requestId))
|
2017-06-21 20:51:06 +00:00
|
|
|
return;
|
2017-06-28 21:39:37 +00:00
|
|
|
|
|
|
|
--this._inflightRequests;
|
|
|
|
if (this._inflightRequests <= this._idleInflight && !this._idleTimer)
|
2017-07-07 22:43:17 +00:00
|
|
|
this._idleTimer = setTimeout(this._networkIdleCallback, this._idleTime);
|
2017-06-28 21:39:37 +00:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:43:17 +00:00
|
|
|
_init() {
|
|
|
|
this._loadingStartedHandler = this._onLoadingStarted.bind(this);
|
|
|
|
this._loadingCompletedHandler = this._onLoadingCompleted.bind(this);
|
2017-07-19 01:54:24 +00:00
|
|
|
this._onResponseHandler = this._onResponse.bind(this);
|
2017-07-07 22:43:17 +00:00
|
|
|
this._client.on('Network.requestWillBeSent', this._loadingStartedHandler);
|
|
|
|
this._client.on('Network.loadingFinished', this._loadingCompletedHandler);
|
|
|
|
this._client.on('Network.loadingFailed', this._loadingCompletedHandler);
|
|
|
|
this._client.on('Network.webSocketCreated', this._loadingStartedHandler);
|
|
|
|
this._client.on('Network.webSocketClosed', this._loadingCompletedHandler);
|
2017-07-19 01:54:24 +00:00
|
|
|
this._networkManager.on(NetworkManager.Events.Response, this._onResponseHandler);
|
2017-07-07 22:43:17 +00:00
|
|
|
|
|
|
|
this._inflightRequests = 0;
|
|
|
|
this._requestIds = new Set();
|
2017-07-19 01:54:24 +00:00
|
|
|
/** @type {!Map<string, !Response>} */
|
|
|
|
this._responses = new Map();
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
|
2017-07-07 22:43:17 +00:00
|
|
|
_cleanup() {
|
|
|
|
this._client.removeListener('Network.requestWillBeSent', this._loadingStartedHandler);
|
|
|
|
this._client.removeListener('Network.loadingFinished', this._loadingCompletedHandler);
|
|
|
|
this._client.removeListener('Network.loadingFailed', this._loadingCompletedHandler);
|
|
|
|
this._client.removeListener('Network.webSocketCreated', this._loadingStartedHandler);
|
|
|
|
this._client.removeListener('Network.webSocketClosed', this._loadingCompletedHandler);
|
2017-07-19 01:54:24 +00:00
|
|
|
this._networkManager.removeListener(NetworkManager.Events.Response, this._onResponseHandler);
|
2017-06-28 21:39:37 +00:00
|
|
|
|
2017-07-07 22:43:17 +00:00
|
|
|
clearTimeout(this._idleTimer);
|
|
|
|
clearTimeout(this._maximumTimer);
|
2017-07-19 01:54:24 +00:00
|
|
|
this._responses = new Map();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Response} response
|
|
|
|
*/
|
|
|
|
_onResponse(response) {
|
|
|
|
this._responses.set(response.url, response);
|
2017-06-21 20:51:06 +00:00
|
|
|
}
|
2017-06-20 23:41:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 01:54:24 +00:00
|
|
|
module.exports = NavigatorWatcher;
|