• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ui/base/ime/input_method_factory.h"
6 
7 #include "base/memory/singleton.h"
8 #include "ui/base/ime/mock_input_method.h"
9 
10 #if defined(OS_CHROMEOS) && defined(USE_X11)
11 #include "ui/base/ime/input_method_ibus.h"
12 #elif defined(OS_WIN)
13 #include "base/win/metro.h"
14 #include "ui/base/ime/input_method_imm32.h"
15 #include "ui/base/ime/input_method_tsf.h"
16 #include "ui/base/ime/remote_input_method_win.h"
17 #elif defined(USE_AURA) && defined(USE_X11)
18 #include "ui/base/ime/input_method_auralinux.h"
19 #else
20 #include "ui/base/ime/input_method_minimal.h"
21 #endif
22 
23 namespace {
24 
25 ui::InputMethodFactory* g_input_method_factory = NULL;
26 
27 #if defined(OS_WIN)
28 ui::InputMethod* g_shared_input_method = NULL;
29 #endif
30 
31 }  // namespace
32 
33 namespace ui {
34 
35 // static
GetInstance()36 InputMethodFactory* InputMethodFactory::GetInstance() {
37   if (!g_input_method_factory)
38     SetInstance(DefaultInputMethodFactory::GetInstance());
39 
40   return g_input_method_factory;
41 }
42 
43 // static
SetInstance(InputMethodFactory * instance)44 void InputMethodFactory::SetInstance(InputMethodFactory* instance) {
45   CHECK(!g_input_method_factory);
46   CHECK(instance);
47 
48   g_input_method_factory = instance;
49 }
50 
51 // static
ClearInstance()52 void InputMethodFactory::ClearInstance() {
53   // It's a client's duty to delete the object.
54   g_input_method_factory = NULL;
55 }
56 
57 // DefaultInputMethodFactory
58 
59 // static
GetInstance()60 DefaultInputMethodFactory* DefaultInputMethodFactory::GetInstance() {
61   return Singleton<DefaultInputMethodFactory>::get();
62 }
63 
CreateInputMethod(internal::InputMethodDelegate * delegate,gfx::AcceleratedWidget widget)64 scoped_ptr<InputMethod> DefaultInputMethodFactory::CreateInputMethod(
65     internal::InputMethodDelegate* delegate,
66     gfx::AcceleratedWidget widget) {
67 #if defined(OS_CHROMEOS) && defined(USE_X11)
68   return scoped_ptr<InputMethod>(new InputMethodIBus(delegate));
69 #elif defined(OS_WIN)
70   if (base::win::IsTSFAwareRequired())
71     return scoped_ptr<InputMethod>(new InputMethodTSF(delegate, widget));
72   if (IsRemoteInputMethodWinRequired(widget))
73     return CreateRemoteInputMethodWin(delegate);
74   return scoped_ptr<InputMethod>(new InputMethodIMM32(delegate, widget));
75 #elif defined(USE_AURA) && defined(USE_X11)
76   return scoped_ptr<InputMethod>(new InputMethodAuraLinux(delegate));
77 #else
78   return scoped_ptr<InputMethod>(new InputMethodMinimal(delegate));
79 #endif
80 }
81 
82 // MockInputMethodFactory
83 
84 // static
GetInstance()85 MockInputMethodFactory* MockInputMethodFactory::GetInstance() {
86   return Singleton<MockInputMethodFactory>::get();
87 }
88 
CreateInputMethod(internal::InputMethodDelegate * delegate,gfx::AcceleratedWidget)89 scoped_ptr<InputMethod> MockInputMethodFactory::CreateInputMethod(
90     internal::InputMethodDelegate* delegate,
91     gfx::AcceleratedWidget /* widget */) {
92   return scoped_ptr<InputMethod>(new MockInputMethod(delegate));
93 }
94 
95 // Shorthands
96 
CreateInputMethod(internal::InputMethodDelegate * delegate,gfx::AcceleratedWidget widget)97 scoped_ptr<InputMethod> CreateInputMethod(
98     internal::InputMethodDelegate* delegate,
99     gfx::AcceleratedWidget widget) {
100   return InputMethodFactory::GetInstance()->CreateInputMethod(delegate, widget);
101 }
102 
SetUpInputMethodFactoryForTesting()103 void SetUpInputMethodFactoryForTesting() {
104   InputMethodFactory::SetInstance(MockInputMethodFactory::GetInstance());
105 }
106 
107 #if defined(OS_WIN)
GetSharedInputMethod()108 InputMethod* GetSharedInputMethod() {
109   if (!g_shared_input_method)
110     g_shared_input_method = CreateInputMethod(NULL, NULL).release();
111   return g_shared_input_method;
112 }
113 
114 namespace internal {
115 
DestroySharedInputMethod()116 void DestroySharedInputMethod() {
117   delete g_shared_input_method;
118   g_shared_input_method = NULL;
119 }
120 
121 }  // namespace internal
122 #endif
123 
124 }  // namespace ui
125