blob: ad2ef32c45afd58cd969ecac1978b1dd5db9214d [file] [log] [blame]
[email protected]07ff5fd2012-07-12 22:39:091// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]198bcfe2009-09-09 22:56:282// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6684dc8112012-07-30 22:10:185#include "chrome/browser/extensions/api/i18n/i18n_api.h"
6
7#include <algorithm>
8#include <string>
9#include <vector>
[email protected]198bcfe2009-09-09 22:56:2810
[email protected]864b1362010-08-19 03:49:3811#include "base/string_piece.h"
[email protected]6684dc8112012-07-30 22:10:1812#include "base/string_split.h"
[email protected]37858e52010-08-26 00:22:0213#include "chrome/browser/prefs/pref_service.h"
[email protected]8ecad5e2010-12-02 21:18:3314#include "chrome/browser/profiles/profile.h"
[email protected]758b0b702013-01-10 12:19:3315#include "chrome/common/extensions/api/i18n.h"
[email protected]4636c832013-01-11 02:10:1116#include "chrome/common/pref_names.h"
[email protected]6684dc8112012-07-30 22:10:1817
18namespace GetAcceptLanguages = extensions::api::i18n::GetAcceptLanguages;
[email protected]198bcfe2009-09-09 22:56:2819
[email protected]ddd231e2010-06-29 20:35:1920// Errors.
21static const char kEmptyAcceptLanguagesError[] = "accept-languages is empty.";
[email protected]198bcfe2009-09-09 22:56:2822
[email protected]4636c832013-01-11 02:10:1123bool I18nGetAcceptLanguagesFunction::RunImpl() {
[email protected]6684dc8112012-07-30 22:10:1824 std::string accept_languages =
25 profile()->GetPrefs()->GetString(prefs::kAcceptLanguages);
[email protected]198bcfe2009-09-09 22:56:2826 // 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]6684dc8112012-07-30 22:10:1836 if (accept_languages.empty()) {
[email protected]198bcfe2009-09-09 22:56:2837 error_ = kEmptyAcceptLanguagesError;
38 return false;
39 }
[email protected]6684dc8112012-07-30 22:10:1840
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]198bcfe2009-09-09 22:56:2847 error_ = kEmptyAcceptLanguagesError;
48 return false;
49 }
[email protected]6684dc8112012-07-30 22:10:1850
51 results_ = GetAcceptLanguages::Results::Create(languages);
[email protected]198bcfe2009-09-09 22:56:2852 return true;
53}