1 // -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const BUNDLE_URL = "chrome://global/locale/viewSource.properties";
10 * ViewSourcePageParent manages the view source <browser> from the chrome side.
12 export class ViewSourcePageParent extends JSWindowActorParent {
17 * Holds the value of the last line found via the "Go to line"
18 * command, to pre-populate the prompt the next time it is
21 this.lastLineFound = null;
25 * Anything added to the messages array will get handled here, and should
26 * get dispatched to a specific function for the message name.
28 receiveMessage(message) {
29 let data = message.data;
31 switch (message.name) {
32 case "ViewSource:PromptAndGoToLine":
33 this.promptAndGoToLine();
35 case "ViewSource:GoToLine:Success":
36 this.onGoToLineSuccess(data.lineNumber);
38 case "ViewSource:GoToLine:Failed":
39 this.onGoToLineFailed();
41 case "ViewSource:StoreWrapping":
42 this.storeWrapping(data.state);
44 case "ViewSource:StoreSyntaxHighlighting":
45 this.storeSyntaxHighlighting(data.state);
51 * A getter for the view source string bundle.
57 return (this._bundle = Services.strings.createBundle(BUNDLE_URL));
61 * Opens the "Go to line" prompt for a user to hop to a particular line
62 * of the source code they're viewing. This will keep prompting until the
63 * user either cancels out of the prompt, or enters a valid line number.
66 let input = { value: this.lastLineFound };
67 let window = Services.wm.getMostRecentWindow(null);
69 let ok = Services.prompt.prompt(
71 this.bundle.GetStringFromName("goToLineTitle"),
72 this.bundle.GetStringFromName("goToLineText"),
82 let line = parseInt(input.value, 10);
85 Services.prompt.alert(
87 this.bundle.GetStringFromName("invalidInputTitle"),
88 this.bundle.GetStringFromName("invalidInputText")
90 this.promptAndGoToLine();
97 * Go to a particular line of the source code. This act is asynchronous.
100 * The line number to try to go to to.
102 goToLine(lineNumber) {
103 this.sendAsyncMessage("ViewSource:GoToLine", { lineNumber });
107 * Called when the frame script reports that a line was successfully gotten
111 * The line number that we successfully got to.
113 onGoToLineSuccess(lineNumber) {
114 // We'll pre-populate the "Go to line" prompt with this value the next
116 this.lastLineFound = lineNumber;
120 * Called when the child reports that we failed to go to a particular
121 * line. This informs the user that their selection was likely out of range,
122 * and then reprompts the user to try again.
125 let window = Services.wm.getMostRecentWindow(null);
126 Services.prompt.alert(
128 this.bundle.GetStringFromName("outOfRangeTitle"),
129 this.bundle.GetStringFromName("outOfRangeText")
131 this.promptAndGoToLine();
135 * @return {boolean} the wrapping state
138 return this.sendQuery("ViewSource:IsWrapping");
142 * @return {boolean} the syntax highlighting state
144 queryIsSyntaxHighlighting() {
145 return this.sendQuery("ViewSource:IsSyntaxHighlighting");
149 * Update the wrapping pref based on the child's current state.
151 * Whether wrapping is currently enabled in the child.
153 storeWrapping(state) {
154 Services.prefs.setBoolPref("view_source.wrap_long_lines", state);
158 * Update the syntax highlighting pref based on the child's current state.
160 * Whether syntax highlighting is currently enabled in the child.
162 storeSyntaxHighlighting(state) {
163 Services.prefs.setBoolPref("view_source.syntax_highlight", state);