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 "ui/base/ime/chromeos/ibus_bridge.h" 6 7 #include <map> 8 #include "base/logging.h" 9 #include "base/memory/singleton.h" 10 11 namespace chromeos { 12 13 static IBusBridge* g_ibus_bridge = NULL; 14 15 // An implementation of IBusBridge. 16 class IBusBridgeImpl : public IBusBridge { 17 public: IBusBridgeImpl()18 IBusBridgeImpl() 19 : input_context_handler_(NULL), 20 engine_handler_(NULL), 21 candidate_window_handler_(NULL) { 22 } 23 ~IBusBridgeImpl()24 virtual ~IBusBridgeImpl() { 25 } 26 27 // IBusBridge override. 28 virtual IBusInputContextHandlerInterface* GetInputContextHandler() const29 GetInputContextHandler() const OVERRIDE { 30 return input_context_handler_; 31 } 32 33 // IBusBridge override. SetInputContextHandler(IBusInputContextHandlerInterface * handler)34 virtual void SetInputContextHandler( 35 IBusInputContextHandlerInterface* handler) OVERRIDE { 36 input_context_handler_ = handler; 37 } 38 39 // IBusBridge override. SetEngineHandler(const std::string & engine_id,IBusEngineHandlerInterface * handler)40 virtual void SetEngineHandler( 41 const std::string& engine_id, 42 IBusEngineHandlerInterface* handler) OVERRIDE { 43 DCHECK(!engine_id.empty()); 44 DCHECK(handler); 45 engine_handler_map_[engine_id] = handler; 46 } 47 48 // IBusBridge override. GetEngineHandler(const std::string & engine_id)49 virtual IBusEngineHandlerInterface* GetEngineHandler( 50 const std::string& engine_id) OVERRIDE { 51 if (engine_id.empty() || 52 engine_handler_map_.find(engine_id) == engine_handler_map_.end()) { 53 return NULL; 54 } 55 return engine_handler_map_[engine_id]; 56 } 57 58 // IBusBridge override. SetCurrentEngineHandler(IBusEngineHandlerInterface * handler)59 virtual void SetCurrentEngineHandler( 60 IBusEngineHandlerInterface* handler) OVERRIDE { 61 engine_handler_ = handler; 62 } 63 64 // IBusBridge override. SetCurrentEngineHandlerById(const std::string & engine_id)65 virtual IBusEngineHandlerInterface* SetCurrentEngineHandlerById( 66 const std::string& engine_id) OVERRIDE { 67 if (engine_id.empty()) { 68 engine_handler_ = NULL; 69 return NULL; 70 } 71 72 DCHECK(engine_handler_map_.find(engine_id) != engine_handler_map_.end()); 73 engine_handler_ = engine_handler_map_[engine_id]; 74 return engine_handler_; 75 } 76 77 // IBusBridge override. GetCurrentEngineHandler() const78 virtual IBusEngineHandlerInterface* GetCurrentEngineHandler() const OVERRIDE { 79 return engine_handler_; 80 } 81 82 // IBusBridge override. 83 virtual IBusPanelCandidateWindowHandlerInterface* GetCandidateWindowHandler() const84 GetCandidateWindowHandler() const OVERRIDE { 85 return candidate_window_handler_; 86 } 87 88 // IBusBridge override. SetCandidateWindowHandler(IBusPanelCandidateWindowHandlerInterface * handler)89 virtual void SetCandidateWindowHandler( 90 IBusPanelCandidateWindowHandlerInterface* handler) OVERRIDE { 91 candidate_window_handler_ = handler; 92 } 93 94 private: 95 IBusInputContextHandlerInterface* input_context_handler_; 96 IBusEngineHandlerInterface* engine_handler_; 97 IBusPanelCandidateWindowHandlerInterface* candidate_window_handler_; 98 std::map<std::string, IBusEngineHandlerInterface*> engine_handler_map_; 99 100 DISALLOW_COPY_AND_ASSIGN(IBusBridgeImpl); 101 }; 102 103 /////////////////////////////////////////////////////////////////////////////// 104 // IBusBridge IBusBridge()105IBusBridge::IBusBridge() { 106 } 107 ~IBusBridge()108IBusBridge::~IBusBridge() { 109 } 110 111 // static. Initialize()112void IBusBridge::Initialize() { 113 if (!g_ibus_bridge) 114 g_ibus_bridge = new IBusBridgeImpl(); 115 } 116 117 // static. Shutdown()118void IBusBridge::Shutdown() { 119 delete g_ibus_bridge; 120 g_ibus_bridge = NULL; 121 } 122 123 // static. Get()124IBusBridge* IBusBridge::Get() { 125 return g_ibus_bridge; 126 } 127 128 } // namespace chromeos 129