OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview 'settings-languages' handles Chrome's language and input | 6 * @fileoverview 'settings-languages' handles Chrome's language and input |
7 * method settings. The 'languages' property, which reflects the current | 7 * method settings. The 'languages' property, which reflects the current |
8 * language settings, must not be changed directly. Instead, changes to | 8 * language settings, must not be changed directly. Instead, changes to |
9 * language settings should be made using the LanguageHelper APIs provided by | 9 * language settings should be made using the LanguageHelper APIs provided by |
10 * this class via languageHelper. | 10 * this class via languageHelper. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 * PromiseResolver to be resolved when the singleton has been initialized. | 71 * PromiseResolver to be resolved when the singleton has been initialized. |
72 * @private {!PromiseResolver} | 72 * @private {!PromiseResolver} |
73 */ | 73 */ |
74 resolver_: { | 74 resolver_: { |
75 type: Object, | 75 type: Object, |
76 value: function() { | 76 value: function() { |
77 return new PromiseResolver(); | 77 return new PromiseResolver(); |
78 }, | 78 }, |
79 }, | 79 }, |
80 | 80 |
81 /** @type {!LanguageSettingsPrivate} */ | |
82 languageSettingsPrivate: Object, | |
83 | |
84 /** @type {!InputMethodPrivate} */ | |
85 inputMethodPrivate: Object, | |
86 | |
87 /** | 81 /** |
88 * Hash map of supported languages by language codes for fast lookup. | 82 * Hash map of supported languages by language codes for fast lookup. |
89 * @private {!Map<string, !chrome.languageSettingsPrivate.Language>} | 83 * @private {!Map<string, !chrome.languageSettingsPrivate.Language>} |
90 */ | 84 */ |
91 supportedLanguageMap_: { | 85 supportedLanguageMap_: { |
92 type: Object, | 86 type: Object, |
93 value: function() { return new Map(); }, | 87 value: function() { return new Map(); }, |
94 }, | 88 }, |
95 | 89 |
96 /** | 90 /** |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 // Observe Chrome OS prefs (ignored for non-Chrome OS). | 135 // Observe Chrome OS prefs (ignored for non-Chrome OS). |
142 'updateRemovableLanguages_(' + | 136 'updateRemovableLanguages_(' + |
143 'prefs.settings.language.preload_engines.value, ' + | 137 'prefs.settings.language.preload_engines.value, ' + |
144 'prefs.settings.language.enabled_extension_imes.value, ' + | 138 'prefs.settings.language.enabled_extension_imes.value, ' + |
145 'languages)', | 139 'languages)', |
146 ], | 140 ], |
147 | 141 |
148 /** @private {?Function} */ | 142 /** @private {?Function} */ |
149 boundOnInputMethodChanged_: null, | 143 boundOnInputMethodChanged_: null, |
150 | 144 |
| 145 /** @private {?settings.LanguagesBrowserProxy} */ |
| 146 browserProxy_: null, |
| 147 |
| 148 /** @private {?LanguageSettingsPrivate} */ |
| 149 languageSettingsPrivate_: null, |
| 150 |
| 151 // <if expr="chromeos"> |
| 152 /** @private {?InputMethodPrivate} */ |
| 153 inputMethodPrivate_: null, |
| 154 // </if> |
| 155 |
151 /** @override */ | 156 /** @override */ |
152 attached: function() { | 157 attached: function() { |
153 this.languageSettingsPrivate = | 158 this.browserProxy_ = settings.LanguagesBrowserProxyImpl.getInstance(); |
154 settings.languageSettingsPrivateApiForTest || | 159 this.languageSettingsPrivate_ = |
155 /** @type {!LanguageSettingsPrivate} */(chrome.languageSettingsPrivate); | 160 this.browserProxy_.getLanguageSettingsPrivate(); |
156 | 161 // <if expr="chromeos"> |
157 this.inputMethodPrivate = | 162 this.inputMethodPrivate_ = this.browserProxy_.getInputMethodPrivate(); |
158 settings.inputMethodPrivateApiForTest || | 163 // </if> |
159 /** @type {!InputMethodPrivate} */(chrome.inputMethodPrivate); | |
160 | 164 |
161 var promises = []; | 165 var promises = []; |
162 | 166 |
163 // Wait until prefs are initialized before creating the model, so we can | 167 // Wait until prefs are initialized before creating the model, so we can |
164 // include information about enabled languages. | 168 // include information about enabled languages. |
165 promises[0] = CrSettingsPrefs.initialized; | 169 promises[0] = CrSettingsPrefs.initialized; |
166 | 170 |
167 // Get the language list. | 171 // Get the language list. |
168 promises[1] = new Promise(function(resolve) { | 172 promises[1] = new Promise(function(resolve) { |
169 this.languageSettingsPrivate.getLanguageList(resolve); | 173 this.languageSettingsPrivate_.getLanguageList(resolve); |
170 }.bind(this)); | 174 }.bind(this)); |
171 | 175 |
172 // Get the translate target language. | 176 // Get the translate target language. |
173 promises[2] = new Promise(function(resolve) { | 177 promises[2] = new Promise(function(resolve) { |
174 this.languageSettingsPrivate.getTranslateTargetLanguage(resolve); | 178 this.languageSettingsPrivate_.getTranslateTargetLanguage(resolve); |
175 }.bind(this)); | 179 }.bind(this)); |
176 | 180 |
177 if (cr.isChromeOS) { | 181 if (cr.isChromeOS) { |
178 promises[3] = new Promise(function(resolve) { | 182 promises[3] = new Promise(function(resolve) { |
179 this.languageSettingsPrivate.getInputMethodLists(function(lists) { | 183 this.languageSettingsPrivate_.getInputMethodLists(function(lists) { |
180 resolve(lists.componentExtensionImes.concat( | 184 resolve(lists.componentExtensionImes.concat( |
181 lists.thirdPartyExtensionImes)); | 185 lists.thirdPartyExtensionImes)); |
182 }); | 186 }); |
183 }.bind(this)); | 187 }.bind(this)); |
184 | 188 |
185 promises[4] = new Promise(function(resolve) { | 189 promises[4] = new Promise(function(resolve) { |
186 this.inputMethodPrivate.getCurrentInputMethod(resolve); | 190 this.inputMethodPrivate_.getCurrentInputMethod(resolve); |
187 }.bind(this)); | 191 }.bind(this)); |
188 } | 192 } |
189 | 193 |
190 if (cr.isWindows || cr.isChromeOS) { | 194 if (cr.isWindows || cr.isChromeOS) { |
191 // Fetch the starting UI language, which affects which actions should be | 195 // Fetch the starting UI language, which affects which actions should be |
192 // enabled. | 196 // enabled. |
193 promises.push(cr.sendWithPromise('getProspectiveUILanguage').then( | 197 promises.push(this.browserProxy_.getProspectiveUILanguage().then( |
194 function(prospectiveUILanguage) { | 198 function(prospectiveUILanguage) { |
195 this.originalProspectiveUILanguage_ = | 199 this.originalProspectiveUILanguage_ = |
196 prospectiveUILanguage || window.navigator.language; | 200 prospectiveUILanguage || window.navigator.language; |
197 }.bind(this))); | 201 }.bind(this))); |
198 } | 202 } |
199 | 203 |
200 Promise.all(promises).then(function(results) { | 204 Promise.all(promises).then(function(results) { |
201 if (!this.isConnected) { | 205 if (!this.isConnected) { |
202 // Return early if this element was detached from the DOM before this | 206 // Return early if this element was detached from the DOM before this |
203 // async callback executes (can happen during testing). | 207 // async callback executes (can happen during testing). |
204 return; | 208 return; |
205 } | 209 } |
206 | 210 |
207 // TODO(dpapad): Cleanup this code. It uses results[3] and results[4] | 211 // TODO(dpapad): Cleanup this code. It uses results[3] and results[4] |
208 // which only exist for ChromeOS. | 212 // which only exist for ChromeOS. |
209 this.createModel_(results[1], results[2], results[3], results[4]); | 213 this.createModel_(results[1], results[2], results[3], results[4]); |
210 this.resolver_.resolve(); | 214 this.resolver_.resolve(); |
211 }.bind(this)); | 215 }.bind(this)); |
212 | 216 |
213 if (cr.isChromeOS) { | 217 if (cr.isChromeOS) { |
214 this.boundOnInputMethodChanged_ = this.onInputMethodChanged_.bind(this); | 218 this.boundOnInputMethodChanged_ = this.onInputMethodChanged_.bind(this); |
215 this.inputMethodPrivate.onChanged.addListener( | 219 this.inputMethodPrivate_.onChanged.addListener( |
216 assert(this.boundOnInputMethodChanged_)); | 220 assert(this.boundOnInputMethodChanged_)); |
217 } | 221 } |
218 }, | 222 }, |
219 | 223 |
220 /** @override */ | 224 /** @override */ |
221 detached: function() { | 225 detached: function() { |
222 if (cr.isChromeOS) { | 226 if (cr.isChromeOS) { |
223 this.inputMethodPrivate.onChanged.removeListener( | 227 this.inputMethodPrivate_.onChanged.removeListener( |
224 assert(this.boundOnInputMethodChanged_)); | 228 assert(this.boundOnInputMethodChanged_)); |
225 this.boundOnInputMethodChanged_ = null; | 229 this.boundOnInputMethodChanged_ = null; |
226 } | 230 } |
227 }, | 231 }, |
228 | 232 |
229 /** | 233 /** |
230 * Updates the prospective UI language based on the new pref value. | 234 * Updates the prospective UI language based on the new pref value. |
231 * @param {string} prospectiveUILanguage | 235 * @param {string} prospectiveUILanguage |
232 * @private | 236 * @private |
233 */ | 237 */ |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 return this.resolver_.promise; | 489 return this.resolver_.promise; |
486 }, | 490 }, |
487 | 491 |
488 // <if expr="chromeos or is_win"> | 492 // <if expr="chromeos or is_win"> |
489 /** | 493 /** |
490 * Sets the prospective UI language to the chosen language. This won't affect | 494 * Sets the prospective UI language to the chosen language. This won't affect |
491 * the actual UI language until a restart. | 495 * the actual UI language until a restart. |
492 * @param {string} languageCode | 496 * @param {string} languageCode |
493 */ | 497 */ |
494 setProspectiveUILanguage: function(languageCode) { | 498 setProspectiveUILanguage: function(languageCode) { |
495 chrome.send('setProspectiveUILanguage', [languageCode]); | 499 this.browserProxy_.setProspectiveUILanguage(languageCode); |
496 }, | 500 }, |
497 | 501 |
498 /** | 502 /** |
499 * True if the prospective UI language was changed from its starting value. | 503 * True if the prospective UI language was changed from its starting value. |
500 * @return {boolean} | 504 * @return {boolean} |
501 */ | 505 */ |
502 requiresRestart: function() { | 506 requiresRestart: function() { |
503 return this.originalProspectiveUILanguage_ != | 507 return this.originalProspectiveUILanguage_ != |
504 this.languages.prospectiveUILanguage; | 508 this.languages.prospectiveUILanguage; |
505 }, | 509 }, |
506 // </if> | 510 // </if> |
507 | 511 |
508 /** | 512 /** |
509 * @param {string} languageCode | 513 * @param {string} languageCode |
510 * @return {boolean} True if the language is enabled. | 514 * @return {boolean} True if the language is enabled. |
511 */ | 515 */ |
512 isLanguageEnabled: function(languageCode) { | 516 isLanguageEnabled: function(languageCode) { |
513 return this.enabledLanguageSet_.has(languageCode); | 517 return this.enabledLanguageSet_.has(languageCode); |
514 }, | 518 }, |
515 | 519 |
516 /** | 520 /** |
517 * Enables the language, making it available for spell check and input. | 521 * Enables the language, making it available for spell check and input. |
518 * @param {string} languageCode | 522 * @param {string} languageCode |
519 */ | 523 */ |
520 enableLanguage: function(languageCode) { | 524 enableLanguage: function(languageCode) { |
521 if (!CrSettingsPrefs.isInitialized) | 525 if (!CrSettingsPrefs.isInitialized) |
522 return; | 526 return; |
523 | 527 |
524 this.languageSettingsPrivate.enableLanguage(languageCode); | 528 this.languageSettingsPrivate_.enableLanguage(languageCode); |
525 this.disableTranslateLanguage(languageCode); | 529 this.disableTranslateLanguage(languageCode); |
526 }, | 530 }, |
527 | 531 |
528 /** | 532 /** |
529 * Disables the language. | 533 * Disables the language. |
530 * @param {string} languageCode | 534 * @param {string} languageCode |
531 */ | 535 */ |
532 disableLanguage: function(languageCode) { | 536 disableLanguage: function(languageCode) { |
533 if (!CrSettingsPrefs.isInitialized) | 537 if (!CrSettingsPrefs.isInitialized) |
534 return; | 538 return; |
(...skipping 12 matching lines...) Expand all Loading... |
547 function(otherLanguageCode) { | 551 function(otherLanguageCode) { |
548 return otherLanguageCode != languageCode && | 552 return otherLanguageCode != languageCode && |
549 this.isLanguageEnabled(otherLanguageCode); | 553 this.isLanguageEnabled(otherLanguageCode); |
550 }.bind(this)); | 554 }.bind(this)); |
551 if (!supportsOtherEnabledLanguages) | 555 if (!supportsOtherEnabledLanguages) |
552 this.removeInputMethod(inputMethod.id); | 556 this.removeInputMethod(inputMethod.id); |
553 } | 557 } |
554 } | 558 } |
555 | 559 |
556 // Remove the language from preferred languages. | 560 // Remove the language from preferred languages. |
557 this.languageSettingsPrivate.disableLanguage(languageCode); | 561 this.languageSettingsPrivate_.disableLanguage(languageCode); |
558 this.enableTranslateLanguage(languageCode); | 562 this.enableTranslateLanguage(languageCode); |
559 }, | 563 }, |
560 | 564 |
561 /** | 565 /** |
562 * @param {string} languageCode Language code for an enabled language. | 566 * @param {string} languageCode Language code for an enabled language. |
563 * @return {boolean} | 567 * @return {boolean} |
564 */ | 568 */ |
565 canDisableLanguage: function(languageCode) { | 569 canDisableLanguage: function(languageCode) { |
566 // Cannot disable the prospective UI language. | 570 // Cannot disable the prospective UI language. |
567 if (languageCode == this.languages.prospectiveUILanguage) | 571 if (languageCode == this.languages.prospectiveUILanguage) |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 */ | 731 */ |
728 getLanguage: function(languageCode) { | 732 getLanguage: function(languageCode) { |
729 return this.supportedLanguageMap_.get(languageCode); | 733 return this.supportedLanguageMap_.get(languageCode); |
730 }, | 734 }, |
731 | 735 |
732 // <if expr="chromeos"> | 736 // <if expr="chromeos"> |
733 /** @param {string} id */ | 737 /** @param {string} id */ |
734 addInputMethod: function(id) { | 738 addInputMethod: function(id) { |
735 if (!this.supportedInputMethodMap_.has(id)) | 739 if (!this.supportedInputMethodMap_.has(id)) |
736 return; | 740 return; |
737 this.languageSettingsPrivate.addInputMethod(id); | 741 this.languageSettingsPrivate_.addInputMethod(id); |
738 }, | 742 }, |
739 | 743 |
740 /** @param {string} id */ | 744 /** @param {string} id */ |
741 removeInputMethod: function(id) { | 745 removeInputMethod: function(id) { |
742 if (!this.supportedInputMethodMap_.has(id)) | 746 if (!this.supportedInputMethodMap_.has(id)) |
743 return; | 747 return; |
744 this.languageSettingsPrivate.removeInputMethod(id); | 748 this.languageSettingsPrivate_.removeInputMethod(id); |
745 }, | 749 }, |
746 | 750 |
747 /** @param {string} id */ | 751 /** @param {string} id */ |
748 setCurrentInputMethod: function(id) { | 752 setCurrentInputMethod: function(id) { |
749 this.inputMethodPrivate.setCurrentInputMethod(id); | 753 this.inputMethodPrivate_.setCurrentInputMethod(id); |
750 }, | 754 }, |
751 | 755 |
752 /** | 756 /** |
753 * @param {string} languageCode | 757 * @param {string} languageCode |
754 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>} | 758 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>} |
755 */ | 759 */ |
756 getInputMethodsForLanguage: function(languageCode) { | 760 getInputMethodsForLanguage: function(languageCode) { |
757 return this.languageInputMethods_.get(languageCode) || []; | 761 return this.languageInputMethods_.get(languageCode) || []; |
758 }, | 762 }, |
759 | 763 |
760 /** | 764 /** |
761 * @param {!chrome.languageSettingsPrivate.InputMethod} inputMethod | 765 * @param {!chrome.languageSettingsPrivate.InputMethod} inputMethod |
762 * @return {boolean} | 766 * @return {boolean} |
763 */ | 767 */ |
764 isComponentIme: function(inputMethod) { | 768 isComponentIme: function(inputMethod) { |
765 return inputMethod.id.startsWith('_comp_'); | 769 return inputMethod.id.startsWith('_comp_'); |
766 }, | 770 }, |
767 | 771 |
768 /** @param {string} id Input method ID. */ | 772 /** @param {string} id Input method ID. */ |
769 openInputMethodOptions: function(id) { | 773 openInputMethodOptions: function(id) { |
770 this.inputMethodPrivate.openOptionsPage(id); | 774 this.inputMethodPrivate_.openOptionsPage(id); |
771 }, | 775 }, |
772 | 776 |
773 /** @param {string} id New current input method ID. */ | 777 /** @param {string} id New current input method ID. */ |
774 onInputMethodChanged_: function(id) { | 778 onInputMethodChanged_: function(id) { |
775 this.set('languages.inputMethods.currentId', id); | 779 this.set('languages.inputMethods.currentId', id); |
776 }, | 780 }, |
777 | 781 |
778 /** @param {string} id Added input method ID. */ | 782 /** @param {string} id Added input method ID. */ |
779 onInputMethodAdded_: function(id) { | 783 onInputMethodAdded_: function(id) { |
780 this.updateEnabledInputMethods_(); | 784 this.updateEnabledInputMethods_(); |
781 }, | 785 }, |
782 | 786 |
783 /** @param {string} id Removed input method ID. */ | 787 /** @param {string} id Removed input method ID. */ |
784 onInputMethodRemoved_: function(id) { | 788 onInputMethodRemoved_: function(id) { |
785 this.updateEnabledInputMethods_(); | 789 this.updateEnabledInputMethods_(); |
786 }, | 790 }, |
787 // </if> | 791 // </if> |
788 }); | 792 }); |
789 })(); | 793 })(); |
OLD | NEW |