| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/api/i18n/i18n_api.h" | 5 #include "chrome/browser/extensions/api/i18n/i18n_api.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/lazy_instance.h" |
| 11 #include "base/string_piece.h" | 12 #include "base/string_piece.h" |
| 12 #include "base/string_split.h" | 13 #include "base/string_split.h" |
| 13 #include "chrome/browser/prefs/pref_service.h" | 14 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/extensions/api/i18n.h" | 16 #include "chrome/common/extensions/api/i18n.h" |
| 17 #include "chrome/common/extensions/api/i18n/default_locale_handler.h" |
| 18 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 19 #include "chrome/common/extensions/manifest_handler.h" |
| 16 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 17 | 21 |
| 18 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; | 22 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
| 19 | 23 |
| 24 namespace extensions { |
| 25 |
| 26 namespace { |
| 27 |
| 20 // Errors. | 28 // Errors. |
| 21 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; | 29 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
| 22 | 30 |
| 31 } |
| 32 |
| 23 bool I18nGetAcceptLanguagesFunction::RunImpl() { | 33 bool I18nGetAcceptLanguagesFunction::RunImpl() { |
| 24 std::string accept_languages = | 34 std::string accept_languages = |
| 25 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 35 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 26 // Currently, there are 2 ways to set browser's accept-languages: through UI | 36 // Currently, there are 2 ways to set browser's accept-languages: through UI |
| 27 // or directly modify the preference file. The accept-languages set through | 37 // or directly modify the preference file. The accept-languages set through |
| 28 // UI is guranteed to be valid, and the accept-languages string returned from | 38 // UI is guranteed to be valid, and the accept-languages string returned from |
| 29 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to | 39 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to |
| 30 // be valid and well-formed, which means each accept-langauge is a valid | 40 // be valid and well-formed, which means each accept-langauge is a valid |
| 31 // code, and accept-languages are seperatd by "," without surrrounding | 41 // code, and accept-languages are seperatd by "," without surrrounding |
| 32 // spaces. But we do not do any validation (either the format or the validity | 42 // spaces. But we do not do any validation (either the format or the validity |
| (...skipping 11 matching lines...) Expand all Loading... |
| 44 languages.end()); | 54 languages.end()); |
| 45 | 55 |
| 46 if (languages.empty()) { | 56 if (languages.empty()) { |
| 47 error_ = kEmptyAcceptLanguagesError; | 57 error_ = kEmptyAcceptLanguagesError; |
| 48 return false; | 58 return false; |
| 49 } | 59 } |
| 50 | 60 |
| 51 results_ = GetAcceptLanguages::Results::Create(languages); | 61 results_ = GetAcceptLanguages::Results::Create(languages); |
| 52 return true; | 62 return true; |
| 53 } | 63 } |
| 64 |
| 65 I18nAPI::I18nAPI(Profile* profile) { |
| 66 ManifestHandler::Register(extension_manifest_keys::kDefaultLocale, |
| 67 new DefaultLocaleHandler); |
| 68 } |
| 69 |
| 70 I18nAPI::~I18nAPI() { |
| 71 } |
| 72 |
| 73 static base::LazyInstance<ProfileKeyedAPIFactory<I18nAPI> > |
| 74 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 75 |
| 76 // static |
| 77 ProfileKeyedAPIFactory<I18nAPI>* I18nAPI::GetFactoryInstance() { |
| 78 return &g_factory.Get(); |
| 79 } |
| 80 |
| 81 } // namespace extensions |
| OLD | NEW |