Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(126)

Side by Side Diff: chrome/browser/chromeos/input_method/component_extension_ime_manager_impl.cc

Issue 13020002: Implement ComponentExtensionIMEManagerDelegate. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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/chromeos/input_method/component_extension_ime_manager_i mpl.h"
6
7 #include "base/logging.h"
8 #include "chrome/browser/extensions/component_loader.h"
9 #include "chrome/browser/extensions/extension_service.h"
10 #include "chrome/browser/extensions/extension_system.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/profiles/profile_manager.h"
13 #include "chrome/common/extensions/extension_file_util.h"
14 #include "chrome/common/extensions/extension_l10n_util.h"
15 #include "chrome/common/extensions/extension_manifest_constants.h"
16
17 namespace chromeos {
18
19 namespace {
20
21 struct WhitelistedComponentExtensionIME {
22 const char* id;
23 const char* path;
24 } whitelisted_component_extension[] = {
25 {
26 "fpfbhcjppmaeaijcidgiibchfbnhbelj",
27 "/usr/share/chromeos-assets/input_methods/nacl_mozc",
28 },
29 };
30
31 extensions::ComponentLoader* GetComponentLoader() {
32 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
33 extensions::ExtensionSystem* extension_system =
34 extensions::ExtensionSystem::Get(profile);
35 ExtensionService* extension_service = extension_system->extension_service();
36 return extension_service->component_loader();
Zachary Kuznia 2013/03/25 06:33:05 nit: one space after return.
Seigo Nonaka 2013/03/25 09:28:50 Done.
37 }
38 } // namespace
39
40 ComponentExtentionIMEManagerImpl::ComponentExtentionIMEManagerImpl()
41 : is_initialized_(false),
42 weak_ptr_factory_(this) {
43 }
44
45 ComponentExtentionIMEManagerImpl::~ComponentExtentionIMEManagerImpl() {
46 }
47
48 std::vector<ComponentExtensionIME> ComponentExtentionIMEManagerImpl::ListIME() {
49 DCHECK(thread_checker_.CalledOnValidThread());
50 return component_extension_list_;
51 }
52
53 bool ComponentExtentionIMEManagerImpl::Load(const std::string& extension_id,
54 const base::FilePath& file_path) {
55 DCHECK(thread_checker_.CalledOnValidThread());
56 if (loaded_extension_id_.find(extension_id) != loaded_extension_id_.end())
57 return false;
58 const std::string loaded_extension_id =
59 GetComponentLoader()->AddOrReplace(file_path);
60 DCHECK_EQ(loaded_extension_id, extension_id);
61 loaded_extension_id_.insert(extension_id);
62 return true;
63 }
64
65 bool ComponentExtentionIMEManagerImpl::Unload(const std::string& extension_id,
66 const base::FilePath& file_path) {
67 DCHECK(thread_checker_.CalledOnValidThread());
68 if (loaded_extension_id_.find(extension_id) == loaded_extension_id_.end())
69 return false;
70 GetComponentLoader()->Remove(extension_id);
71 loaded_extension_id_.erase(extension_id);
72 return true;
73 }
74
75 scoped_ptr<DictionaryValue> ComponentExtentionIMEManagerImpl::GetManifest(
76 const base::FilePath& file_path) {
77 std::string error;
78 scoped_ptr<DictionaryValue> manifest(
79 extension_file_util::LoadManifest(file_path, &error));
80 if (!manifest.get())
81 LOG(ERROR) << "Failed at getting manifest";
82 if (!extension_l10n_util::LocalizeExtension(file_path,
83 manifest.get(),
84 &error))
85 LOG(ERROR) << "Localization failed";
86
87 return manifest.Pass();
88 }
89
90 void ComponentExtentionIMEManagerImpl::Initialize(
91 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner,
92 const base::Closure& callback) {
93 DCHECK(!is_initialized_);
94 std::vector<ComponentExtensionIME>* component_extension_ime_list
95 = new std::vector<ComponentExtensionIME>;
96 file_task_runner->PostTaskAndReply(
97 FROM_HERE,
98 base::Bind(&ComponentExtentionIMEManagerImpl::ReadComponentExtensionsInfo,
99 base::Unretained(component_extension_ime_list)),
100 base::Bind(
101 &ComponentExtentionIMEManagerImpl::OnReadComponentExtensionsInfo,
102 weak_ptr_factory_.GetWeakPtr(),
103 base::Owned(component_extension_ime_list),
104 callback));
105 }
106
107 bool ComponentExtentionIMEManagerImpl::IsInitialized() {
108 return is_initialized_;
109 }
110
111 bool ComponentExtentionIMEManagerImpl::ReadEngineComponent(
112 const DictionaryValue& dict,
113 IBusComponent::EngineDescription* out) {
Zachary Kuznia 2013/03/25 06:33:05 DCHECK(BrowserThread::FILE)
Seigo Nonaka 2013/03/25 09:28:50 Done.
114 DCHECK(out);
115 std::string type;
116 if (!dict.GetString(extension_manifest_keys::kType, &type))
117 return false;
118 if (type != "ime")
119 return false;
120 if (!dict.GetString(extension_manifest_keys::kId, &out->engine_id))
121 return false;
122 if (!dict.GetString(extension_manifest_keys::kName, &out->display_name))
123 return false;
124 if (!dict.GetString(extension_manifest_keys::kLanguage, &out->language_code))
125 return false;
126
127 const ListValue* layouts = NULL;
128 if (!dict.GetList(extension_manifest_keys::kLayouts, &layouts))
129 return false;
130
131 if (layouts->GetSize() > 0) {
132 if (!layouts->GetString(0, &out->layout))
133 return false;
134 }
135 return true;
136 }
137
138 bool ComponentExtentionIMEManagerImpl::ReadExtensionInfo(
139 const DictionaryValue& manifest,
140 ComponentExtensionIME* out) {
Zachary Kuznia 2013/03/25 06:33:05 DCHECK(BrowserThread::FILE)
Seigo Nonaka 2013/03/25 09:28:50 Done.
141 if (!manifest.GetString(extension_manifest_keys::kDescription,
142 &out->description))
143 return false;
144 // TODO(nona): option page handling.
145 return true;
146 }
147
148 void ComponentExtentionIMEManagerImpl::ReadComponentExtensionsInfo(
Zachary Kuznia 2013/03/25 06:33:05 DCHECK(BrowserThread::FILE)
Seigo Nonaka 2013/03/25 09:28:50 Done.
149 std::vector<ComponentExtensionIME>* out_imes) {
150 DCHECK(out_imes);
151 for (size_t i = 0; i < arraysize(whitelisted_component_extension); ++i) {
152 const base::FilePath extension_path = base::FilePath(
153 whitelisted_component_extension[i].path);
154
155 scoped_ptr<DictionaryValue> manifest = GetManifest(extension_path);
156 if (!manifest.get())
157 continue;
158
159 ComponentExtensionIME component_ime;
160 if (!ReadExtensionInfo(*manifest.get(), &component_ime))
161 continue;
162
163 const ListValue* component_list;
164 if (!manifest->GetList(extension_manifest_keys::kInputComponents,
165 &component_list))
166 continue;
167
168 for (size_t i = 0; i < component_list->GetSize(); ++i) {
169 const DictionaryValue* dictionary;
170 if (!component_list->GetDictionary(i, &dictionary))
171 continue;
172
173 IBusComponent::EngineDescription engine;
174 ReadEngineComponent(*dictionary, &engine);
175 component_ime.engines.push_back(engine);
176 }
177 out_imes->push_back(component_ime);
178 }
179 }
180
181 void ComponentExtentionIMEManagerImpl::OnReadComponentExtensionsInfo(
182 std::vector<ComponentExtensionIME>* result,
183 const base::Closure& callback) {
184 DCHECK(result);
Zachary Kuznia 2013/03/25 06:33:05 DCHECK(BrowserThread::UI), too.
Seigo Nonaka 2013/03/25 09:28:50 Done.
185 component_extension_list_ = *result;
Zachary Kuznia 2013/03/25 06:33:05 This is accessed on other threads, so this could b
Seigo Nonaka 2013/03/25 09:28:50 |component_extension_list_| is accessed on only UI
186 is_initialized_ = true;
187 callback.Run();
188 }
189
190 } // namespace chromeos
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/input_method/component_extension_ime_manager_impl.h ('k') | chrome/chrome_browser_chromeos.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698