15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2011 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/translate/translate_prefs.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/command_line.h"
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_service.h"
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/strings/string_split.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_util.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/prefs/scoped_user_pref_update.h"
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/translate/translate_accept_languages.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/translate/translate_manager.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/pref_names.h"
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/common/translate/translate_util.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "components/user_prefs/pref_registry_syncable.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char TranslatePrefs::kPrefTranslateLanguageBlacklist[] =
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "translate_language_blacklist";
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char TranslatePrefs::kPrefTranslateSiteBlacklist[] =
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "translate_site_blacklist";
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char TranslatePrefs::kPrefTranslateWhitelists[] =
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "translate_whitelists";
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char TranslatePrefs::kPrefTranslateDeniedCount[] =
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "translate_denied_count";
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char TranslatePrefs::kPrefTranslateAcceptedCount[] =
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    "translate_accepted_count";
30eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochconst char TranslatePrefs::kPrefTranslateBlockedLanguages[] =
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    "translate_blocked_languages";
32eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
33eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochnamespace {
34eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
35eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid GetBlacklistedLanguages(const PrefService* prefs,
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             std::vector<std::string>* languages) {
37eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(languages->empty());
38eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
39eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const char* key = TranslatePrefs::kPrefTranslateLanguageBlacklist;
40eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const ListValue* list = prefs->GetList(key);
41eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (ListValue::const_iterator it = list->begin(); it != list->end(); ++it) {
42eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::string lang;
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    (*it)->GetAsString(&lang);
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    languages->push_back(lang);
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}  // namespace
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace {
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)void AppendLanguageToAcceptLanguages(PrefService* prefs,
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                     const std::string& language) {
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (!TranslateAcceptLanguages::CanBeAcceptLanguage(language))
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    return;
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::string accept_language = language;
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TranslateUtil::ToChromeLanguageSynonym(&accept_language);
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::string accept_languages_str = prefs->GetString(prefs::kAcceptLanguages);
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  std::vector<std::string> accept_languages;
627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::SplitString(accept_languages_str, ',', &accept_languages);
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (std::find(accept_languages.begin(),
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                accept_languages.end(),
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                accept_language) == accept_languages.end()) {
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    accept_languages.push_back(accept_language);
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  accept_languages_str = JoinString(accept_languages, ',');
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  prefs->SetString(prefs::kAcceptLanguages, accept_languages_str);
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TranslatePrefs: public: -----------------------------------------------------
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)TranslatePrefs::TranslatePrefs(PrefService* user_prefs)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : prefs_(user_prefs) {
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
80eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochbool TranslatePrefs::IsBlockedLanguage(
812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& original_language) const {
82eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CommandLine* command_line = CommandLine::ForCurrentProcess();
83eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (command_line->HasSwitch(switches::kEnableTranslateSettings)) {
84eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return IsValueBlacklisted(kPrefTranslateBlockedLanguages,
85eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              original_language);
86eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
87eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return IsValueBlacklisted(kPrefTranslateLanguageBlacklist,
88eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                              original_language);
89eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
92eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TranslatePrefs::BlockLanguage(
93eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& original_language) {
947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
95eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (command_line->HasSwitch(switches::kEnableTranslateSettings)) {
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    BlacklistValue(kPrefTranslateBlockedLanguages, original_language);
977d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    AppendLanguageToAcceptLanguages(prefs_, original_language);
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    BlacklistValue(kPrefTranslateLanguageBlacklist, original_language);
100eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
101eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TranslatePrefs::UnblockLanguage(
104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::string& original_language) {
105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CommandLine* command_line = CommandLine::ForCurrentProcess();
106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (command_line->HasSwitch(switches::kEnableTranslateSettings)) {
107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    RemoveValueFromBlacklist(kPrefTranslateBlockedLanguages,
108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             original_language);
109eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  } else {
110eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    RemoveValueFromBlacklist(kPrefTranslateLanguageBlacklist,
111eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             original_language);
112eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TranslatePrefs::RemoveLanguageFromLegacyBlacklist(
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_language) {
117eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  RemoveValueFromBlacklist(kPrefTranslateLanguageBlacklist,
118eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           original_language);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TranslatePrefs::IsSiteBlacklisted(const std::string& site) const {
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return IsValueBlacklisted(kPrefTranslateSiteBlacklist, site);
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::BlacklistSite(const std::string& site) {
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BlacklistValue(kPrefTranslateSiteBlacklist, site);
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::RemoveSiteFromBlacklist(const std::string& site) {
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RemoveValueFromBlacklist(kPrefTranslateSiteBlacklist, site);
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TranslatePrefs::IsLanguagePairWhitelisted(
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_language,
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& target_language) {
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists);
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (dict && !dict->empty()) {
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string auto_target_lang;
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (dict->GetString(original_language, &auto_target_lang) &&
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        auto_target_lang == target_language)
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::WhitelistLanguagePair(
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_language,
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& target_language) {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists);
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* dict = update.Get();
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!dict) {
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "Unregistered translate whitelist pref";
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->SetString(original_language, target_language);
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::RemoveLanguagePairFromWhitelist(
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_language,
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& target_language) {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateWhitelists);
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* dict = update.Get();
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!dict) {
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "Unregistered translate whitelist pref";
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->Remove(original_language, NULL);
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TranslatePrefs::HasBlacklistedLanguages() const {
171eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CommandLine* command_line = CommandLine::ForCurrentProcess();
172eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (command_line->HasSwitch(switches::kEnableTranslateSettings))
173eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return !IsListEmpty(kPrefTranslateBlockedLanguages);
174eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  else
175eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    return !IsListEmpty(kPrefTranslateLanguageBlacklist);
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TranslatePrefs::ClearBlacklistedLanguages() {
179eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  CommandLine* command_line = CommandLine::ForCurrentProcess();
180eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (command_line->HasSwitch(switches::kEnableTranslateSettings))
181eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    prefs_->ClearPref(kPrefTranslateBlockedLanguages);
182eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  else
183eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    prefs_->ClearPref(kPrefTranslateLanguageBlacklist);
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
186c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool TranslatePrefs::HasBlacklistedSites() const {
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !IsListEmpty(kPrefTranslateSiteBlacklist);
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TranslatePrefs::ClearBlacklistedSites() {
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  prefs_->ClearPref(kPrefTranslateSiteBlacklist);
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TranslatePrefs::HasWhitelistedLanguagePairs() const {
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return !IsDictionaryEmpty(kPrefTranslateWhitelists);
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TranslatePrefs::ClearWhitelistedLanguagePairs() {
1992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  prefs_->ClearPref(kPrefTranslateWhitelists);
2002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
2012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)int TranslatePrefs::GetTranslationDeniedCount(
2032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& language) const {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const DictionaryValue* dict =
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs_->GetDictionary(kPrefTranslateDeniedCount);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dict->GetInteger(language, &count) ? count : 0;
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::IncrementTranslationDeniedCount(
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& language) {
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateDeniedCount);
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* dict = update.Get();
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->GetInteger(language, &count);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->SetInteger(language, count + 1);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::ResetTranslationDeniedCount(const std::string& language) {
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateDeniedCount);
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  update.Get()->SetInteger(language, 0);
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int TranslatePrefs::GetTranslationAcceptedCount(const std::string& language) {
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const DictionaryValue* dict =
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      prefs_->GetDictionary(kPrefTranslateAcceptedCount);
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return dict->GetInteger(language, &count) ? count : 0;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::IncrementTranslationAcceptedCount(
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& language) {
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateAcceptedCount);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* dict = update.Get();
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int count = 0;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->GetInteger(language, &count);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dict->SetInteger(language, count + 1);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::ResetTranslationAcceptedCount(
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& language) {
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(prefs_, kPrefTranslateAcceptedCount);
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  update.Get()->SetInteger(language, 0);
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TranslatePrefs: public, static: ---------------------------------------------
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// static
2507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)bool TranslatePrefs::CanTranslateLanguage(Profile* profile,
2517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                                          const std::string& language) {
2527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  TranslatePrefs translate_prefs(profile->GetPrefs());
253eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool blocked = translate_prefs.IsBlockedLanguage(language);
2547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
2567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  if (command_line->HasSwitch(switches::kEnableTranslateSettings)) {
2577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    bool is_accept_language =
2587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        TranslateManager::IsAcceptLanguage(profile, language);
2597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    bool can_be_accept_language =
2607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)        TranslateAcceptLanguages::CanBeAcceptLanguage(language);
2617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2627d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Don't translate any user black-listed languages. Checking
2637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // |is_accept_language| is necessary because if the user eliminates the
2647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // language from the preference, it is natural to forget whether or not
2657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // the language should be translated. Checking |cannot_be_accept_language|
2667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // is also necessary because some minor languages can't be selected in the
2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // language preference even though the language is available in Translate
2687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // server.
269eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (blocked && (is_accept_language || !can_be_accept_language))
2707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return false;
2717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  } else {
2727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    // Don't translate any user user selected language.
273eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    if (blocked)
2747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      return false;
2757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  }
2767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  return true;
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// static
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TranslatePrefs::ShouldAutoTranslate(PrefService* user_prefs,
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& original_language, std::string* target_language) {
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TranslatePrefs prefs(user_prefs);
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return prefs.IsLanguageWhitelisted(original_language, target_language);
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// static
2887dbb3d5cf0c15f500944d211057644d6a2f37371Ben Murdochvoid TranslatePrefs::RegisterProfilePrefs(
289c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    user_prefs::PrefRegistrySyncable* registry) {
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registry->RegisterListPref(kPrefTranslateLanguageBlacklist,
291c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  registry->RegisterListPref(kPrefTranslateSiteBlacklist,
293c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
294c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterDictionaryPref(
295c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kPrefTranslateWhitelists,
296c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
297c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterDictionaryPref(
298c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kPrefTranslateDeniedCount,
299c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
300c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  registry->RegisterDictionaryPref(
301c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      kPrefTranslateAcceptedCount,
302c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
303eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  registry->RegisterListPref(kPrefTranslateBlockedLanguages,
304eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                             user_prefs::PrefRegistrySyncable::SYNCABLE_PREF);
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// static
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)void TranslatePrefs::MigrateUserPrefs(PrefService* user_prefs) {
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Old format of kPrefTranslateWhitelists
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - original language -> list of target langs to auto-translate
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - list of langs is in order of being enabled i.e. last in list is the
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   most recent language that user enabled via
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   Always translate |source_lang| to |target_lang|"
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - this results in a one-to-n relationship between source lang and target
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   langs.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // New format:
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - original language -> one target language to auto-translate
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - each time that the user enables the "Always translate..." option, that
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   target lang overwrites the previous one.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - this results in a one-to-one relationship between source lang and target
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   lang
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - we replace old list of target langs with the last target lang in list,
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   assuming the last (i.e. most recent) target lang is what user wants to
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //   keep auto-translated.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryPrefUpdate update(user_prefs, kPrefTranslateWhitelists);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DictionaryValue* dict = update.Get();
327eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (dict && !dict->empty()) {
328eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    DictionaryValue::Iterator iter(*dict);
329eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    while (!iter.IsAtEnd()) {
330eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      const ListValue* list = NULL;
331eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (!iter.value().GetAsList(&list) || !list)
332eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        break;  // Dictionary has either been migrated or new format.
333eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      std::string key = iter.key();
334eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      // Advance the iterator before removing the current element.
335eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      iter.Advance();
336eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      std::string target_lang;
337eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (list->empty() ||
338eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          !list->GetString(list->GetSize() - 1, &target_lang) ||
339eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          target_lang.empty()) {
340eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        dict->Remove(key, NULL);
341eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      } else {
342eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        dict->SetString(key, target_lang);
343eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
344eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
345eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
346eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
347eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Get the union of the blacklist and the Accept languages, and set this to
348eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // the new language set 'translate_blocked_languages'. This is used for the
349eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // settings UI for Translate and configration to determine which langauage
350eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // should be translated instead of the blacklist. The blacklist is no longer
351eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // used after launching the settings UI.
352eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // After that, Set 'translate_languages_not_translate' to Accept languages to
353eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // enable settings for users.
354eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool merged = user_prefs->HasPrefPath(kPrefTranslateBlockedLanguages);
355eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
356eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  const CommandLine& command_line = *CommandLine::ForCurrentProcess();
357eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  bool enabled_translate_settings =
358eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      command_line.HasSwitch(switches::kEnableTranslateSettings);
359eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
360eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  if (!merged && enabled_translate_settings) {
361eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<std::string> blacklisted_languages;
362eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    GetBlacklistedLanguages(user_prefs, &blacklisted_languages);
363eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
364eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::string accept_languages_str =
365eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        user_prefs->GetString(prefs::kAcceptLanguages);
366eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<std::string> accept_languages;
367eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    base::SplitString(accept_languages_str, ',', &accept_languages);
368eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
369eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<std::string> blocked_languages;
370eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    CreateBlockedLanguages(&blocked_languages,
371eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           blacklisted_languages,
372eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                           accept_languages);
373eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
374eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Create the new preference kPrefTranslateBlockedLanguages.
375eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    {
376eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ListValue* blocked_languages_list = new ListValue();
377eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      for (std::vector<std::string>::const_iterator it =
378eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch               blocked_languages.begin();
379eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           it != blocked_languages.end(); ++it) {
380eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        blocked_languages_list->Append(new StringValue(*it));
381eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      }
382eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ListPrefUpdate update(user_prefs, kPrefTranslateBlockedLanguages);
383eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      ListValue* list = update.Get();
384eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      DCHECK(list != NULL);
385eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      list->Swap(blocked_languages_list);
386eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    }
387eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
388eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    // Update kAcceptLanguages
389eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    for (std::vector<std::string>::const_iterator it =
390eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch             blocked_languages.begin();
391eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch         it != blocked_languages.end(); ++it) {
392eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      std::string lang = *it;
393eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      TranslateUtil::ToChromeLanguageSynonym(&lang);
394eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      bool not_found =
395eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          std::find(accept_languages.begin(), accept_languages.end(), lang) ==
396eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch          accept_languages.end();
397eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch      if (not_found)
398eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch        accept_languages.push_back(lang);
3992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    }
400eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
401eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::string new_accept_languages_str = JoinString(accept_languages, ",");
402eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    user_prefs->SetString(prefs::kAcceptLanguages, new_accept_languages_str);
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TranslatePrefs: private: ----------------------------------------------------
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
408eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch// static
409eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochvoid TranslatePrefs::CreateBlockedLanguages(
410eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::vector<std::string>* blocked_languages,
411eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<std::string>& blacklisted_languages,
412eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    const std::vector<std::string>& accept_languages) {
413eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  DCHECK(blocked_languages->empty());
414eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
415eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  std::set<std::string> result;
416eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
417eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (std::vector<std::string>::const_iterator it =
418eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch           blacklisted_languages.begin();
419eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch       it != blacklisted_languages.end(); ++it) {
420eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    result.insert(*it);
421eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
422eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
423eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  for (std::vector<std::string>::const_iterator it = accept_languages.begin();
424eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch       it != accept_languages.end(); ++it) {
425eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    std::string lang = *it;
426eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    TranslateUtil::ToTranslateLanguageSynonym(&lang);
427eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    result.insert(lang);
428eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
429eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
430eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  blocked_languages->insert(blocked_languages->begin(),
431eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                            result.begin(), result.end());
432eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch}
433eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TranslatePrefs::IsValueInList(const ListValue* list,
4352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& in_value) const {
4365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  for (size_t i = 0; i < list->GetSize(); ++i) {
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string value;
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (list->GetString(i, &value) && value == in_value)
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return true;
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TranslatePrefs::IsValueBlacklisted(const char* pref_id,
4452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& value) const {
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ListValue* blacklist = prefs_->GetList(pref_id);
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return (blacklist && !blacklist->empty() && IsValueInList(blacklist, value));
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::BlacklistValue(const char* pref_id,
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& value) {
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  {
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ListPrefUpdate update(prefs_, pref_id);
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ListValue* blacklist = update.Get();
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!blacklist) {
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED() << "Unregistered translate blacklist pref";
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return;
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    blacklist->Append(new StringValue(value));
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TranslatePrefs::RemoveValueFromBlacklist(const char* pref_id,
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& value) {
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListPrefUpdate update(prefs_, pref_id);
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ListValue* blacklist = update.Get();
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!blacklist) {
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NOTREACHED() << "Unregistered translate blacklist pref";
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return;
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  StringValue string_value(value);
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  blacklist->Remove(string_value, NULL);
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool TranslatePrefs::IsLanguageWhitelisted(
4762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    const std::string& original_language, std::string* target_language) const {
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const DictionaryValue* dict = prefs_->GetDictionary(kPrefTranslateWhitelists);
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (dict && dict->GetString(original_language, target_language)) {
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK(!target_language->empty());
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return !target_language->empty();
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return false;
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
4842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TranslatePrefs::IsListEmpty(const char* pref_id) const {
4862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const ListValue* blacklist = prefs_->GetList(pref_id);
4872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (blacklist == NULL || blacklist->empty());
4882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
4892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool TranslatePrefs::IsDictionaryEmpty(const char* pref_id) const {
4912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const DictionaryValue* dict = prefs_->GetDictionary(pref_id);
4922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  return (dict == NULL || dict->empty());
4932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
494