Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / front_end / main / FrontendWebSocketAPI.js
blobbbe8c1c17cfce2f6208ed7e30c4774e713549afd
1 // Copyright 2015 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.
5 /**
6 * @constructor
7 * @implements {WebInspector.Linkifier.LinkHandler}
8 */
9 WebInspector.FrontendWebSocketAPI = function()
11 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.DispatchFrontendAPIMessage, this._onFrontendAPIMessage, this);
12 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FrontendAPIAttached, this._onAttach, this);
13 InspectorFrontendHost.events.addEventListener(InspectorFrontendHostAPI.Events.FrontendAPIDetached, this._onDetach, this);
16 WebInspector.FrontendWebSocketAPI.prototype = {
17 _onAttach: function()
19 WebInspector.workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeContentCommitted, this._workingCopyCommitted, this);
20 WebInspector.workspace.addEventListener(WebInspector.Workspace.Events.UISourceCodeWorkingCopyChanged, this._workingCopyChanged, this);
21 WebInspector.Linkifier.setLinkHandler(this);
24 _onDetach: function()
26 WebInspector.workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeContentCommitted, this._workingCopyCommitted, this);
27 WebInspector.workspace.removeEventListener(WebInspector.Workspace.Events.UISourceCodeWorkingCopyChanged, this._workingCopyChanged, this);
28 WebInspector.Linkifier.setLinkHandler(null);
31 /**
32 * @override
33 * @param {string} url
34 * @param {number=} lineNumber
35 * @return {boolean}
37 handleLink: function(url, lineNumber)
39 var uiSourceCode = WebInspector.networkMapping.uiSourceCodeForURLForAnyTarget(url);
40 if (uiSourceCode)
41 url = uiSourceCode.originURL();
42 if (url.startsWith("file://")) {
43 var file = url.substring(7);
44 this._issueFrontendAPINotification("Frontend.revealLocation", { file: file, line: lineNumber });
45 return true;
47 return false;
50 /**
51 * @param {!WebInspector.Event} event
53 _onFrontendAPIMessage: function(event)
55 var message = JSON.parse(/** @type {string} */ (event.data));
56 this._dispatchFrontendAPIMessage(message["id"], message["method"], message["params"] || null);
59 /**
60 * @param {number} id
61 * @param {string} method
62 * @param {?Object} params
64 _dispatchFrontendAPIMessage: function(id, method, params)
66 this._dispatchingFrontendMessage = true;
67 switch (method) {
68 case "Frontend.updateBuffer":
69 var file = params["file"];
70 var buffer = params["buffer"];
71 var saved = params["saved"];
72 var uiSourceCode = WebInspector.workspace.filesystemUISourceCode("file://" + file);
73 if (uiSourceCode) {
74 if (buffer !== uiSourceCode.workingCopy())
75 uiSourceCode.setWorkingCopy(buffer);
76 if (saved)
77 uiSourceCode.checkContentUpdated();
79 this._issueResponse(id);
80 break;
81 default:
82 WebInspector.console.log("Unhandled API message: " + method);
84 this._dispatchingFrontendMessage = false;
87 /**
88 * @param {!WebInspector.Event} event
89 * @param {boolean=} saved
91 _workingCopyChanged: function(event, saved)
93 if (this._dispatchingFrontendMessage)
94 return;
95 var uiSourceCode = /** @type {!WebInspector.UISourceCode} */ (event.data["uiSourceCode"]);
96 var url = uiSourceCode.originURL();
97 if (url.startsWith("file://"))
98 url = url.substring(7);
99 var params = { file: url, buffer: uiSourceCode.workingCopy() };
100 if (saved)
101 params.saved = true;
102 this._issueFrontendAPINotification("Frontend.bufferUpdated", params);
106 * @param {!WebInspector.Event} event
108 _workingCopyCommitted: function(event)
110 this._workingCopyChanged(event, true);
114 * @param {number} id
115 * @param {!Object=} params
117 _issueResponse: function(id, params)
119 var object = {id: id};
120 if (params)
121 object.params = params;
122 InspectorFrontendHost.sendFrontendAPINotification(JSON.stringify(object));
126 * @param {string} method
127 * @param {?Object} params
129 _issueFrontendAPINotification: function(method, params)
131 InspectorFrontendHost.sendFrontendAPINotification(JSON.stringify({ method: method, params: params }));