Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 var initialize_EmptyPageMock = function() { | |
| 2 | |
| 3 var id = 0; | |
| 4 | |
| 5 function nextId(prefix) { | |
| 6 return (prefix || '') + (++id); | |
| 7 } | |
| 8 | |
| 9 InspectorTest.connectToPage = function(targetName, pageMock, makeMainTarget) { | |
| 10 var mockTarget = SDK.targetManager.createTarget(nextId('mock-target-'), targ etName, pageMock.capabilities(), params => pageMock.createConnection(params)); | |
| 11 if (makeMainTarget) { | |
| 12 SDK.targetManager._targets = SDK.targetManager._targets.filter(target => target !== mockTarget); | |
| 13 SDK.targetManager._targets.unshift(mockTarget); | |
| 14 } | |
| 15 return mockTarget; | |
| 16 } | |
| 17 | |
| 18 InspectorTest.PageMock = class { | |
| 19 constructor(url) { | |
| 20 this._url = url; | |
| 21 this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.J S | SDK.Target.Capability.Browser; | |
| 22 /** @type {!Set<string>} */ | |
| 23 this._enabledDomains = new Set(); | |
| 24 | |
| 25 this._mainFrame = { | |
| 26 id: nextId(), | |
| 27 loaderId: nextId(), | |
| 28 mimeType: 'text/html', | |
| 29 securityOrigin: this._url, | |
| 30 url: this._url | |
| 31 }; | |
| 32 | |
| 33 this._executionContexts = []; | |
| 34 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false)); | |
| 35 | |
| 36 this._scripts = []; | |
| 37 this._scriptContents = new Map(); | |
| 38 | |
| 39 this._dispatchMap = { | |
| 40 'Debugger.enable': this._debuggerEnable, | |
| 41 'Debugger.getScriptSource': this._debuggergetScriptSource, | |
|
dgozman
2017/03/16 21:12:56
typo in method name
lushnikov
2017/03/16 21:19:13
Done.
| |
| 42 'Debugger.setBlackboxPatterns': (id, params) => this._sendResponse(i d, {}), | |
| 43 'Runtime.enable': this._runtimeEnable, | |
| 44 'Page.enable': this._pageEnable, | |
| 45 'Page.getResourceTree': this._pageGetResourceTree, | |
| 46 }; | |
| 47 } | |
| 48 | |
| 49 capabilities() { | |
| 50 return this._capabilities; | |
| 51 } | |
| 52 | |
| 53 disableDOMCapability() { | |
| 54 this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM); | |
| 55 } | |
| 56 | |
| 57 createConnection(params) { | |
| 58 this._enabledDomains.clear(); | |
| 59 this._connection = new MockPageConnection(this, params); | |
| 60 return this._connection; | |
| 61 } | |
| 62 | |
| 63 evalScript(url, content, isContentScript) { | |
| 64 var id = nextId(); | |
| 65 content += '\n//# sourceURL=' + url; | |
| 66 this._scriptContents.set(id, content); | |
| 67 | |
| 68 var context = this._executionContexts.find(context => context.auxData.is Default !== isContentScript); | |
| 69 if (!context) { | |
| 70 context = this._createExecutionContext(this._mainFrame, isContentScr ipt); | |
| 71 this._fireEvent('Runtime.executionContextCreated', {context: context }); | |
| 72 } | |
| 73 | |
| 74 var text = new Common.Text(content); | |
| 75 var script = { | |
| 76 scriptId: id, | |
| 77 url: url, | |
| 78 startLine: 0, | |
| 79 startColumn: 0, | |
| 80 endLine: text.lineCount(), | |
| 81 endColumn: text.lineAt(text.lineCount()).length - 1, | |
| 82 executionContextId: context.id, | |
| 83 hash: String.hashCode(content), | |
| 84 executionContextAuxData: context.auxData, | |
| 85 sourceMapURL: '', | |
| 86 hasSourceURL: true, | |
| 87 isLiveEdit: false, | |
| 88 isModule: false, | |
| 89 length: content.length | |
| 90 }; | |
| 91 this._scripts.push(script); | |
| 92 this._fireEvent('Debugger.scriptParsed', script); | |
| 93 } | |
| 94 | |
| 95 reload() { | |
| 96 this._fireEvent('Page.frameStartedLoading', {frameId: this._mainFrame.id }); | |
| 97 for (var context of this._executionContexts) | |
| 98 this._fireEvent('Runtime.executionContextDestroyed', {executionConte xtId: context.id}); | |
| 99 this._scripts = []; | |
| 100 this._scriptContents.clear(); | |
| 101 this._executionContexts = []; | |
| 102 this._fireEvent('Runtime.executionContextsCleared', {}); | |
| 103 this._executionContexts.push(this._createExecutionContext(this._mainFram e, false)); | |
| 104 for (var context of this._executionContexts) | |
| 105 this._fireEvent('Runtime.executionContextCreated', {context: context }); | |
| 106 | |
| 107 this._fireEvent('Page.frameNavigated', {frame: this._mainFrame}); | |
| 108 this._fireEvent('Page.loadEventFired', {timestamp: Date.now() / 1000}); | |
| 109 this._fireEvent('Page.frameStoppedLoading', {frameId: this._mainFrame.id }); | |
| 110 this._fireEvent('Page.domContentEventFired', {timestamp: Date.now() / 10 00}); | |
| 111 } | |
| 112 | |
| 113 close() { | |
| 114 if (this._connection) { | |
| 115 this._connection.disconnect(); | |
| 116 this._connection = null; | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 _createExecutionContext(frame, isContentScript) { | |
| 121 return { | |
| 122 id: nextId(), | |
| 123 auxData: {isDefault: !isContentScript, frameId: frame.id }, | |
| 124 origin: frame.securityOrigin, | |
| 125 name: '', | |
| 126 }; | |
| 127 } | |
| 128 | |
| 129 // ------------------------------------------------------------------------- | |
| 130 // Command Handlers | |
| 131 // ------------------------------------------------------------------------- | |
| 132 | |
| 133 _debuggerEnable(id, params) { | |
| 134 this._enabledDomains.add('Debugger'); | |
| 135 this._sendResponse(id, {}); | |
| 136 for (var script of this._scripts) | |
| 137 this._fireEvent('Debugger.scriptParsed', script); | |
| 138 } | |
| 139 | |
| 140 _debuggerGetScriptSource(id, params) { | |
| 141 if (!this._scriptContents.has(params.scriptId)) { | |
| 142 this._sendResponse(id, undefined, { | |
| 143 message: 'Can\'t get script content for id ' + params.scriptId, | |
| 144 code: 1, | |
| 145 }); | |
| 146 return; | |
| 147 } | |
| 148 var result = {scriptSource: this._scriptContents.get(params.scriptId)}; | |
| 149 this._sendResponse(id, result); | |
| 150 } | |
| 151 | |
| 152 _runtimeEnable(id, params) { | |
| 153 this._enabledDomains.add('Runtime'); | |
| 154 this._sendResponse(id, {}); | |
| 155 for (var context of this._executionContexts) | |
| 156 this._fireEvent('Runtime.executionContextCreated', {context: context }); | |
| 157 } | |
| 158 | |
| 159 _pageEnable(id, params) { | |
| 160 this._enabledDomains.add('Page'); | |
| 161 this._sendResponse(id, {}); | |
| 162 } | |
| 163 | |
| 164 _pageGetResourceTree(id, params) { | |
| 165 var result = { | |
| 166 frameTree: { | |
| 167 frame: this._mainFrame, | |
| 168 resources: [] | |
| 169 } | |
| 170 } | |
| 171 this._sendResponse(id, result); | |
| 172 } | |
| 173 | |
| 174 _isSupportedDomain(methodName) { | |
| 175 var domain = methodName.split('.')[0]; | |
| 176 if (domain === 'Page') | |
| 177 return !!(this._capabilities & SDK.Target.Capability.DOM); | |
| 178 return true; | |
| 179 } | |
| 180 | |
| 181 _dispatch(id, methodName, params, message) { | |
| 182 var handler = this._isSupportedDomain(methodName) ? this._dispatchMap[me thodName] : null; | |
| 183 if (handler) | |
| 184 return handler.call(this, id, params); | |
| 185 this._sendResponse(id, undefined, { | |
| 186 message: 'Can\'t handle command ' + methodName, | |
| 187 code: Protocol.InspectorBackend.DevToolsStubErrorCode, | |
| 188 }); | |
| 189 } | |
| 190 | |
| 191 _sendResponse(id, result, error) { | |
| 192 var message = { | |
| 193 id: id, | |
| 194 result: result, | |
| 195 error: error | |
| 196 }; | |
| 197 this._connection.sendMessageToDevTools(message); | |
| 198 } | |
| 199 | |
| 200 _fireEvent(methodName, params) { | |
| 201 var domain = methodName.split('.')[0]; | |
| 202 // Non-enabled domains can't send events. | |
| 203 if (!this._enabledDomains.has(domain)) | |
|
dgozman
2017/03/16 21:12:56
Use _isSupportedDomain instead.
lushnikov
2017/03/16 21:19:13
That's not needed since non-supported domain can't
| |
| 204 return; | |
| 205 var message = { | |
| 206 method: methodName, | |
| 207 params: params | |
| 208 }; | |
| 209 this._connection.sendMessageToDevTools(message); | |
| 210 } | |
| 211 } | |
| 212 | |
| 213 var MockPageConnection = class { | |
| 214 /** | |
| 215 * @param {!Protocol.InspectorBackend.Connection.Params} params | |
| 216 */ | |
| 217 constructor(page, params) { | |
| 218 this._page = page; | |
| 219 this._onMessage = params.onMessage; | |
| 220 this._onDisconnect = params.onDisconnect; | |
| 221 } | |
| 222 | |
| 223 sendMessageToDevTools(message) { | |
| 224 setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0) ; | |
| 225 } | |
| 226 | |
| 227 /** | |
| 228 * @override | |
| 229 * @param {string} message | |
| 230 */ | |
| 231 sendMessage(message) { | |
| 232 var json = JSON.parse(message); | |
| 233 this._page._dispatch(json.id, json.method, json.params, message); | |
| 234 } | |
| 235 | |
| 236 /** | |
| 237 * @override | |
| 238 * @return {!Promise} | |
| 239 */ | |
| 240 disconnect() { | |
| 241 this._onDisconnect.call(null, 'force disconnect'); | |
| 242 this._onDisconnect = null; | |
| 243 this._onMessage = null; | |
| 244 return Promise.resolve(); | |
| 245 } | |
| 246 }; | |
| 247 | |
| 248 }; | |
| 249 | |
| OLD | NEW |