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/lazy_instance.h" |
12 #include "base/strings/string_piece.h" | 12 #include "base/strings/string_piece.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
15 #include "chrome/common/extensions/api/i18n.h" | 15 #include "chrome/common/extensions/api/i18n.h" |
16 #include "chrome/common/pref_names.h" | 16 #include "chrome/common/pref_names.h" |
17 #include "components/prefs/pref_service.h" | 17 #include "components/prefs/pref_service.h" |
18 | 18 |
19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; | 19 namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Errors. | 25 // Errors. |
26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; | 26 static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
27 | 27 |
28 } | 28 } |
29 | 29 |
30 bool I18nGetAcceptLanguagesFunction::RunSync() { | 30 ExtensionFunction::ResponseAction I18nGetAcceptLanguagesFunction::Run() { |
31 std::string accept_languages = | 31 std::string accept_languages = Profile::FromBrowserContext(browser_context()) |
32 GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 32 ->GetPrefs() |
| 33 ->GetString(prefs::kAcceptLanguages); |
33 // Currently, there are 2 ways to set browser's accept-languages: through UI | 34 // Currently, there are 2 ways to set browser's accept-languages: through UI |
34 // or directly modify the preference file. The accept-languages set through | 35 // or directly modify the preference file. The accept-languages set through |
35 // UI is guranteed to be valid, and the accept-languages string returned from | 36 // UI is guranteed to be valid, and the accept-languages string returned from |
36 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to | 37 // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to |
37 // be valid and well-formed, which means each accept-langauge is a valid | 38 // be valid and well-formed, which means each accept-langauge is a valid |
38 // code, and accept-languages are seperatd by "," without surrrounding | 39 // code, and accept-languages are seperatd by "," without surrrounding |
39 // spaces. But we do not do any validation (either the format or the validity | 40 // spaces. But we do not do any validation (either the format or the validity |
40 // of the language code) on accept-languages set through editing preference | 41 // of the language code) on accept-languages set through editing preference |
41 // file directly. So, here, we're adding extra checks to be resistant to | 42 // file directly. So, here, we're adding extra checks to be resistant to |
42 // crashes caused by data corruption. | 43 // crashes caused by data corruption. |
43 if (accept_languages.empty()) { | 44 if (accept_languages.empty()) |
44 error_ = kEmptyAcceptLanguagesError; | 45 return RespondNow(Error(kEmptyAcceptLanguagesError)); |
45 return false; | |
46 } | |
47 | 46 |
48 std::vector<std::string> languages = base::SplitString( | 47 std::vector<std::string> languages = base::SplitString( |
49 accept_languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 48 accept_languages, ",", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
50 languages.erase(std::remove(languages.begin(), languages.end(), ""), | 49 languages.erase(std::remove(languages.begin(), languages.end(), ""), |
51 languages.end()); | 50 languages.end()); |
52 | 51 |
53 if (languages.empty()) { | 52 if (languages.empty()) |
54 error_ = kEmptyAcceptLanguagesError; | 53 return RespondNow(Error(kEmptyAcceptLanguagesError)); |
55 return false; | |
56 } | |
57 | 54 |
58 results_ = GetAcceptLanguages::Results::Create(languages); | 55 return RespondNow( |
59 return true; | 56 ArgumentList(GetAcceptLanguages::Results::Create(languages))); |
60 } | 57 } |
61 | 58 |
62 } // namespace extensions | 59 } // namespace extensions |
OLD | NEW |