Skip to content

Commit

Permalink
Add rule methods to chrome event type definitions.
Browse files Browse the repository at this point in the history
Related to #2527

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=175736558
  • Loading branch information
brad4d authored and Tyler Breisacher committed Nov 14, 2017
1 parent dad576e commit 9dc0353
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 12 deletions.
83 changes: 80 additions & 3 deletions contrib/externs/chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,93 @@ Port.prototype.postMessage = function(obj) {};
Port.prototype.disconnect = function() {};


/**
* Base event type without listener methods.
*
* This interface exists for event interfaces whose addListeners() method takes
* more than one parameter. Those interfaces must inherit from this one, so they
* can supply their own custom listener method declarations.
*
* Event interfaces whose addListeners() method takes just one parameter should
* inherit from ChromeBaseEvent instead. It extends this interface.
*
* @see https://developer.chrome.com/extensions/events.html
* @interface
*/
function ChromeBaseEventNoListeners() {}


/**
* @param {!Array<!Rule>} rules
* @param {function(!Array<!Rule>): void=} callback
* @see https://developer.chrome.com/extensions/events#method-Event-addRules
*/
ChromeBaseEventNoListeners.prototype.addRules = function(rules, callback) {};


/**
* Returns currently registered rules.
*
* NOTE: The API allows the first argument to be omitted.
* That cannot be correctly represented here, so we end up incorrectly
* allowing 2 callback arguments.
* @param {!Array<string>|function(!Array<!Rule>): void} ruleIdentifiersOrCb
* @param {function(!Array<!Rule>): void=} callback
* @see https://developer.chrome.com/extensions/events#method-Event-getRules
*/
ChromeBaseEventNoListeners.prototype.getRules =
function(ruleIdentifiersOrCb, callback) {};


/**
* Removes currently registered rules.
*
* NOTE: The API allows the either or both arguments to be omitted.
* That cannot be correctly represented here, so we end up incorrectly
* allowing 2 callback arguments.
* @param {(!Array<string>|function(): void)=} ruleIdentifiersOrCb
* @param {function(): void=} callback
* @see https://developer.chrome.com/extensions/events#method-Event-removeRules
*/
ChromeBaseEventNoListeners.prototype.removeRules =
function(ruleIdentifiersOrCb, callback) {};


/**
* @see https://developer.chrome.com/extensions/events#type-Rule
* @record
*/
function Rule() {}


/** @type {string|undefined} */
Rule.prototype.id;


/** @type {!Array<string>|undefined} */
Rule.prototype.tags;


/** @type {!Array<*>} */
Rule.prototype.conditions;


/** @type {!Array<*>} */
Rule.prototype.actions;


/** @type {number|undefined} */
Rule.prototype.priority;


/**
* Base event type from which all others inherit.
*
* LISTENER must be a function type that returns void.
*
* @see https://developer.chrome.com/extensions/events.html
* @interface
* @extends {ChromeBaseEventNoListeners}
* @template LISTENER
*/
function ChromeBaseEvent() {}
Expand Down Expand Up @@ -101,9 +181,6 @@ ChromeBaseEvent.prototype.hasListener = function(callback) {};
ChromeBaseEvent.prototype.hasListeners = function() {};


// TODO(bradfordcsmith): Add the addRules, getRules, and removeRules methods?


/**
* Event whose listeners take unspecified parameters.
*
Expand Down
21 changes: 12 additions & 9 deletions contrib/externs/chrome_extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,14 @@
*
* For those cases, create a record type for the event that extends
* ChromeBaseEvent with the correct listener function type filled in.
* See any of the Event type definitions below for an example.
* See any of the Event type definitions below that extend ChromeBaseEvent for
* an example.
*
* In some cases the addListener method takes more than one argument, so
* inheritance from ChromeBaseEvent will not work. In those cases extend
* ChromeBaseEventNoListeners instead and add the listener interfaces
* explicitly. See any of the Event type definitions below that extend
* ChromeBaseEventNoListeners for an example.
*
* E. Nullability
* We treat the Chrome Extension API pages as "the truth". Not-null types
Expand Down Expand Up @@ -4249,9 +4256,8 @@ chrome.input.ime = {};
*
* The addListener() method for this class takes more than one argument, so it
* isn't possible to just extend ChromeBaseEvent here.
* TODO(bradfordcsmith): These events won't automatically inherit new methods
* added to `ChromeBaseEvent`.
* @interface
* @extends {ChromeBaseEventNoListeners}
*/
function ChromeInputImeOnKeyEventEvent() {}

Expand Down Expand Up @@ -6034,9 +6040,8 @@ chrome.webNavigation.onHistoryStateUpdated;
*
* The `addListener()` methods for these take more than one argument, so they
* cannot just extend `ChromeBaseEvent`.
* TODO(bradfordcsmith): These events won't automatically inherit new methods
* added to `ChromeBaseEvent`.
* @interface
* @extends {ChromeBaseEventNoListeners}
* @template LISTENER
*/
function WebRequestBaseEvent() {}
Expand Down Expand Up @@ -6111,10 +6116,9 @@ function WebRequestOnAuthRequiredEvent() {}
*
* The `addListener()` methods for these take more than one argument, so they
* cannot just extend `ChromeBaseEvent`.
* TODO(bradfordcsmith): These events won't automatically inherit new methods
* added to `ChromeBaseEvent`.
* @see https://developer.chrome.com/extensions/webRequest
* @interface
* @extends {ChromeBaseEventNoListeners}
*/
function WebRequestOnErrorOccurredEvent() {}

Expand Down Expand Up @@ -9341,9 +9345,8 @@ chrome.mdns.MdnsService.prototype.serviceData;
*
* The `addListener()` methods for these take more than one argument, so they
* cannot just extend `ChromeBaseEvent`.
* TODO(bradfordcsmith): These events won't automatically inherit new methods
* added to `ChromeBaseEvent`.
* @interface
* @extends {ChromeBaseEventNoListeners}
*/
chrome.mdns.ServiceListEvent = function() {};

Expand Down

0 comments on commit 9dc0353

Please sign in to comment.