blob: 7db0a2bb4494e9b167f7dd77c9d420cdd60c040e (
plain)
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
|
// Copyright (c) 2009 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/chromeos/language_library.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "chrome/browser/chrome_thread.h"
#include "chrome/browser/chromeos/cros_library.h"
// Allows InvokeLater without adding refcounting. This class is a Singleton and
// won't be deleted until it's last InvokeLater is run.
template <>
struct RunnableMethodTraits<chromeos::LanguageLibrary> {
void RetainCallee(chromeos::LanguageLibrary* obj) {}
void ReleaseCallee(chromeos::LanguageLibrary* obj) {}
};
namespace chromeos {
LanguageLibrary::LanguageLibrary() : language_status_connection_(NULL) {
if (EnsureLoaded()) {
Init();
}
}
LanguageLibrary::~LanguageLibrary() {
if (EnsureLoaded()) {
chromeos::DisconnectLanguageStatus(language_status_connection_);
}
}
LanguageLibrary::Observer::~Observer() {
}
// static
LanguageLibrary* LanguageLibrary::Get() {
return Singleton<LanguageLibrary>::get();
}
// static
bool LanguageLibrary::EnsureLoaded() {
return CrosLibrary::EnsureLoaded();
}
void LanguageLibrary::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void LanguageLibrary::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
chromeos::InputLanguageList* LanguageLibrary::GetLanguages() {
chromeos::InputLanguageList* result = NULL;
if (EnsureLoaded()) {
result = chromeos::GetLanguages(language_status_connection_);
}
return result ? result : CreateFallbackInputLanguageList();
}
void LanguageLibrary::ChangeLanguage(
LanguageCategory category, const std::string& id) {
if (EnsureLoaded()) {
chromeos::ChangeLanguage(language_status_connection_, category, id.c_str());
}
}
// static
void LanguageLibrary::LanguageChangedHandler(
void* object, const chromeos::InputLanguage& current_language) {
LanguageLibrary* language_library = static_cast<LanguageLibrary*>(object);
language_library->UpdateCurrentLanguage(current_language);
}
void LanguageLibrary::Init() {
language_status_connection_ = chromeos::MonitorLanguageStatus(
&LanguageChangedHandler, this);
}
void LanguageLibrary::UpdateCurrentLanguage(
const chromeos::InputLanguage& current_language) {
// Make sure we run on UI thread.
if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) {
DLOG(INFO) << "UpdateCurrentLanguage (Background thread)";
ChromeThread::PostTask(
ChromeThread::UI, FROM_HERE,
// NewRunnableMethod() copies |current_language| by value.
NewRunnableMethod(
this, &LanguageLibrary::UpdateCurrentLanguage, current_language));
return;
}
DLOG(INFO) << "UpdateCurrentLanguage (UI thread)";
current_language_ = current_language;
FOR_EACH_OBSERVER(Observer, observers_, LanguageChanged(this));
}
} // namespace chromeos
|