summaryrefslogtreecommitdiff
path: root/chromium/ui/base/ime/input_method_factory.cc
blob: 616dd22bd24f9773e9d4e1abb9db263e96556a4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ui/base/ime/input_method_factory.h"

#include "base/memory/singleton.h"
#include "ui/base/ime/mock_input_method.h"

#if defined(OS_CHROMEOS) && defined(USE_X11)
#include "ui/base/ime/input_method_ibus.h"
#elif defined(OS_WIN)
#include "base/win/metro.h"
#include "ui/base/ime/input_method_imm32.h"
#include "ui/base/ime/input_method_tsf.h"
#include "ui/base/ime/remote_input_method_win.h"
#elif defined(USE_AURA) && defined(USE_X11)
#include "ui/base/ime/input_method_auralinux.h"
#else
#include "ui/base/ime/input_method_minimal.h"
#endif

namespace {

ui::InputMethodFactory* g_input_method_factory = NULL;

#if defined(OS_WIN)
ui::InputMethod* g_shared_input_method = NULL;
#endif

}  // namespace

namespace ui {

// static
InputMethodFactory* InputMethodFactory::GetInstance() {
  if (!g_input_method_factory)
    SetInstance(DefaultInputMethodFactory::GetInstance());

  return g_input_method_factory;
}

// static
void InputMethodFactory::SetInstance(InputMethodFactory* instance) {
  CHECK(!g_input_method_factory);
  CHECK(instance);

  g_input_method_factory = instance;
}

// static
void InputMethodFactory::ClearInstance() {
  // It's a client's duty to delete the object.
  g_input_method_factory = NULL;
}

// DefaultInputMethodFactory

// static
DefaultInputMethodFactory* DefaultInputMethodFactory::GetInstance() {
  return Singleton<DefaultInputMethodFactory>::get();
}

scoped_ptr<InputMethod> DefaultInputMethodFactory::CreateInputMethod(
    internal::InputMethodDelegate* delegate,
    gfx::AcceleratedWidget widget) {
#if defined(OS_CHROMEOS) && defined(USE_X11)
  return scoped_ptr<InputMethod>(new InputMethodIBus(delegate));
#elif defined(OS_WIN)
  if (base::win::IsTSFAwareRequired())
    return scoped_ptr<InputMethod>(new InputMethodTSF(delegate, widget));
  if (IsRemoteInputMethodWinRequired(widget))
    return CreateRemoteInputMethodWin(delegate);
  return scoped_ptr<InputMethod>(new InputMethodIMM32(delegate, widget));
#elif defined(USE_AURA) && defined(USE_X11)
  return scoped_ptr<InputMethod>(new InputMethodAuraLinux(delegate));
#else
  return scoped_ptr<InputMethod>(new InputMethodMinimal(delegate));
#endif
}

// MockInputMethodFactory

// static
MockInputMethodFactory* MockInputMethodFactory::GetInstance() {
  return Singleton<MockInputMethodFactory>::get();
}

scoped_ptr<InputMethod> MockInputMethodFactory::CreateInputMethod(
    internal::InputMethodDelegate* delegate,
    gfx::AcceleratedWidget /* widget */) {
  return scoped_ptr<InputMethod>(new MockInputMethod(delegate));
}

// Shorthands

scoped_ptr<InputMethod> CreateInputMethod(
    internal::InputMethodDelegate* delegate,
    gfx::AcceleratedWidget widget) {
  return InputMethodFactory::GetInstance()->CreateInputMethod(delegate, widget);
}

void SetUpInputMethodFactoryForTesting() {
  InputMethodFactory::SetInstance(MockInputMethodFactory::GetInstance());
}

#if defined(OS_WIN)
InputMethod* GetSharedInputMethod() {
  if (!g_shared_input_method)
    g_shared_input_method = CreateInputMethod(NULL, NULL).release();
  return g_shared_input_method;
}

namespace internal {

void DestroySharedInputMethod() {
  delete g_shared_input_method;
  g_shared_input_method = NULL;
}

}  // namespace internal
#endif

}  // namespace ui