Home | History | Annotate | Download | only in ime
      1 // Copyright (c) 2012 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/ibus_bridge.h"
      6 
      7 #include <map>
      8 #include "base/logging.h"
      9 #include "base/memory/singleton.h"
     10 
     11 namespace chromeos {
     12 
     13 namespace {
     14 void NoOpCreateEngineReply(const dbus::ObjectPath& unused_path) {}
     15 }  // namespace
     16 
     17 static IBusBridge* g_ibus_bridge = NULL;
     18 
     19 // An implementation of IBusBridge.
     20 class IBusBridgeImpl : public IBusBridge {
     21  public:
     22   IBusBridgeImpl()
     23     : input_context_handler_(NULL),
     24       engine_handler_(NULL),
     25       candidate_window_handler_(NULL),
     26       panel_handler_(NULL) {
     27   }
     28 
     29   virtual ~IBusBridgeImpl() {
     30   }
     31 
     32   // IBusBridge override.
     33   virtual IBusInputContextHandlerInterface*
     34       GetInputContextHandler() const OVERRIDE {
     35     return input_context_handler_;
     36   }
     37 
     38   // IBusBridge override.
     39   virtual void SetInputContextHandler(
     40       IBusInputContextHandlerInterface* handler) OVERRIDE {
     41     input_context_handler_ = handler;
     42   }
     43 
     44   // IBusBridge override.
     45   virtual IBusEngineHandlerInterface* GetEngineHandler() const OVERRIDE {
     46     return engine_handler_;
     47   }
     48 
     49   // IBusBridge override.
     50   virtual void SetEngineHandler(IBusEngineHandlerInterface* handler) OVERRIDE {
     51     engine_handler_ = handler;
     52   }
     53 
     54   // IBusBridge override.
     55   virtual IBusPanelCandidateWindowHandlerInterface*
     56   GetCandidateWindowHandler() const OVERRIDE {
     57     return candidate_window_handler_;
     58   }
     59 
     60   // IBusBridge override.
     61   virtual void SetCandidateWindowHandler(
     62       IBusPanelCandidateWindowHandlerInterface* handler) OVERRIDE {
     63     candidate_window_handler_ = handler;
     64   }
     65 
     66   // IBusBridge override.
     67   virtual IBusPanelPropertyHandlerInterface*
     68       GetPropertyHandler() const OVERRIDE {
     69     return panel_handler_;
     70   }
     71 
     72   // IBusBridge override.
     73   virtual void SetPropertyHandler(
     74       IBusPanelPropertyHandlerInterface* handler) OVERRIDE {
     75     panel_handler_ = handler;
     76   }
     77 
     78   virtual void SetCreateEngineHandler(
     79       const std::string& engine_id,
     80       const IBusEngineFactoryService::CreateEngineHandler& handler) OVERRIDE {
     81     create_engine_handler_map_[engine_id] = handler;
     82   }
     83 
     84   // IBusBridge override.
     85   virtual void UnsetCreateEngineHandler(const std::string& engine_id) OVERRIDE {
     86     create_engine_handler_map_.erase(engine_id);
     87   }
     88 
     89   // IBusBridge override.
     90   virtual void CreateEngine(const std::string& engine_id) OVERRIDE {
     91     // TODO(nona): Change following condition to DCHECK once all legacy IME is
     92     // migrated to extension IME.
     93     if (create_engine_handler_map_[engine_id].is_null())
     94       return;
     95     create_engine_handler_map_[engine_id].Run(
     96         base::Bind(&NoOpCreateEngineReply));
     97   }
     98 
     99  private:
    100   IBusInputContextHandlerInterface* input_context_handler_;
    101   IBusEngineHandlerInterface* engine_handler_;
    102   IBusPanelCandidateWindowHandlerInterface* candidate_window_handler_;
    103   IBusPanelPropertyHandlerInterface* panel_handler_;
    104   std::map<std::string, IBusEngineFactoryService::CreateEngineHandler>
    105       create_engine_handler_map_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(IBusBridgeImpl);
    108 };
    109 
    110 ///////////////////////////////////////////////////////////////////////////////
    111 // IBusBridge
    112 IBusBridge::IBusBridge() {
    113 }
    114 
    115 IBusBridge::~IBusBridge() {
    116 }
    117 
    118 // static.
    119 void IBusBridge::Initialize() {
    120   CHECK(!g_ibus_bridge) << "Already initialized.";
    121   g_ibus_bridge = new IBusBridgeImpl();
    122 }
    123 
    124 // static.
    125 void IBusBridge::Shutdown() {
    126   CHECK(g_ibus_bridge) << "Shutdown called before Initialize().";
    127   delete g_ibus_bridge;
    128   g_ibus_bridge = NULL;
    129 }
    130 
    131 // static.
    132 IBusBridge* IBusBridge::Get() {
    133   return g_ibus_bridge;
    134 }
    135 
    136 }  // namespace chromeos
    137