| OLD | NEW |
| (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 #include "content/public/browser/browser_thread.h" |
| 17 |
| 18 namespace chromeos { |
| 19 |
| 20 namespace { |
| 21 |
| 22 struct WhitelistedComponentExtensionIME { |
| 23 const char* id; |
| 24 const char* path; |
| 25 } whitelisted_component_extension[] = { |
| 26 { |
| 27 "fpfbhcjppmaeaijcidgiibchfbnhbelj", |
| 28 "/usr/share/chromeos-assets/input_methods/nacl_mozc", |
| 29 }, |
| 30 }; |
| 31 |
| 32 extensions::ComponentLoader* GetComponentLoader() { |
| 33 Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord(); |
| 34 extensions::ExtensionSystem* extension_system = |
| 35 extensions::ExtensionSystem::Get(profile); |
| 36 ExtensionService* extension_service = extension_system->extension_service(); |
| 37 return extension_service->component_loader(); |
| 38 } |
| 39 } // namespace |
| 40 |
| 41 ComponentExtentionIMEManagerImpl::ComponentExtentionIMEManagerImpl() |
| 42 : is_initialized_(false), |
| 43 weak_ptr_factory_(this) { |
| 44 } |
| 45 |
| 46 ComponentExtentionIMEManagerImpl::~ComponentExtentionIMEManagerImpl() { |
| 47 } |
| 48 |
| 49 std::vector<ComponentExtensionIME> ComponentExtentionIMEManagerImpl::ListIME() { |
| 50 DCHECK(thread_checker_.CalledOnValidThread()); |
| 51 return component_extension_list_; |
| 52 } |
| 53 |
| 54 bool ComponentExtentionIMEManagerImpl::Load(const std::string& extension_id, |
| 55 const base::FilePath& file_path) { |
| 56 DCHECK(thread_checker_.CalledOnValidThread()); |
| 57 if (loaded_extension_id_.find(extension_id) != loaded_extension_id_.end()) |
| 58 return false; |
| 59 const std::string loaded_extension_id = |
| 60 GetComponentLoader()->AddOrReplace(file_path); |
| 61 DCHECK_EQ(loaded_extension_id, extension_id); |
| 62 loaded_extension_id_.insert(extension_id); |
| 63 return true; |
| 64 } |
| 65 |
| 66 bool ComponentExtentionIMEManagerImpl::Unload(const std::string& extension_id, |
| 67 const base::FilePath& file_path) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 if (loaded_extension_id_.find(extension_id) == loaded_extension_id_.end()) |
| 70 return false; |
| 71 GetComponentLoader()->Remove(extension_id); |
| 72 loaded_extension_id_.erase(extension_id); |
| 73 return true; |
| 74 } |
| 75 |
| 76 scoped_ptr<DictionaryValue> ComponentExtentionIMEManagerImpl::GetManifest( |
| 77 const base::FilePath& file_path) { |
| 78 std::string error; |
| 79 scoped_ptr<DictionaryValue> manifest( |
| 80 extension_file_util::LoadManifest(file_path, &error)); |
| 81 if (!manifest.get()) |
| 82 LOG(ERROR) << "Failed at getting manifest"; |
| 83 if (!extension_l10n_util::LocalizeExtension(file_path, |
| 84 manifest.get(), |
| 85 &error)) |
| 86 LOG(ERROR) << "Localization failed"; |
| 87 |
| 88 return manifest.Pass(); |
| 89 } |
| 90 |
| 91 void ComponentExtentionIMEManagerImpl::Initialize( |
| 92 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner, |
| 93 const base::Closure& callback) { |
| 94 DCHECK(!is_initialized_); |
| 95 std::vector<ComponentExtensionIME>* component_extension_ime_list |
| 96 = new std::vector<ComponentExtensionIME>; |
| 97 file_task_runner->PostTaskAndReply( |
| 98 FROM_HERE, |
| 99 base::Bind(&ComponentExtentionIMEManagerImpl::ReadComponentExtensionsInfo, |
| 100 base::Unretained(component_extension_ime_list)), |
| 101 base::Bind( |
| 102 &ComponentExtentionIMEManagerImpl::OnReadComponentExtensionsInfo, |
| 103 weak_ptr_factory_.GetWeakPtr(), |
| 104 base::Owned(component_extension_ime_list), |
| 105 callback)); |
| 106 } |
| 107 |
| 108 bool ComponentExtentionIMEManagerImpl::IsInitialized() { |
| 109 return is_initialized_; |
| 110 } |
| 111 |
| 112 // static |
| 113 bool ComponentExtentionIMEManagerImpl::ReadEngineComponent( |
| 114 const DictionaryValue& dict, |
| 115 IBusComponent::EngineDescription* out) { |
| 116 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 117 DCHECK(out); |
| 118 std::string type; |
| 119 if (!dict.GetString(extension_manifest_keys::kType, &type)) |
| 120 return false; |
| 121 if (type != "ime") |
| 122 return false; |
| 123 if (!dict.GetString(extension_manifest_keys::kId, &out->engine_id)) |
| 124 return false; |
| 125 if (!dict.GetString(extension_manifest_keys::kName, &out->display_name)) |
| 126 return false; |
| 127 if (!dict.GetString(extension_manifest_keys::kLanguage, &out->language_code)) |
| 128 return false; |
| 129 |
| 130 const ListValue* layouts = NULL; |
| 131 if (!dict.GetList(extension_manifest_keys::kLayouts, &layouts)) |
| 132 return false; |
| 133 |
| 134 if (layouts->GetSize() > 0) { |
| 135 if (!layouts->GetString(0, &out->layout)) |
| 136 return false; |
| 137 } |
| 138 return true; |
| 139 } |
| 140 |
| 141 // static |
| 142 bool ComponentExtentionIMEManagerImpl::ReadExtensionInfo( |
| 143 const DictionaryValue& manifest, |
| 144 ComponentExtensionIME* out) { |
| 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 146 if (!manifest.GetString(extension_manifest_keys::kDescription, |
| 147 &out->description)) |
| 148 return false; |
| 149 // TODO(nona): option page handling. |
| 150 return true; |
| 151 } |
| 152 |
| 153 // static |
| 154 void ComponentExtentionIMEManagerImpl::ReadComponentExtensionsInfo( |
| 155 std::vector<ComponentExtensionIME>* out_imes) { |
| 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 157 DCHECK(out_imes); |
| 158 for (size_t i = 0; i < arraysize(whitelisted_component_extension); ++i) { |
| 159 const base::FilePath extension_path = base::FilePath( |
| 160 whitelisted_component_extension[i].path); |
| 161 |
| 162 scoped_ptr<DictionaryValue> manifest = GetManifest(extension_path); |
| 163 if (!manifest.get()) |
| 164 continue; |
| 165 |
| 166 ComponentExtensionIME component_ime; |
| 167 if (!ReadExtensionInfo(*manifest.get(), &component_ime)) |
| 168 continue; |
| 169 |
| 170 const ListValue* component_list; |
| 171 if (!manifest->GetList(extension_manifest_keys::kInputComponents, |
| 172 &component_list)) |
| 173 continue; |
| 174 |
| 175 for (size_t i = 0; i < component_list->GetSize(); ++i) { |
| 176 const DictionaryValue* dictionary; |
| 177 if (!component_list->GetDictionary(i, &dictionary)) |
| 178 continue; |
| 179 |
| 180 IBusComponent::EngineDescription engine; |
| 181 ReadEngineComponent(*dictionary, &engine); |
| 182 component_ime.engines.push_back(engine); |
| 183 } |
| 184 out_imes->push_back(component_ime); |
| 185 } |
| 186 } |
| 187 |
| 188 void ComponentExtentionIMEManagerImpl::OnReadComponentExtensionsInfo( |
| 189 std::vector<ComponentExtensionIME>* result, |
| 190 const base::Closure& callback) { |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); |
| 192 DCHECK(result); |
| 193 component_extension_list_ = *result; |
| 194 is_initialized_ = true; |
| 195 callback.Run(); |
| 196 } |
| 197 |
| 198 } // namespace chromeos |
| OLD | NEW |