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) { | |
| 10 return SDK.targetManager.createTarget(nextId("mock-target-"), targetName, pa geMock.capabilities(), params => pageMock.createConnection(params)); | |
| 11 } | |
| 12 | |
| 13 InspectorTest.PageMock = class { | |
| 14 constructor(url) { | |
| 15 this._url = url; | |
| 16 this._capabilities = SDK.Target.Capability.DOM | SDK.Target.Capability.J S | SDK.Target.Capability.Browser; | |
| 17 | |
| 18 this._mainFrame = { | |
| 19 id: nextId(), | |
| 20 loaderId: nextId(), | |
| 21 mimeType: "text/html", | |
| 22 securityOrigin: this._url, | |
| 23 url: this._url | |
| 24 }; | |
| 25 | |
| 26 this._createExecutionContext(); | |
| 27 | |
| 28 this._debuggerEnabled = false; | |
| 29 this._scripts = []; | |
| 30 this._scriptContents = new Map(); | |
| 31 | |
| 32 this._enablePageAgent = true; | |
| 33 this._initializeDispatchMap(); | |
| 34 } | |
| 35 | |
| 36 capabilities() { | |
| 37 return this._capabilities; | |
| 38 } | |
| 39 | |
| 40 disableDOMCapability() { | |
| 41 this._capabilities = this._capabilities & (~SDK.Target.Capability.DOM); | |
| 42 this._initializeDispatchMap(); | |
| 43 } | |
| 44 | |
| 45 _hasDOMCapability() { | |
| 46 return !!(this._capabilities & SDK.Target.Capability.DOM); | |
| 47 } | |
| 48 | |
| 49 _initializeDispatchMap() { | |
| 50 this._dispatchMap = { | |
| 51 "Debugger.enable": this._enableDebugger, | |
| 52 "Debugger.getScriptSource": this._getScriptSource, | |
| 53 "Debugger.setBlackboxPatterns": (id, params) => this._sendResponse(i d, {}), | |
| 54 "Runtime.enable": this._enableRuntime, | |
| 55 }; | |
| 56 if (this._hasDOMCapability()) { | |
| 57 Object.assign(this._dispatchMap, { | |
| 58 "Page.enable": (id, params) => this._sendResponse(id, {}), | |
| 59 "Page.getResourceTree": this._getResourceTree, | |
| 60 }); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 createConnection(params) { | |
| 65 this._connection = new MockPageConnection(this, params); | |
| 66 return this._connection; | |
| 67 } | |
| 68 | |
| 69 addScript(url, content, hasSourceURL, isContentScript) { | |
|
dgozman
2017/03/16 00:17:10
Note that content scripts must have non-default ex
lushnikov
2017/03/16 06:30:49
Done.
| |
| 70 url = hasSourceURL ? url : Common.ParsedURL.completeURL(this._url, url); | |
| 71 var id = nextId(); | |
| 72 this._scriptContents.set(id, content); | |
| 73 var auxData = isContentScript ? {isDefault: false} : undefined; | |
|
dgozman
2017/03/16 00:17:10
this._executionContext.auxData
lushnikov
2017/03/16 06:30:50
Done.
| |
| 74 var script = { | |
| 75 scriptId: id, | |
| 76 url: url, | |
| 77 startLine: 0, | |
| 78 startColumn: 0, | |
| 79 endLine: 0, | |
|
dgozman
2017/03/16 00:17:09
Let's call lineEndings on it and pass true endLine
lushnikov
2017/03/16 06:30:49
Done.
| |
| 80 endColumn: content.length, | |
| 81 executionContextId: this._executionContext.id, | |
| 82 hash: String.hashCode(content), | |
| 83 executionContextAuxData: auxData, | |
| 84 sourceMapURL: "", | |
| 85 hasSourceURL: hasSourceURL, | |
| 86 isLiveEdit: false, | |
| 87 isModule: false, | |
| 88 length: content.length | |
| 89 }; | |
| 90 this._scripts.push(script); | |
| 91 if (this._debuggerEnabled) | |
| 92 this._fireEvent("Debugger.scriptParsed", script); | |
| 93 } | |
| 94 | |
| 95 reload() { | |
| 96 this._fireEvent("Runtime.executionContextDestroyed", {executionContextId : this._executionContext.id}); | |
|
dgozman
2017/03/16 00:17:09
I believe this one is not required.
lushnikov
2017/03/16 06:30:49
We currently send it whenever the page gets reload
| |
| 97 this._scripts = []; | |
| 98 this._scriptContents.clear(); | |
| 99 this._fireEvent("Runtime.executionContextsCleared", {}); | |
| 100 this._createExecutionContext(); | |
| 101 this._fireEvent("Runtime.executionContextCreated", {context: this._execu tionContext}); | |
| 102 | |
| 103 if (this._hasDOMCapability()) | |
| 104 this._fireEvent("Page.frameNavigated", {frame: this._mainFrame}); | |
| 105 } | |
| 106 | |
| 107 _createExecutionContext() { | |
| 108 this._executionContext = { | |
| 109 id: nextId(), | |
| 110 auxData: {isDefault: true, frameId: this._mainFrame.id}, | |
| 111 origin: this._mainFrame.securityOrigin, | |
| 112 name: '', | |
| 113 }; | |
| 114 } | |
| 115 | |
| 116 _enableRuntime(id, params) { | |
| 117 this._sendResponse(id, {}); | |
| 118 this._fireEvent("Runtime.executionContextCreated", {context: this._execu tionContext}); | |
| 119 } | |
| 120 | |
| 121 _enableDebugger(id, params) { | |
| 122 this._debuggerEnabled = true; | |
| 123 this._sendResponse(id, {}); | |
| 124 for (var script of this._scripts) | |
| 125 this._fireEvent("Debugger.scriptParsed", script); | |
| 126 } | |
| 127 | |
| 128 _getResourceTree(id, params) { | |
| 129 var result = { | |
| 130 frameTree: { | |
| 131 frame: this._mainFrame, | |
| 132 resources: [] | |
|
dgozman
2017/03/16 00:17:09
Should we list all scripts here?
lushnikov
2017/03/16 06:30:49
I consider those to be evals for simplicity; renam
| |
| 133 } | |
| 134 } | |
| 135 this._sendResponse(id, result); | |
| 136 } | |
| 137 | |
| 138 _getScriptSource(id, params) { | |
| 139 if (!this._scriptContents.has(params.scriptId)) { | |
| 140 this._sendResponse(id, undefined, { | |
| 141 message: 'Can\'t get script content for id ' + params.scriptId, | |
| 142 code: 1, | |
| 143 }); | |
| 144 return; | |
| 145 } | |
| 146 var result = {scriptSource: this._scriptContents.get(params.scriptId)}; | |
| 147 this._sendResponse(id, result); | |
| 148 } | |
| 149 | |
| 150 _dispatch(id, methodName, params, message) { | |
| 151 var handler = this._dispatchMap[methodName]; | |
| 152 if (handler) | |
| 153 return handler.call(this, id, params); | |
| 154 this._sendResponse(id, undefined, { | |
| 155 message: 'Can\'t handle command ' + methodName, | |
| 156 code: Protocol.InspectorBackend.DevToolsStubErrorCode, | |
| 157 }); | |
| 158 } | |
| 159 | |
| 160 _sendResponse(id, result, error) { | |
| 161 var message = { | |
| 162 id: id, | |
| 163 result: result, | |
| 164 error: error | |
| 165 }; | |
| 166 this._connection.sendMessageToDevTools(message); | |
| 167 } | |
| 168 | |
| 169 _fireEvent(methodName, params) { | |
| 170 var message = { | |
| 171 method: methodName, | |
| 172 params: params | |
| 173 }; | |
| 174 this._connection.sendMessageToDevTools(message); | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 var MockPageConnection = class { | |
| 179 /** | |
| 180 * @param {!Protocol.InspectorBackend.Connection.Params} params | |
| 181 */ | |
| 182 constructor(page, params) { | |
| 183 this._page = page; | |
| 184 this._onMessage = params.onMessage; | |
| 185 this._onDisconnect = params.onDisconnect; | |
| 186 } | |
| 187 | |
| 188 sendMessageToDevTools(message) { | |
| 189 setTimeout(() => this._onMessage.call(null, JSON.stringify(message)), 0) ; | |
| 190 } | |
| 191 | |
| 192 /** | |
| 193 * @override | |
| 194 * @param {string} message | |
| 195 */ | |
| 196 sendMessage(message) { | |
| 197 var json = JSON.parse(message); | |
| 198 this._page._dispatch(json.id, json.method, json.params, message); | |
| 199 } | |
| 200 | |
| 201 /** | |
| 202 * @override | |
| 203 * @return {!Promise} | |
| 204 */ | |
| 205 disconnect() { | |
| 206 this._onDisconnect.call(null, 'force disconnect'); | |
| 207 this._onDisconnect = null; | |
| 208 this._onMessage = null; | |
| 209 return Promise.resolve(); | |
| 210 } | |
| 211 }; | |
| 212 | |
| 213 }; | |
| 214 | |
| OLD | NEW |