| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 /** | 5 /** |
| 6 * Implements the NavigationCollector object that powers the extension. | 6 * Implements the NavigationCollector object that powers the extension. |
| 7 * | 7 * |
| 8 * @author mkwst@google.com (Mike West) | 8 * @author mkwst@google.com (Mike West) |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 /** | 11 /** |
| 12 * Collects navigation events, and provides a list of successful requests | 12 * Collects navigation events, and provides a list of successful requests |
| 13 * that you can do interesting things with. Calling the constructor will | 13 * that you can do interesting things with. Calling the constructor will |
| 14 * automatically bind handlers to the relevant webnavigation API events, | 14 * automatically bind handlers to the relevant webnavigation API events, |
| 15 * and to a `getMostRequestedUrls` extension message for internal | 15 * and to a `getMostRequestedUrls` extension message for internal |
| 16 * communication between background pages and popups. | 16 * communication between background pages and popups. |
| 17 * | 17 * |
| 18 * @constructor | 18 * @constructor |
| 19 */ | 19 */ |
| 20 function NavigationCollector() { | 20 function NavigationCollector() { |
| 21 /** | 21 /** |
| 22 * A list of currently pending requests, implemented as a hash of each | 22 * A list of currently pending requests, implemented as a hash of each |
| 23 * request's tab ID, frame ID, and URL in order to ensure uniqueness. | 23 * request's tab ID, frame ID, and URL in order to ensure uniqueness. |
| 24 * | 24 * |
| 25 * @type {Object.<string, {start: number}>} | 25 * @type {Object<string, {start: number}>} |
| 26 * @private | 26 * @private |
| 27 */ | 27 */ |
| 28 this.pending_ = {}; | 28 this.pending_ = {}; |
| 29 | 29 |
| 30 /** | 30 /** |
| 31 * A list of completed requests, implemented as a hash of each | 31 * A list of completed requests, implemented as a hash of each |
| 32 * request's tab ID, frame ID, and URL in order to ensure uniqueness. | 32 * request's tab ID, frame ID, and URL in order to ensure uniqueness. |
| 33 * | 33 * |
| 34 * @type {Object.<string, Array.<NavigationCollector.Request>>} | 34 * @type {Object<string, Array<NavigationCollector.Request>>} |
| 35 * @private | 35 * @private |
| 36 */ | 36 */ |
| 37 this.completed_ = {}; | 37 this.completed_ = {}; |
| 38 | 38 |
| 39 /** | 39 /** |
| 40 * A list of requests that errored off, implemented as a hash of each | 40 * A list of requests that errored off, implemented as a hash of each |
| 41 * request's tab ID, frame ID, and URL in order to ensure uniqueness. | 41 * request's tab ID, frame ID, and URL in order to ensure uniqueness. |
| 42 * | 42 * |
| 43 * @type {Object.<string, Array.<NavigationCollector.Request>>} | 43 * @type {Object<string, Array<NavigationCollector.Request>>} |
| 44 * @private | 44 * @private |
| 45 */ | 45 */ |
| 46 this.errored_ = {}; | 46 this.errored_ = {}; |
| 47 | 47 |
| 48 // Bind handlers to the 'webNavigation' events that we're interested | 48 // Bind handlers to the 'webNavigation' events that we're interested |
| 49 // in handling in order to build up a complete picture of the whole | 49 // in handling in order to build up a complete picture of the whole |
| 50 // navigation event. | 50 // navigation event. |
| 51 chrome.webNavigation.onCreatedNavigationTarget.addListener( | 51 chrome.webNavigation.onCreatedNavigationTarget.addListener( |
| 52 this.onCreatedNavigationTargetListener_.bind(this)); | 52 this.onCreatedNavigationTargetListener_.bind(this)); |
| 53 chrome.webNavigation.onBeforeNavigate.addListener( | 53 chrome.webNavigation.onBeforeNavigate.addListener( |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 * @enum {string} | 107 * @enum {string} |
| 108 */ | 108 */ |
| 109 NavigationCollector.NavigationQualifier = { | 109 NavigationCollector.NavigationQualifier = { |
| 110 CLIENT_REDIRECT: 'client_redirect', | 110 CLIENT_REDIRECT: 'client_redirect', |
| 111 FORWARD_BACK: 'forward_back', | 111 FORWARD_BACK: 'forward_back', |
| 112 SERVER_REDIRECT: 'server_redirect' | 112 SERVER_REDIRECT: 'server_redirect' |
| 113 }; | 113 }; |
| 114 | 114 |
| 115 /** | 115 /** |
| 116 * @typedef {{url: string, transitionType: NavigationCollector.NavigationType, | 116 * @typedef {{url: string, transitionType: NavigationCollector.NavigationType, |
| 117 * transitionQualifier: Array.<NavigationCollector.NavigationQualifier>, | 117 * transitionQualifier: Array<NavigationCollector.NavigationQualifier>, |
| 118 * openedInNewTab: boolean, source: {frameId: ?number, tabId: ?number}, | 118 * openedInNewTab: boolean, source: {frameId: ?number, tabId: ?number}, |
| 119 * duration: number}} | 119 * duration: number}} |
| 120 */ | 120 */ |
| 121 NavigationCollector.Request; | 121 NavigationCollector.Request; |
| 122 | 122 |
| 123 /////////////////////////////////////////////////////////////////////////////// | 123 /////////////////////////////////////////////////////////////////////////////// |
| 124 | 124 |
| 125 NavigationCollector.prototype = { | 125 NavigationCollector.prototype = { |
| 126 /** | 126 /** |
| 127 * Returns a somewhat unique ID for a given WebNavigation request. | 127 * Returns a somewhat unique ID for a given WebNavigation request. |
| (...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 onMessageListener_: function(message, sender, sendResponse) { | 407 onMessageListener_: function(message, sender, sendResponse) { |
| 408 if (message.type === 'getMostRequestedUrls') | 408 if (message.type === 'getMostRequestedUrls') |
| 409 sendResponse({result: this.getMostRequestedUrls(message.num)}); | 409 sendResponse({result: this.getMostRequestedUrls(message.num)}); |
| 410 else | 410 else |
| 411 sendResponse({}); | 411 sendResponse({}); |
| 412 }, | 412 }, |
| 413 | 413 |
| 414 /////////////////////////////////////////////////////////////////////////////// | 414 /////////////////////////////////////////////////////////////////////////////// |
| 415 | 415 |
| 416 /** | 416 /** |
| 417 * @return {Object.<string, NavigationCollector.Request>} The complete list of | 417 * @return {Object<string, NavigationCollector.Request>} The complete list of |
| 418 * successful navigation requests. | 418 * successful navigation requests. |
| 419 */ | 419 */ |
| 420 get completed() { | 420 get completed() { |
| 421 return this.completed_; | 421 return this.completed_; |
| 422 }, | 422 }, |
| 423 | 423 |
| 424 | 424 |
| 425 /** | 425 /** |
| 426 * @return {Object.<string, Navigationcollector.Request>} The complete list of | 426 * @return {Object<string, Navigationcollector.Request>} The complete list of |
| 427 * unsuccessful navigation requests. | 427 * unsuccessful navigation requests. |
| 428 */ | 428 */ |
| 429 get errored() { | 429 get errored() { |
| 430 return this.errored_; | 430 return this.errored_; |
| 431 }, | 431 }, |
| 432 | 432 |
| 433 | 433 |
| 434 /** | 434 /** |
| 435 * Get a list of the X most requested URLs. | 435 * Get a list of the X most requested URLs. |
| 436 * | 436 * |
| 437 * @param {number=} num The number of successful navigation requests to | 437 * @param {number=} num The number of successful navigation requests to |
| 438 * return. If 0 is passed in, or the argument left off entirely, all | 438 * return. If 0 is passed in, or the argument left off entirely, all |
| 439 * successful requests are returned. | 439 * successful requests are returned. |
| 440 * @return {Object.<string, NavigationCollector.Request>} The list of | 440 * @return {Object<string, NavigationCollector.Request>} The list of |
| 441 * successful navigation requests, sorted in decending order of frequency. | 441 * successful navigation requests, sorted in decending order of frequency. |
| 442 */ | 442 */ |
| 443 getMostRequestedUrls: function(num) { | 443 getMostRequestedUrls: function(num) { |
| 444 return this.getMostFrequentUrls_(this.completed, num); | 444 return this.getMostFrequentUrls_(this.completed, num); |
| 445 }, | 445 }, |
| 446 | 446 |
| 447 | 447 |
| 448 /** | 448 /** |
| 449 * Get a list of the X most errored URLs. | 449 * Get a list of the X most errored URLs. |
| 450 * | 450 * |
| 451 * @param {number=} num The number of unsuccessful navigation requests to | 451 * @param {number=} num The number of unsuccessful navigation requests to |
| 452 * return. If 0 is passed in, or the argument left off entirely, all | 452 * return. If 0 is passed in, or the argument left off entirely, all |
| 453 * successful requests are returned. | 453 * successful requests are returned. |
| 454 * @return {Object.<string, NavigationCollector.Request>} The list of | 454 * @return {Object<string, NavigationCollector.Request>} The list of |
| 455 * unsuccessful navigation requests, sorted in decending order | 455 * unsuccessful navigation requests, sorted in decending order |
| 456 * of frequency. | 456 * of frequency. |
| 457 */ | 457 */ |
| 458 getMostErroredUrls: function(num) { | 458 getMostErroredUrls: function(num) { |
| 459 return this.getMostErroredUrls_(this.errored, num); | 459 return this.getMostErroredUrls_(this.errored, num); |
| 460 }, | 460 }, |
| 461 | 461 |
| 462 | 462 |
| 463 /** | 463 /** |
| 464 * Get a list of the most frequent URLs in a list. | 464 * Get a list of the most frequent URLs in a list. |
| 465 * | 465 * |
| 466 * @param {NavigationCollector.Request} list A list of URLs to parse. | 466 * @param {NavigationCollector.Request} list A list of URLs to parse. |
| 467 * @param {number=} num The number of navigation requests to return. If | 467 * @param {number=} num The number of navigation requests to return. If |
| 468 * 0 is passed in, or the argument left off entirely, all requests | 468 * 0 is passed in, or the argument left off entirely, all requests |
| 469 * are returned. | 469 * are returned. |
| 470 * @return {Object.<string, NavigationCollector.Request>} The list of | 470 * @return {Object<string, NavigationCollector.Request>} The list of |
| 471 * navigation requests, sorted in decending order of frequency. | 471 * navigation requests, sorted in decending order of frequency. |
| 472 * @private | 472 * @private |
| 473 */ | 473 */ |
| 474 getMostFrequentUrls_: function(list, num) { | 474 getMostFrequentUrls_: function(list, num) { |
| 475 var result = []; | 475 var result = []; |
| 476 var avg; | 476 var avg; |
| 477 // Convert the 'completed_' object to an array. | 477 // Convert the 'completed_' object to an array. |
| 478 for (var x in list) { | 478 for (var x in list) { |
| 479 avg = 0; | 479 avg = 0; |
| 480 if (list.hasOwnProperty(x) && list[x].length) { | 480 if (list.hasOwnProperty(x) && list[x].length) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 491 } | 491 } |
| 492 } | 492 } |
| 493 // Sort the array. | 493 // Sort the array. |
| 494 result.sort(function(a, b) { | 494 result.sort(function(a, b) { |
| 495 return b.numRequests - a.numRequests; | 495 return b.numRequests - a.numRequests; |
| 496 }); | 496 }); |
| 497 // Return the requested number of results. | 497 // Return the requested number of results. |
| 498 return num ? result.slice(0, num) : result; | 498 return num ? result.slice(0, num) : result; |
| 499 } | 499 } |
| 500 }; | 500 }; |
| OLD | NEW |