1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/spellchecker/spellcheck_host.h"
#include "base/string_split.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/prefs/pref_member.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/spellchecker/spellcheck_host_impl.h"
#include "chrome/browser/spellchecker/spellcheck_platform_mac.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/spellcheck_common.h"
using content::BrowserThread;
namespace {
// An event used by browser tests to receive status events from this class and
// its derived classes.
base::WaitableEvent* g_status_event = NULL;
SpellCheckHost::EventType g_status_type = SpellCheckHost::BDICT_NOTINITIALIZED;
} // namespace
// static
SpellCheckHost* SpellCheckHost::Create(
SpellCheckProfileProvider* profile,
const std::string& language,
net::URLRequestContextGetter* request_context_getter,
SpellCheckHostMetrics* metrics) {
SpellCheckHostImpl* host =
new SpellCheckHostImpl(profile, language, request_context_getter,
metrics);
if (!host)
return NULL;
host->Initialize();
return host;
}
// static
int SpellCheckHost::GetSpellCheckLanguages(
Profile* profile,
std::vector<std::string>* languages) {
StringPrefMember accept_languages_pref;
StringPrefMember dictionary_language_pref;
accept_languages_pref.Init(prefs::kAcceptLanguages, profile->GetPrefs(),
NULL);
dictionary_language_pref.Init(prefs::kSpellCheckDictionary,
profile->GetPrefs(), NULL);
std::string dictionary_language = dictionary_language_pref.GetValue();
// The current dictionary language should be there.
languages->push_back(dictionary_language);
// Now scan through the list of accept languages, and find possible mappings
// from this list to the existing list of spell check languages.
std::vector<std::string> accept_languages;
#if defined(OS_MACOSX)
if (spellcheck_mac::SpellCheckerAvailable())
spellcheck_mac::GetAvailableLanguages(&accept_languages);
else
base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
#else
base::SplitString(accept_languages_pref.GetValue(), ',', &accept_languages);
#endif // !OS_MACOSX
for (std::vector<std::string>::const_iterator i = accept_languages.begin();
i != accept_languages.end(); ++i) {
std::string language =
SpellCheckCommon::GetCorrespondingSpellCheckLanguage(*i);
if (!language.empty() &&
std::find(languages->begin(), languages->end(), language) ==
languages->end()) {
languages->push_back(language);
}
}
for (size_t i = 0; i < languages->size(); ++i) {
if ((*languages)[i] == dictionary_language)
return i;
}
return -1;
}
// static
bool SpellCheckHost::SignalStatusEvent(SpellCheckHost::EventType status_type) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
if (!g_status_event)
return false;
g_status_type = status_type;
g_status_event->Signal();
return true;
}
// static
void SpellCheckHost::AttachStatusEvent(base::WaitableEvent* status_event) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
g_status_event = status_event;
}
// static
SpellCheckHost::EventType SpellCheckHost::WaitStatusEvent() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (g_status_event)
g_status_event->Wait();
return g_status_type;
}
|