OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 /** | |
5 * @implements {SDK.TargetManager.Observer} | |
6 * @unrestricted | |
7 */ | |
8 Emulation.MultitargetTouchModel = class { | |
9 constructor() { | |
10 this._touchEnabled = false; | |
11 this._touchMobile = false; | |
12 this._customTouchEnabled = false; | |
13 | |
14 SDK.targetManager.observeTargets(this, SDK.Target.Capability.TouchEmulation)
; | |
15 } | |
16 | |
17 /** | |
18 * @return {!Emulation.MultitargetTouchModel} | |
19 */ | |
20 static instance() { | |
21 if (!Emulation.MultitargetTouchModel._instance) | |
22 Emulation.MultitargetTouchModel._instance = new Emulation.MultitargetTouch
Model(); | |
23 return /** @type {!Emulation.MultitargetTouchModel} */ (Emulation.Multitarge
tTouchModel._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 SDK.targetManager.targets(SDK.Target.Capability.TouchEmul
ation)) | |
46 this._applyToTarget(target); | |
47 } | |
48 | |
49 /** | |
50 * @param {!SDK.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 overlayModel = target.model(SDK.OverlayModel); | |
58 var inspectModeEnabled = overlayModel ? overlayModel.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 | |
78 var symbol = Emulation.MultitargetTouchModel._symbol; | |
79 var previous = target[symbol] || {enabled: false, configuration: 'mobile', s
criptId: ''}; | |
80 | |
81 if (previous.enabled === current.enabled && (!current.enabled || previous.co
nfiguration === current.configuration)) | |
82 return; | |
83 | |
84 if (previous.scriptId) { | |
85 target.pageAgent().removeScriptToEvaluateOnLoad(previous.scriptId); | |
86 target[symbol].scriptId = ''; | |
87 } | |
88 | |
89 target[symbol] = current; | |
90 target[symbol].scriptId = ''; | |
91 | |
92 if (current.enabled) | |
93 target.pageAgent().addScriptToEvaluateOnLoad('(' + injectedFunction.toStri
ng() + ')()', scriptAddedCallback); | |
94 | |
95 /** | |
96 * @param {?Protocol.Error} error | |
97 * @param {string} scriptId | |
98 */ | |
99 function scriptAddedCallback(error, scriptId) { | |
100 (target[symbol] || {}).scriptId = error ? '' : scriptId; | |
101 } | |
102 | |
103 target.emulationAgent().setTouchEmulationEnabled(current.enabled, current.co
nfiguration); | |
104 } | |
105 | |
106 /** | |
107 * @param {!Common.Event} event | |
108 */ | |
109 _inspectModeToggled(event) { | |
110 var overlayModel = /** @type {!SDK.OverlayModel} */ (event.data); | |
111 this._applyToTarget(overlayModel.target()); | |
112 } | |
113 | |
114 /** | |
115 * @override | |
116 * @param {!SDK.Target} target | |
117 */ | |
118 targetAdded(target) { | |
119 var overlayModel = target.model(SDK.OverlayModel); | |
120 if (overlayModel) | |
121 overlayModel.addEventListener(SDK.OverlayModel.Events.InspectModeWillBeTog
gled, this._inspectModeToggled, this); | |
122 this._applyToTarget(target); | |
123 } | |
124 | |
125 /** | |
126 * @override | |
127 * @param {!SDK.Target} target | |
128 */ | |
129 targetRemoved(target) { | |
130 var overlayModel = target.model(SDK.OverlayModel); | |
131 if (overlayModel) { | |
132 overlayModel.removeEventListener( | |
133 SDK.OverlayModel.Events.InspectModeWillBeToggled, this._inspectModeTog
gled, this); | |
134 } | |
135 } | |
136 }; | |
137 | |
138 Emulation.MultitargetTouchModel._symbol = Symbol('MultitargetTouchModel.symbol')
; | |
139 | |
140 /** @type {?Emulation.MultitargetTouchModel} */ | |
141 Emulation.MultitargetTouchModel._instance = null; | |
OLD | NEW |