Home | History | Annotate | Download | only in browser
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/spellcheck_host.h"
      6 
      7 #include "base/string_split.h"
      8 #include "chrome/browser/prefs/pref_member.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/spellcheck_host_impl.h"
     11 #include "chrome/browser/spellchecker_platform_engine.h"
     12 #include "chrome/common/pref_names.h"
     13 #include "chrome/common/spellcheck_common.h"
     14 
     15 // static
     16 scoped_refptr<SpellCheckHost> SpellCheckHost::Create(
     17     SpellCheckHostObserver* observer,
     18     const std::string& language,
     19     net::URLRequestContextGetter* request_context_getter) {
     20   scoped_refptr<SpellCheckHostImpl> host =
     21       new SpellCheckHostImpl(observer,
     22                              language,
     23                              request_context_getter);
     24   if (!host)
     25     return NULL;
     26 
     27   host->Initialize();
     28   return host;
     29 }
     30 
     31 // static
     32 int SpellCheckHost::GetSpellCheckLanguages(
     33     Profile* profile,
     34     std::vector<std::string>* languages) {
     35   StringPrefMember accept_languages_pref;
     36   StringPrefMember dictionary_language_pref;
     37   accept_languages_pref.Init(prefs::kAcceptLanguages, profile->GetPrefs(),
     38                              NULL);
     39   dictionary_language_pref.Init(prefs::kSpellCheckDictionary,
     40                                 profile->GetPrefs(), NULL);
     41   std::string dictionary_language = dictionary_language_pref.GetValue();
     42 
     43   // The current dictionary language should be there.
     44   languages->push_back(dictionary_language);
     45 
     46   // Now scan through the list of accept languages, and find possible mappings
     47   // from this list to the existing list of spell check languages.
     48   std::vector<std::string> accept_languages;
     49 
     50   if (SpellCheckerPlatform::SpellCheckerAvailable())
     51     SpellCheckerPlatform::GetAvailableLanguages(&accept_languages);
     52   else
     53     base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
     54 
     55   for (std::vector<std::string>::const_iterator i = accept_languages.begin();
     56        i != accept_languages.end(); ++i) {
     57     std::string language =
     58         SpellCheckCommon::GetCorrespondingSpellCheckLanguage(*i);
     59     if (!language.empty() &&
     60         std::find(languages->begin(), languages->end(), language) ==
     61         languages->end()) {
     62       languages->push_back(language);
     63     }
     64   }
     65 
     66   for (size_t i = 0; i < languages->size(); ++i) {
     67     if ((*languages)[i] == dictionary_language)
     68       return i;
     69   }
     70   return -1;
     71 }
     72