Commit 9823151d authored by Yi Su's avatar Yi Su Committed by Commit Bot

Introduce Timer class to clean up public attributes on __gCrWeb.findInPage.

This CL encapsulates code that are related with overtime checking into
Timer class. This helps make the code cleaner and safer.

Bug: 228585
Change-Id: Id2d018b90a6976e39504914dd3f354a276b5601d
Reviewed-on: https://chromium-review.googlesource.com/c/1349714
Commit-Queue: Yi Su <mrsuyi@chromium.org>
Reviewed-by: default avatarPeter Lee <pkl@chromium.org>
Reviewed-by: default avatarJustin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610945}
parent a186b26e
...@@ -22,6 +22,9 @@ __gCrWeb.findInPage = {}; ...@@ -22,6 +22,9 @@ __gCrWeb.findInPage = {};
* Node, otherwise the match involves multiple HTML TEXT Nodes. * Node, otherwise the match involves multiple HTML TEXT Nodes.
*/ */
class Match { class Match {
/**
* @param{Array<Node>} nodes All <chrome_find> Nodes of this match.
*/
constructor(nodes) { constructor(nodes) {
this.nodes = nodes; this.nodes = nodes;
} }
...@@ -82,7 +85,6 @@ __gCrWeb.findInPage.selectedMatchIndex = -1; ...@@ -82,7 +85,6 @@ __gCrWeb.findInPage.selectedMatchIndex = -1;
*/ */
class Replacement { class Replacement {
/** /**
* Contructor for Replacement.
* @param {Node} The HTML Node containing search result. * @param {Node} The HTML Node containing search result.
* @param {Array<Node>} New HTML Nodes created for substitution of |oldNode|. * @param {Array<Node>} New HTML Nodes created for substitution of |oldNode|.
*/ */
...@@ -221,23 +223,24 @@ function getRegex_(findText) { ...@@ -221,23 +223,24 @@ function getRegex_(findText) {
}; };
/** /**
* Get current timestamp. * A timer that checks timeout for long tasks.
* @return {number} timestamp.
*/ */
__gCrWeb.findInPage.time = function() { class Timer {
return (new Date).getTime(); /**
}; * @param {Number} timeoutMs Timeout in milliseconds.
*/
constructor(timeoutMs) {
this.beginTime = Date.now();
this.timeoutMs = timeoutMs;
}
/** /**
* After |timeCheck| iterations, return true if |now| - |start| is greater than * @return {Boolean} Whether this timer has been reached.
* |timeout|.
* @return {boolean} Find in page needs to return.
*/ */
__gCrWeb.findInPage.overTime = function() { overtime() {
return ( return Date.now() - this.beginTime > this.timeoutMs;
__gCrWeb.findInPage.time() - __gCrWeb.findInPage.startTime > }
__gCrWeb.findInPage.timeout); }
};
/** /**
* Looks for a phrase in the DOM. * Looks for a phrase in the DOM.
...@@ -294,8 +297,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -294,8 +297,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
if (searchInProgress_ == false) if (searchInProgress_ == false)
return NO_RESULTS; return NO_RESULTS;
__gCrWeb.findInPage.timeout = timeout; let timer = new Timer(timeout);
__gCrWeb.findInPage.startTime = __gCrWeb.findInPage.time();
let regex = __gCrWeb.findInPage.regex; let regex = __gCrWeb.findInPage.regex;
// Go through every node in DFS fashion. // Go through every node in DFS fashion.
...@@ -350,7 +352,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -350,7 +352,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
} }
} }
if (__gCrWeb.findInPage.overTime()) if (timer.overtime())
return '[false]'; return '[false]';
if (__gCrWeb.findInPage.stack.length > 0) { if (__gCrWeb.findInPage.stack.length > 0) {
...@@ -362,7 +364,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -362,7 +364,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
// Execute replacements to highlight search results. // Execute replacements to highlight search results.
for (let i = replacementsIndex_; i < replacements_.length; ++i) { for (let i = replacementsIndex_; i < replacements_.length; ++i) {
if (__gCrWeb.findInPage.overTime()) { if (timer.overtime()) {
replacementsIndex_ = i; replacementsIndex_ = i;
return __gCrWeb.stringify([false]); return __gCrWeb.stringify([false]);
} }
...@@ -374,7 +376,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) { ...@@ -374,7 +376,7 @@ __gCrWeb.findInPage.pumpSearch = function(timeout) {
let maxVisible = MAX_VISIBLE_ELEMENTS; let maxVisible = MAX_VISIBLE_ELEMENTS;
for (let index = __gCrWeb.findInPage.visibleIndex; index < max; index++) { for (let index = __gCrWeb.findInPage.visibleIndex; index < max; index++) {
let match = __gCrWeb.findInPage.matches[index]; let match = __gCrWeb.findInPage.matches[index];
if (__gCrWeb.findInPage.overTime()) { if (timer.overtime()) {
__gCrWeb.findInPage.visibleIndex = index; __gCrWeb.findInPage.visibleIndex = index;
return __gCrWeb.stringify([false]); return __gCrWeb.stringify([false]);
} }
......
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