[email protected] | 07ff5fd | 2012-07-12 22:39:09 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 5 | #include "chrome/browser/extensions/api/i18n/i18n_api.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <string> |
| 9 | #include <vector> |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 10 | |
[email protected] | 864b136 | 2010-08-19 03:49:38 | [diff] [blame] | 11 | #include "base/string_piece.h" |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 12 | #include "base/string_split.h" |
[email protected] | 37858e5 | 2010-08-26 00:22:02 | [diff] [blame] | 13 | #include "chrome/browser/prefs/pref_service.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 14 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 758b0b70 | 2013-01-10 12:19:33 | [diff] [blame] | 15 | #include "chrome/common/extensions/api/i18n.h" |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame^] | 16 | #include "chrome/common/pref_names.h" |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 17 | |
| 18 | namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages; |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 19 | |
[email protected] | ddd231e | 2010-06-29 20:35:19 | [diff] [blame] | 20 | // Errors. |
| 21 | static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty."; |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 22 | |
[email protected] | 4636c83 | 2013-01-11 02:10:11 | [diff] [blame^] | 23 | bool I18nGetAcceptLanguagesFunction::RunImpl() { |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 24 | std::string accept_languages = |
| 25 | profile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 26 | // 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 |
| 28 | // UI is guranteed to be valid, and the accept-languages string returned from |
| 29 | // profile()->GetPrefs()->GetString(prefs::kAcceptLanguages) is guranteed to |
| 30 | // be valid and well-formed, which means each accept-langauge is a valid |
| 31 | // code, and accept-languages are seperatd by "," without surrrounding |
| 32 | // spaces. But we do not do any validation (either the format or the validity |
| 33 | // of the language code) on accept-languages set through editing preference |
| 34 | // file directly. So, here, we're adding extra checks to be resistant to |
| 35 | // crashes caused by data corruption. |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 36 | if (accept_languages.empty()) { |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 37 | error_ = kEmptyAcceptLanguagesError; |
| 38 | return false; |
| 39 | } |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 40 | |
| 41 | std::vector<std::string> languages; |
| 42 | base::SplitString(accept_languages, ',', &languages); |
| 43 | languages.erase(std::remove(languages.begin(), languages.end(), ""), |
| 44 | languages.end()); |
| 45 | |
| 46 | if (languages.empty()) { |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 47 | error_ = kEmptyAcceptLanguagesError; |
| 48 | return false; |
| 49 | } |
[email protected] | 6684dc811 | 2012-07-30 22:10:18 | [diff] [blame] | 50 | |
| 51 | results_ = GetAcceptLanguages::Results::Create(languages); |
[email protected] | 198bcfe | 2009-09-09 22:56:28 | [diff] [blame] | 52 | return true; |
| 53 | } |