Home | History | Annotate | Download | only in ime
      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 "chromeos/ime/component_extension_ime_manager.h"
      6 
      7 #include "base/logging.h"
      8 #include "base/strings/string_util.h"
      9 #include "chromeos/ime/extension_ime_util.h"
     10 
     11 namespace chromeos {
     12 
     13 ComponentExtensionEngine::ComponentExtensionEngine() {
     14 }
     15 
     16 ComponentExtensionEngine::~ComponentExtensionEngine() {
     17 }
     18 
     19 ComponentExtensionIME::ComponentExtensionIME() {
     20 }
     21 
     22 ComponentExtensionIME::~ComponentExtensionIME() {
     23 }
     24 
     25 ComponentExtensionIMEManagerDelegate::ComponentExtensionIMEManagerDelegate() {
     26 }
     27 
     28 ComponentExtensionIMEManagerDelegate::~ComponentExtensionIMEManagerDelegate() {
     29 }
     30 
     31 ComponentExtensionIMEManager::ComponentExtensionIMEManager()
     32     : is_initialized_(false) {
     33 }
     34 
     35 ComponentExtensionIMEManager::~ComponentExtensionIMEManager() {
     36 }
     37 
     38 void ComponentExtensionIMEManager::Initialize(
     39     scoped_ptr<ComponentExtensionIMEManagerDelegate> delegate) {
     40   delegate_ = delegate.Pass();
     41   component_extension_imes_ = delegate_->ListIME();
     42   is_initialized_ = true;
     43   FOR_EACH_OBSERVER(Observer, observers_, OnInitialized());
     44 }
     45 
     46 bool ComponentExtensionIMEManager::IsInitialized() {
     47   return is_initialized_;
     48 }
     49 
     50 bool ComponentExtensionIMEManager::LoadComponentExtensionIME(
     51     const std::string& input_method_id) {
     52   ComponentExtensionIME ime;
     53   if (FindEngineEntry(input_method_id, &ime, NULL))
     54     return delegate_->Load(ime.id, ime.manifest, ime.path);
     55   else
     56     return false;
     57 }
     58 
     59 bool ComponentExtensionIMEManager::UnloadComponentExtensionIME(
     60     const std::string& input_method_id) {
     61   ComponentExtensionIME ime;
     62   if (FindEngineEntry(input_method_id, &ime, NULL))
     63     return delegate_->Unload(ime.id, ime.path);
     64   else
     65     return false;
     66 }
     67 
     68 bool ComponentExtensionIMEManager::IsWhitelisted(
     69     const std::string& input_method_id) {
     70   return extension_ime_util::IsComponentExtensionIME(input_method_id) &&
     71       FindEngineEntry(input_method_id, NULL, NULL);
     72 }
     73 
     74 bool ComponentExtensionIMEManager::IsWhitelistedExtension(
     75     const std::string& extension_id) {
     76   for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
     77     if (component_extension_imes_[i].id == extension_id)
     78       return true;
     79   }
     80   return false;
     81 }
     82 
     83 std::string ComponentExtensionIMEManager::GetId(
     84     const std::string& extension_id,
     85     const std::string& engine_id) {
     86   ComponentExtensionEngine engine;
     87   const std::string& input_method_id =
     88       extension_ime_util::GetComponentInputMethodID(extension_id, engine_id);
     89   if (!FindEngineEntry(input_method_id, NULL, &engine))
     90     return "";
     91   return input_method_id;
     92 }
     93 
     94 std::string ComponentExtensionIMEManager::GetName(
     95     const std::string& input_method_id) {
     96   ComponentExtensionEngine engine;
     97   if (!FindEngineEntry(input_method_id, NULL, &engine))
     98     return "";
     99   return engine.display_name;
    100 }
    101 
    102 std::string ComponentExtensionIMEManager::GetDescription(
    103     const std::string& input_method_id) {
    104   ComponentExtensionEngine engine;
    105   if (!FindEngineEntry(input_method_id, NULL, &engine))
    106     return "";
    107   return engine.description;
    108 }
    109 
    110 std::vector<std::string> ComponentExtensionIMEManager::ListIMEByLanguage(
    111     const std::string& language) {
    112   std::vector<std::string> result;
    113   for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
    114     for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) {
    115       const ComponentExtensionIME& ime = component_extension_imes_[i];
    116       if (std::find(ime.engines[j].language_codes.begin(),
    117                     ime.engines[j].language_codes.end(),
    118                     language) != ime.engines[j].language_codes.end()) {
    119         result.push_back(extension_ime_util::GetComponentInputMethodID(
    120             ime.id,
    121             ime.engines[j].engine_id));
    122       }
    123     }
    124   }
    125   return result;
    126 }
    127 
    128 input_method::InputMethodDescriptors
    129     ComponentExtensionIMEManager::GetAllIMEAsInputMethodDescriptor() {
    130   input_method::InputMethodDescriptors result;
    131   for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
    132     for (size_t j = 0; j < component_extension_imes_[i].engines.size(); ++j) {
    133       result.push_back(
    134           input_method::InputMethodDescriptor(
    135               extension_ime_util::GetComponentInputMethodID(
    136                   component_extension_imes_[i].id,
    137                   component_extension_imes_[i].engines[j].engine_id),
    138               component_extension_imes_[i].engines[j].display_name,
    139               component_extension_imes_[i].engines[j].layouts,
    140               component_extension_imes_[i].engines[j].language_codes,
    141               false,  // Do not use IME on login screen.
    142               component_extension_imes_[i].options_page_url,
    143               component_extension_imes_[i].input_view_url));
    144     }
    145   }
    146   return result;
    147 }
    148 
    149 void ComponentExtensionIMEManager::AddObserver(Observer* observer) {
    150   observers_.AddObserver(observer);
    151 }
    152 
    153 void ComponentExtensionIMEManager::RemoveObserver(Observer* observer) {
    154   observers_.RemoveObserver(observer);
    155 }
    156 
    157 bool ComponentExtensionIMEManager::FindEngineEntry(
    158     const std::string& input_method_id,
    159     ComponentExtensionIME* out_extension,
    160     ComponentExtensionEngine* out_engine) {
    161   if (!extension_ime_util::IsComponentExtensionIME(input_method_id))
    162     return false;
    163   for (size_t i = 0; i < component_extension_imes_.size(); ++i) {
    164     const std::string extension_id = component_extension_imes_[i].id;
    165     const std::vector<ComponentExtensionEngine>& engines =
    166         component_extension_imes_[i].engines;
    167 
    168     for (size_t j = 0; j < engines.size(); ++j) {
    169       const std::string trial_ime_id =
    170           extension_ime_util::GetComponentInputMethodID(
    171               extension_id, engines[j].engine_id);
    172       if (trial_ime_id != input_method_id)
    173         continue;
    174 
    175       if (out_extension)
    176         *out_extension = component_extension_imes_[i];
    177       if (out_engine)
    178         *out_engine = component_extension_imes_[i].engines[j];
    179       return true;
    180     }
    181   }
    182   return false;
    183 }
    184 
    185 }  // namespace chromeos
    186