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 /** @private {?InputMethodPrivate} */ |
| 152 inputMethodPrivate_: null, |
| 153 |
151 /** @override */ | 154 /** @override */ |
152 attached: function() { | 155 attached: function() { |
153 this.languageSettingsPrivate = | 156 this.browserProxy_ = settings.LanguagesBrowserProxyImpl.getInstance(); |
154 settings.languageSettingsPrivateApiForTest || | 157 this.languageSettingsPrivate_ = |
155 /** @type {!LanguageSettingsPrivate} */(chrome.languageSettingsPrivate); | 158 this.browserProxy_.getLanguageSettingsPrivate(); |
156 | 159 this.inputMethodPrivate_ = this.browserProxy_.getInputMethodPrivate(); |
157 this.inputMethodPrivate = | |
158 settings.inputMethodPrivateApiForTest || | |
159 /** @type {!InputMethodPrivate} */(chrome.inputMethodPrivate); | |
160 | 160 |
161 var promises = []; | 161 var promises = []; |
162 | 162 |
163 // Wait until prefs are initialized before creating the model, so we can | 163 // Wait until prefs are initialized before creating the model, so we can |
164 // include information about enabled languages. | 164 // include information about enabled languages. |
165 promises[0] = CrSettingsPrefs.initialized; | 165 promises[0] = CrSettingsPrefs.initialized; |
166 | 166 |
167 // Get the language list. | 167 // Get the language list. |
168 promises[1] = new Promise(function(resolve) { | 168 promises[1] = new Promise(function(resolve) { |
169 this.languageSettingsPrivate.getLanguageList(resolve); | 169 this.languageSettingsPrivate_.getLanguageList(resolve); |
170 }.bind(this)); | 170 }.bind(this)); |
171 | 171 |
172 // Get the translate target language. | 172 // Get the translate target language. |
173 promises[2] = new Promise(function(resolve) { | 173 promises[2] = new Promise(function(resolve) { |
174 this.languageSettingsPrivate.getTranslateTargetLanguage(resolve); | 174 this.languageSettingsPrivate_.getTranslateTargetLanguage(resolve); |
175 }.bind(this)); | 175 }.bind(this)); |
176 | 176 |
177 if (cr.isChromeOS) { | 177 if (cr.isChromeOS) { |
178 promises[3] = new Promise(function(resolve) { | 178 promises[3] = new Promise(function(resolve) { |
179 this.languageSettingsPrivate.getInputMethodLists(function(lists) { | 179 this.languageSettingsPrivate_.getInputMethodLists(function(lists) { |
180 resolve(lists.componentExtensionImes.concat( | 180 resolve(lists.componentExtensionImes.concat( |
181 lists.thirdPartyExtensionImes)); | 181 lists.thirdPartyExtensionImes)); |
182 }); | 182 }); |
183 }.bind(this)); | 183 }.bind(this)); |
184 | 184 |
185 promises[4] = new Promise(function(resolve) { | 185 promises[4] = new Promise(function(resolve) { |
186 this.inputMethodPrivate.getCurrentInputMethod(resolve); | 186 this.inputMethodPrivate_.getCurrentInputMethod(resolve); |
187 }.bind(this)); | 187 }.bind(this)); |
188 } | 188 } |
189 | 189 |
190 if (cr.isWindows || cr.isChromeOS) { | 190 if (cr.isWindows || cr.isChromeOS) { |
191 // Fetch the starting UI language, which affects which actions should be | 191 // Fetch the starting UI language, which affects which actions should be |
192 // enabled. | 192 // enabled. |
193 promises.push(cr.sendWithPromise('getProspectiveUILanguage').then( | 193 promises.push(this.browserProxy_.getProspectiveUILanguage().then( |
194 function(prospectiveUILanguage) { | 194 function(prospectiveUILanguage) { |
195 this.originalProspectiveUILanguage_ = | 195 this.originalProspectiveUILanguage_ = |
196 prospectiveUILanguage || window.navigator.language; | 196 prospectiveUILanguage || window.navigator.language; |
197 }.bind(this))); | 197 }.bind(this))); |
198 } | 198 } |
199 | 199 |
200 Promise.all(promises).then(function(results) { | 200 Promise.all(promises).then(function(results) { |
201 if (!this.isConnected) { | 201 if (!this.isConnected) { |
202 // Return early if this element was detached from the DOM before this | 202 // Return early if this element was detached from the DOM before this |
203 // async callback executes (can happen during testing). | 203 // async callback executes (can happen during testing). |
204 return; | 204 return; |
205 } | 205 } |
206 | 206 |
207 // TODO(dpapad): Cleanup this code. It uses results[3] and results[4] | 207 // TODO(dpapad): Cleanup this code. It uses results[3] and results[4] |
208 // which only exist for ChromeOS. | 208 // which only exist for ChromeOS. |
209 this.createModel_(results[1], results[2], results[3], results[4]); | 209 this.createModel_(results[1], results[2], results[3], results[4]); |
210 this.resolver_.resolve(); | 210 this.resolver_.resolve(); |
211 }.bind(this)); | 211 }.bind(this)); |
212 | 212 |
213 if (cr.isChromeOS) { | 213 if (cr.isChromeOS) { |
214 this.boundOnInputMethodChanged_ = this.onInputMethodChanged_.bind(this); | 214 this.boundOnInputMethodChanged_ = this.onInputMethodChanged_.bind(this); |
215 this.inputMethodPrivate.onChanged.addListener( | 215 this.inputMethodPrivate_.onChanged.addListener( |
216 assert(this.boundOnInputMethodChanged_)); | 216 assert(this.boundOnInputMethodChanged_)); |
217 } | 217 } |
218 }, | 218 }, |
219 | 219 |
220 /** @override */ | 220 /** @override */ |
221 detached: function() { | 221 detached: function() { |
222 if (cr.isChromeOS) { | 222 if (cr.isChromeOS) { |
223 this.inputMethodPrivate.onChanged.removeListener( | 223 this.inputMethodPrivate_.onChanged.removeListener( |
224 assert(this.boundOnInputMethodChanged_)); | 224 assert(this.boundOnInputMethodChanged_)); |
225 this.boundOnInputMethodChanged_ = null; | 225 this.boundOnInputMethodChanged_ = null; |
226 } | 226 } |
227 }, | 227 }, |
228 | 228 |
229 /** | 229 /** |
230 * Updates the prospective UI language based on the new pref value. | 230 * Updates the prospective UI language based on the new pref value. |
231 * @param {string} prospectiveUILanguage | 231 * @param {string} prospectiveUILanguage |
232 * @private | 232 * @private |
233 */ | 233 */ |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 return this.resolver_.promise; | 485 return this.resolver_.promise; |
486 }, | 486 }, |
487 | 487 |
488 // <if expr="chromeos or is_win"> | 488 // <if expr="chromeos or is_win"> |
489 /** | 489 /** |
490 * Sets the prospective UI language to the chosen language. This won't affect | 490 * Sets the prospective UI language to the chosen language. This won't affect |
491 * the actual UI language until a restart. | 491 * the actual UI language until a restart. |
492 * @param {string} languageCode | 492 * @param {string} languageCode |
493 */ | 493 */ |
494 setProspectiveUILanguage: function(languageCode) { | 494 setProspectiveUILanguage: function(languageCode) { |
495 chrome.send('setProspectiveUILanguage', [languageCode]); | 495 this.browserProxy_.setProspectiveUILanguage(languageCode); |
496 }, | 496 }, |
497 | 497 |
498 /** | 498 /** |
499 * True if the prospective UI language was changed from its starting value. | 499 * True if the prospective UI language was changed from its starting value. |
500 * @return {boolean} | 500 * @return {boolean} |
501 */ | 501 */ |
502 requiresRestart: function() { | 502 requiresRestart: function() { |
503 return this.originalProspectiveUILanguage_ != | 503 return this.originalProspectiveUILanguage_ != |
504 this.languages.prospectiveUILanguage; | 504 this.languages.prospectiveUILanguage; |
505 }, | 505 }, |
506 // </if> | 506 // </if> |
507 | 507 |
508 /** | 508 /** |
509 * @param {string} languageCode | 509 * @param {string} languageCode |
510 * @return {boolean} True if the language is enabled. | 510 * @return {boolean} True if the language is enabled. |
511 */ | 511 */ |
512 isLanguageEnabled: function(languageCode) { | 512 isLanguageEnabled: function(languageCode) { |
513 return this.enabledLanguageSet_.has(languageCode); | 513 return this.enabledLanguageSet_.has(languageCode); |
514 }, | 514 }, |
515 | 515 |
516 /** | 516 /** |
517 * Enables the language, making it available for spell check and input. | 517 * Enables the language, making it available for spell check and input. |
518 * @param {string} languageCode | 518 * @param {string} languageCode |
519 */ | 519 */ |
520 enableLanguage: function(languageCode) { | 520 enableLanguage: function(languageCode) { |
521 if (!CrSettingsPrefs.isInitialized) | 521 if (!CrSettingsPrefs.isInitialized) |
522 return; | 522 return; |
523 | 523 |
524 this.languageSettingsPrivate.enableLanguage(languageCode); | 524 this.languageSettingsPrivate_.enableLanguage(languageCode); |
525 this.disableTranslateLanguage(languageCode); | 525 this.disableTranslateLanguage(languageCode); |
526 }, | 526 }, |
527 | 527 |
528 /** | 528 /** |
529 * Disables the language. | 529 * Disables the language. |
530 * @param {string} languageCode | 530 * @param {string} languageCode |
531 */ | 531 */ |
532 disableLanguage: function(languageCode) { | 532 disableLanguage: function(languageCode) { |
533 if (!CrSettingsPrefs.isInitialized) | 533 if (!CrSettingsPrefs.isInitialized) |
534 return; | 534 return; |
(...skipping 12 matching lines...) Expand all Loading... |
547 function(otherLanguageCode) { | 547 function(otherLanguageCode) { |
548 return otherLanguageCode != languageCode && | 548 return otherLanguageCode != languageCode && |
549 this.isLanguageEnabled(otherLanguageCode); | 549 this.isLanguageEnabled(otherLanguageCode); |
550 }.bind(this)); | 550 }.bind(this)); |
551 if (!supportsOtherEnabledLanguages) | 551 if (!supportsOtherEnabledLanguages) |
552 this.removeInputMethod(inputMethod.id); | 552 this.removeInputMethod(inputMethod.id); |
553 } | 553 } |
554 } | 554 } |
555 | 555 |
556 // Remove the language from preferred languages. | 556 // Remove the language from preferred languages. |
557 this.languageSettingsPrivate.disableLanguage(languageCode); | 557 this.languageSettingsPrivate_.disableLanguage(languageCode); |
558 this.enableTranslateLanguage(languageCode); | 558 this.enableTranslateLanguage(languageCode); |
559 }, | 559 }, |
560 | 560 |
561 /** | 561 /** |
562 * @param {string} languageCode Language code for an enabled language. | 562 * @param {string} languageCode Language code for an enabled language. |
563 * @return {boolean} | 563 * @return {boolean} |
564 */ | 564 */ |
565 canDisableLanguage: function(languageCode) { | 565 canDisableLanguage: function(languageCode) { |
566 // Cannot disable the prospective UI language. | 566 // Cannot disable the prospective UI language. |
567 if (languageCode == this.languages.prospectiveUILanguage) | 567 if (languageCode == this.languages.prospectiveUILanguage) |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
727 */ | 727 */ |
728 getLanguage: function(languageCode) { | 728 getLanguage: function(languageCode) { |
729 return this.supportedLanguageMap_.get(languageCode); | 729 return this.supportedLanguageMap_.get(languageCode); |
730 }, | 730 }, |
731 | 731 |
732 // <if expr="chromeos"> | 732 // <if expr="chromeos"> |
733 /** @param {string} id */ | 733 /** @param {string} id */ |
734 addInputMethod: function(id) { | 734 addInputMethod: function(id) { |
735 if (!this.supportedInputMethodMap_.has(id)) | 735 if (!this.supportedInputMethodMap_.has(id)) |
736 return; | 736 return; |
737 this.languageSettingsPrivate.addInputMethod(id); | 737 this.languageSettingsPrivate_.addInputMethod(id); |
738 }, | 738 }, |
739 | 739 |
740 /** @param {string} id */ | 740 /** @param {string} id */ |
741 removeInputMethod: function(id) { | 741 removeInputMethod: function(id) { |
742 if (!this.supportedInputMethodMap_.has(id)) | 742 if (!this.supportedInputMethodMap_.has(id)) |
743 return; | 743 return; |
744 this.languageSettingsPrivate.removeInputMethod(id); | 744 this.languageSettingsPrivate_.removeInputMethod(id); |
745 }, | 745 }, |
746 | 746 |
747 /** @param {string} id */ | 747 /** @param {string} id */ |
748 setCurrentInputMethod: function(id) { | 748 setCurrentInputMethod: function(id) { |
749 this.inputMethodPrivate.setCurrentInputMethod(id); | 749 this.inputMethodPrivate_.setCurrentInputMethod(id); |
750 }, | 750 }, |
751 | 751 |
752 /** | 752 /** |
753 * @param {string} languageCode | 753 * @param {string} languageCode |
754 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>} | 754 * @return {!Array<!chrome.languageSettingsPrivate.InputMethod>} |
755 */ | 755 */ |
756 getInputMethodsForLanguage: function(languageCode) { | 756 getInputMethodsForLanguage: function(languageCode) { |
757 return this.languageInputMethods_.get(languageCode) || []; | 757 return this.languageInputMethods_.get(languageCode) || []; |
758 }, | 758 }, |
759 | 759 |
760 /** | 760 /** |
761 * @param {!chrome.languageSettingsPrivate.InputMethod} inputMethod | 761 * @param {!chrome.languageSettingsPrivate.InputMethod} inputMethod |
762 * @return {boolean} | 762 * @return {boolean} |
763 */ | 763 */ |
764 isComponentIme: function(inputMethod) { | 764 isComponentIme: function(inputMethod) { |
765 return inputMethod.id.startsWith('_comp_'); | 765 return inputMethod.id.startsWith('_comp_'); |
766 }, | 766 }, |
767 | 767 |
768 /** @param {string} id Input method ID. */ | 768 /** @param {string} id Input method ID. */ |
769 openInputMethodOptions: function(id) { | 769 openInputMethodOptions: function(id) { |
770 this.inputMethodPrivate.openOptionsPage(id); | 770 this.inputMethodPrivate_.openOptionsPage(id); |
771 }, | 771 }, |
772 | 772 |
773 /** @param {string} id New current input method ID. */ | 773 /** @param {string} id New current input method ID. */ |
774 onInputMethodChanged_: function(id) { | 774 onInputMethodChanged_: function(id) { |
775 this.set('languages.inputMethods.currentId', id); | 775 this.set('languages.inputMethods.currentId', id); |
776 }, | 776 }, |
777 | 777 |
778 /** @param {string} id Added input method ID. */ | 778 /** @param {string} id Added input method ID. */ |
779 onInputMethodAdded_: function(id) { | 779 onInputMethodAdded_: function(id) { |
780 this.updateEnabledInputMethods_(); | 780 this.updateEnabledInputMethods_(); |
781 }, | 781 }, |
782 | 782 |
783 /** @param {string} id Removed input method ID. */ | 783 /** @param {string} id Removed input method ID. */ |
784 onInputMethodRemoved_: function(id) { | 784 onInputMethodRemoved_: function(id) { |
785 this.updateEnabledInputMethods_(); | 785 this.updateEnabledInputMethods_(); |
786 }, | 786 }, |
787 // </if> | 787 // </if> |
788 }); | 788 }); |
789 })(); | 789 })(); |
OLD | NEW |