OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 | |
5 /** | 4 /** |
6 * @constructor | |
7 * @implements {WebInspector.TargetManager.Observer} | 5 * @implements {WebInspector.TargetManager.Observer} |
| 6 * @unrestricted |
8 */ | 7 */ |
9 WebInspector.MultitargetTouchModel = function() | 8 WebInspector.MultitargetTouchModel = class { |
10 { | 9 constructor() { |
11 this._touchEnabled = false; | 10 this._touchEnabled = false; |
12 this._touchMobile = false; | 11 this._touchMobile = false; |
13 this._customTouchEnabled = false; | 12 this._customTouchEnabled = false; |
14 | 13 |
15 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Capabili
ty.Browser); | 14 WebInspector.targetManager.observeTargets(this, WebInspector.Target.Capabili
ty.Browser); |
| 15 } |
| 16 |
| 17 /** |
| 18 * @return {!WebInspector.MultitargetTouchModel} |
| 19 */ |
| 20 static instance() { |
| 21 if (!WebInspector.MultitargetTouchModel._instance) |
| 22 WebInspector.MultitargetTouchModel._instance = new WebInspector.Multitarge
tTouchModel(); |
| 23 return /** @type {!WebInspector.MultitargetTouchModel} */ (WebInspector.Mult
itargetTouchModel._instance); |
| 24 } |
| 25 |
| 26 /** |
| 27 * @param {boolean} enabled |
| 28 * @param {boolean} mobile |
| 29 */ |
| 30 setTouchEnabled(enabled, mobile) { |
| 31 this._touchEnabled = enabled; |
| 32 this._touchMobile = mobile; |
| 33 this._updateAllTargets(); |
| 34 } |
| 35 |
| 36 /** |
| 37 * @param {boolean} enabled |
| 38 */ |
| 39 setCustomTouchEnabled(enabled) { |
| 40 this._customTouchEnabled = enabled; |
| 41 this._updateAllTargets(); |
| 42 } |
| 43 |
| 44 _updateAllTargets() { |
| 45 for (var target of WebInspector.targetManager.targets(WebInspector.Target.Ca
pability.Browser)) |
| 46 this._applyToTarget(target); |
| 47 } |
| 48 |
| 49 /** |
| 50 * @param {!WebInspector.Target} target |
| 51 */ |
| 52 _applyToTarget(target) { |
| 53 var current = {enabled: this._touchEnabled, configuration: this._touchMobile
? 'mobile' : 'desktop'}; |
| 54 if (this._customTouchEnabled) |
| 55 current = {enabled: true, configuration: 'mobile'}; |
| 56 |
| 57 var domModel = WebInspector.DOMModel.fromTarget(target); |
| 58 var inspectModeEnabled = domModel ? domModel.inspectModeEnabled() : false; |
| 59 if (inspectModeEnabled) |
| 60 current = {enabled: false, configuration: 'mobile'}; |
| 61 |
| 62 /** |
| 63 * @suppressGlobalPropertiesCheck |
| 64 */ |
| 65 const injectedFunction = function() { |
| 66 const touchEvents = ['ontouchstart', 'ontouchend', 'ontouchmove', 'ontouch
cancel']; |
| 67 var recepients = [window.__proto__, document.__proto__]; |
| 68 for (var i = 0; i < touchEvents.length; ++i) { |
| 69 for (var j = 0; j < recepients.length; ++j) { |
| 70 if (!(touchEvents[i] in recepients[j])) |
| 71 Object.defineProperty( |
| 72 recepients[j], touchEvents[i], {value: null, writable: true, con
figurable: true, enumerable: true}); |
| 73 } |
| 74 } |
| 75 }; |
| 76 |
| 77 var symbol = WebInspector.MultitargetTouchModel._symbol; |
| 78 var previous = target[symbol] || {enabled: false, configuration: 'mobile', s
criptId: ''}; |
| 79 |
| 80 if (previous.enabled === current.enabled && (!current.enabled || previous.co
nfiguration === current.configuration)) |
| 81 return; |
| 82 |
| 83 if (previous.scriptId) { |
| 84 target.pageAgent().removeScriptToEvaluateOnLoad(previous.scriptId); |
| 85 target[symbol].scriptId = ''; |
| 86 } |
| 87 |
| 88 target[symbol] = current; |
| 89 target[symbol].scriptId = ''; |
| 90 |
| 91 if (current.enabled) |
| 92 target.pageAgent().addScriptToEvaluateOnLoad('(' + injectedFunction.toStri
ng() + ')()', scriptAddedCallback); |
| 93 |
| 94 /** |
| 95 * @param {?Protocol.Error} error |
| 96 * @param {string} scriptId |
| 97 */ |
| 98 function scriptAddedCallback(error, scriptId) { |
| 99 (target[symbol] || {}).scriptId = error ? '' : scriptId; |
| 100 } |
| 101 |
| 102 target.emulationAgent().setTouchEmulationEnabled(current.enabled, current.co
nfiguration); |
| 103 } |
| 104 |
| 105 /** |
| 106 * @param {!WebInspector.Event} event |
| 107 */ |
| 108 _inspectModeToggled(event) { |
| 109 var domModel = /** @type {!WebInspector.DOMModel} */ (event.target); |
| 110 this._applyToTarget(domModel.target()); |
| 111 } |
| 112 |
| 113 /** |
| 114 * @override |
| 115 * @param {!WebInspector.Target} target |
| 116 */ |
| 117 targetAdded(target) { |
| 118 var domModel = WebInspector.DOMModel.fromTarget(target); |
| 119 if (domModel) |
| 120 domModel.addEventListener(WebInspector.DOMModel.Events.InspectModeWillBeTo
ggled, this._inspectModeToggled, this); |
| 121 this._applyToTarget(target); |
| 122 } |
| 123 |
| 124 /** |
| 125 * @override |
| 126 * @param {!WebInspector.Target} target |
| 127 */ |
| 128 targetRemoved(target) { |
| 129 var domModel = WebInspector.DOMModel.fromTarget(target); |
| 130 if (domModel) |
| 131 domModel.removeEventListener( |
| 132 WebInspector.DOMModel.Events.InspectModeWillBeToggled, this._inspectMo
deToggled, this); |
| 133 } |
16 }; | 134 }; |
17 | 135 |
18 WebInspector.MultitargetTouchModel._symbol = Symbol("MultitargetTouchModel.symbo
l"); | 136 WebInspector.MultitargetTouchModel._symbol = Symbol('MultitargetTouchModel.symbo
l'); |
19 | |
20 WebInspector.MultitargetTouchModel.prototype = { | |
21 /** | |
22 * @param {boolean} enabled | |
23 * @param {boolean} mobile | |
24 */ | |
25 setTouchEnabled: function(enabled, mobile) | |
26 { | |
27 this._touchEnabled = enabled; | |
28 this._touchMobile = mobile; | |
29 this._updateAllTargets(); | |
30 }, | |
31 | |
32 /** | |
33 * @param {boolean} enabled | |
34 */ | |
35 setCustomTouchEnabled: function(enabled) | |
36 { | |
37 this._customTouchEnabled = enabled; | |
38 this._updateAllTargets(); | |
39 }, | |
40 | |
41 _updateAllTargets: function() | |
42 { | |
43 for (var target of WebInspector.targetManager.targets(WebInspector.Targe
t.Capability.Browser)) | |
44 this._applyToTarget(target); | |
45 }, | |
46 | |
47 /** | |
48 * @param {!WebInspector.Target} target | |
49 */ | |
50 _applyToTarget: function(target) | |
51 { | |
52 var current = {enabled: this._touchEnabled, configuration : this._touchM
obile ? "mobile" : "desktop"}; | |
53 if (this._customTouchEnabled) | |
54 current = {enabled: true, configuration: "mobile"}; | |
55 | |
56 var domModel = WebInspector.DOMModel.fromTarget(target); | |
57 var inspectModeEnabled = domModel ? domModel.inspectModeEnabled() : fals
e; | |
58 if (inspectModeEnabled) | |
59 current = {enabled: false, configuration: "mobile"}; | |
60 | |
61 /** | |
62 * @suppressGlobalPropertiesCheck | |
63 */ | |
64 const injectedFunction = function() { | |
65 const touchEvents = ["ontouchstart", "ontouchend", "ontouchmove", "o
ntouchcancel"]; | |
66 var recepients = [window.__proto__, document.__proto__]; | |
67 for (var i = 0; i < touchEvents.length; ++i) { | |
68 for (var j = 0; j < recepients.length; ++j) { | |
69 if (!(touchEvents[i] in recepients[j])) | |
70 Object.defineProperty(recepients[j], touchEvents[i], { v
alue: null, writable: true, configurable: true, enumerable: true }); | |
71 } | |
72 } | |
73 }; | |
74 | |
75 var symbol = WebInspector.MultitargetTouchModel._symbol; | |
76 var previous = target[symbol] || {enabled: false, configuration: "mobile
", scriptId: ""}; | |
77 | |
78 if (previous.enabled === current.enabled && (!current.enabled || previou
s.configuration === current.configuration)) | |
79 return; | |
80 | |
81 if (previous.scriptId) { | |
82 target.pageAgent().removeScriptToEvaluateOnLoad(previous.scriptId); | |
83 target[symbol].scriptId = ""; | |
84 } | |
85 | |
86 target[symbol] = current; | |
87 target[symbol].scriptId = ""; | |
88 | |
89 if (current.enabled) | |
90 target.pageAgent().addScriptToEvaluateOnLoad("(" + injectedFunction.
toString() + ")()", scriptAddedCallback); | |
91 | |
92 /** | |
93 * @param {?Protocol.Error} error | |
94 * @param {string} scriptId | |
95 */ | |
96 function scriptAddedCallback(error, scriptId) | |
97 { | |
98 (target[symbol] || {}).scriptId = error ? "" : scriptId; | |
99 } | |
100 | |
101 target.emulationAgent().setTouchEmulationEnabled(current.enabled, curren
t.configuration); | |
102 }, | |
103 | |
104 /** | |
105 * @param {!WebInspector.Event} event | |
106 */ | |
107 _inspectModeToggled: function(event) | |
108 { | |
109 var domModel = /** @type {!WebInspector.DOMModel} */ (event.target); | |
110 this._applyToTarget(domModel.target()); | |
111 }, | |
112 | |
113 /** | |
114 * @override | |
115 * @param {!WebInspector.Target} target | |
116 */ | |
117 targetAdded: function(target) | |
118 { | |
119 var domModel = WebInspector.DOMModel.fromTarget(target); | |
120 if (domModel) | |
121 domModel.addEventListener(WebInspector.DOMModel.Events.InspectModeWi
llBeToggled, this._inspectModeToggled, this); | |
122 this._applyToTarget(target); | |
123 }, | |
124 | |
125 /** | |
126 * @override | |
127 * @param {!WebInspector.Target} target | |
128 */ | |
129 targetRemoved: function(target) | |
130 { | |
131 var domModel = WebInspector.DOMModel.fromTarget(target); | |
132 if (domModel) | |
133 domModel.removeEventListener(WebInspector.DOMModel.Events.InspectMod
eWillBeToggled, this._inspectModeToggled, this); | |
134 } | |
135 }; | |
136 | |
137 | 137 |
138 /** @type {?WebInspector.MultitargetTouchModel} */ | 138 /** @type {?WebInspector.MultitargetTouchModel} */ |
139 WebInspector.MultitargetTouchModel._instance = null; | 139 WebInspector.MultitargetTouchModel._instance = null; |
140 | 140 |
141 /** | 141 |
142 * @return {!WebInspector.MultitargetTouchModel} | |
143 */ | |
144 WebInspector.MultitargetTouchModel.instance = function() | |
145 { | |
146 if (!WebInspector.MultitargetTouchModel._instance) | |
147 WebInspector.MultitargetTouchModel._instance = new WebInspector.Multitar
getTouchModel(); | |
148 return /** @type {!WebInspector.MultitargetTouchModel} */ (WebInspector.Mult
itargetTouchModel._instance); | |
149 }; | |
OLD | NEW |