1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
8 WebInspector.Context = function()
10 this._flavors = new Map();
11 this._eventDispatchers = new Map();
17 WebInspector.Context.Events = {
18 FlavorChanged: "FlavorChanged"
21 WebInspector.Context.prototype = {
23 * @param {function(new:T, ...)} flavorType
24 * @param {?T} flavorValue
27 setFlavor: function(flavorType, flavorValue)
29 var value = this._flavors.get(flavorType) || null;
30 if (value === flavorValue)
33 this._flavors.set(flavorType, flavorValue);
35 this._flavors.remove(flavorType);
37 this._dispatchFlavorChange(flavorType, flavorValue);
41 * @param {function(new:T, ...)} flavorType
42 * @param {?T} flavorValue
45 _dispatchFlavorChange: function(flavorType, flavorValue)
47 var dispatcher = this._eventDispatchers.get(flavorType);
50 dispatcher.dispatchEventToListeners(WebInspector.Context.Events.FlavorChanged, flavorValue);
54 * @param {function(new:Object, ...)} flavorType
55 * @param {function(!WebInspector.Event)} listener
56 * @param {!Object=} thisObject
58 addFlavorChangeListener: function(flavorType, listener, thisObject)
60 var dispatcher = this._eventDispatchers.get(flavorType);
62 dispatcher = new WebInspector.Object();
63 this._eventDispatchers.set(flavorType, dispatcher);
65 dispatcher.addEventListener(WebInspector.Context.Events.FlavorChanged, listener, thisObject);
69 * @param {function(new:Object, ...)} flavorType
70 * @param {function(!WebInspector.Event)} listener
71 * @param {!Object=} thisObject
73 removeFlavorChangeListener: function(flavorType, listener, thisObject)
75 var dispatcher = this._eventDispatchers.get(flavorType);
78 dispatcher.removeEventListener(WebInspector.Context.Events.FlavorChanged, listener, thisObject);
79 if (!dispatcher.hasEventListeners(WebInspector.Context.Events.FlavorChanged))
80 this._eventDispatchers.remove(flavorType);
84 * @param {function(new:T, ...)} flavorType
88 flavor: function(flavorType)
90 return this._flavors.get(flavorType) || null;
94 * @return {!Array.<function(new:Object, ...)>}
98 return this._flavors.keys();
102 * @param {!Array.<!Runtime.Extension>} extensions
103 * @return {!Set.<!Runtime.Extension>}
105 applicableExtensions: function(extensions)
107 var targetExtensionSet = new Set();
109 var availableFlavors = this.flavors();
110 extensions.forEach(function(extension) {
111 if (self.runtime.isExtensionApplicableToContextTypes(extension, availableFlavors))
112 targetExtensionSet.add(extension);
115 return targetExtensionSet;
119 WebInspector.context = new WebInspector.Context();