Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / toolkit / actors / ViewSourcePageParent.sys.mjs
blobcc45b248cc24af3e61e6ec4cb5e1cad8b1223215
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";
9 /**
10  * ViewSourcePageParent manages the view source <browser> from the chrome side.
11  */
12 export class ViewSourcePageParent extends JSWindowActorParent {
13   constructor() {
14     super();
16     /**
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
19      * opened.
20      */
21     this.lastLineFound = null;
22   }
24   /**
25    * Anything added to the messages array will get handled here, and should
26    * get dispatched to a specific function for the message name.
27    */
28   receiveMessage(message) {
29     let data = message.data;
31     switch (message.name) {
32       case "ViewSource:PromptAndGoToLine":
33         this.promptAndGoToLine();
34         break;
35       case "ViewSource:GoToLine:Success":
36         this.onGoToLineSuccess(data.lineNumber);
37         break;
38       case "ViewSource:GoToLine:Failed":
39         this.onGoToLineFailed();
40         break;
41       case "ViewSource:StoreWrapping":
42         this.storeWrapping(data.state);
43         break;
44       case "ViewSource:StoreSyntaxHighlighting":
45         this.storeSyntaxHighlighting(data.state);
46         break;
47     }
48   }
50   /**
51    * A getter for the view source string bundle.
52    */
53   get bundle() {
54     if (this._bundle) {
55       return this._bundle;
56     }
57     return (this._bundle = Services.strings.createBundle(BUNDLE_URL));
58   }
60   /**
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.
64    */
65   promptAndGoToLine() {
66     let input = { value: this.lastLineFound };
67     let window = Services.wm.getMostRecentWindow(null);
69     let ok = Services.prompt.prompt(
70       window,
71       this.bundle.GetStringFromName("goToLineTitle"),
72       this.bundle.GetStringFromName("goToLineText"),
73       input,
74       null,
75       { value: 0 }
76     );
78     if (!ok) {
79       return;
80     }
82     let line = parseInt(input.value, 10);
84     if (!(line > 0)) {
85       Services.prompt.alert(
86         window,
87         this.bundle.GetStringFromName("invalidInputTitle"),
88         this.bundle.GetStringFromName("invalidInputText")
89       );
90       this.promptAndGoToLine();
91     } else {
92       this.goToLine(line);
93     }
94   }
96   /**
97    * Go to a particular line of the source code. This act is asynchronous.
98    *
99    * @param lineNumber
100    *        The line number to try to go to to.
101    */
102   goToLine(lineNumber) {
103     this.sendAsyncMessage("ViewSource:GoToLine", { lineNumber });
104   }
106   /**
107    * Called when the frame script reports that a line was successfully gotten
108    * to.
109    *
110    * @param lineNumber
111    *        The line number that we successfully got to.
112    */
113   onGoToLineSuccess(lineNumber) {
114     // We'll pre-populate the "Go to line" prompt with this value the next
115     // time it comes up.
116     this.lastLineFound = lineNumber;
117   }
119   /**
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.
123    */
124   onGoToLineFailed() {
125     let window = Services.wm.getMostRecentWindow(null);
126     Services.prompt.alert(
127       window,
128       this.bundle.GetStringFromName("outOfRangeTitle"),
129       this.bundle.GetStringFromName("outOfRangeText")
130     );
131     this.promptAndGoToLine();
132   }
134   /**
135    * @return {boolean} the wrapping state
136    */
137   queryIsWrapping() {
138     return this.sendQuery("ViewSource:IsWrapping");
139   }
141   /**
142    * @return {boolean} the syntax highlighting state
143    */
144   queryIsSyntaxHighlighting() {
145     return this.sendQuery("ViewSource:IsSyntaxHighlighting");
146   }
148   /**
149    * Update the wrapping pref based on the child's current state.
150    * @param state
151    *        Whether wrapping is currently enabled in the child.
152    */
153   storeWrapping(state) {
154     Services.prefs.setBoolPref("view_source.wrap_long_lines", state);
155   }
157   /**
158    * Update the syntax highlighting pref based on the child's current state.
159    * @param state
160    *        Whether syntax highlighting is currently enabled in the child.
161    */
162   storeSyntaxHighlighting(state) {
163     Services.prefs.setBoolPref("view_source.syntax_highlight", state);
164   }