• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #ifndef UI_BASE_IME_INPUT_METHOD_FACTORY_H_
6 #define UI_BASE_IME_INPUT_METHOD_FACTORY_H_
7 
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "ui/base/ime/input_method_initializer.h"
12 #include "ui/base/ui_export.h"
13 #include "ui/gfx/native_widget_types.h"
14 
15 template <typename T> struct DefaultSingletonTraits;
16 
17 namespace ui {
18 namespace internal {
19 class InputMethodDelegate;
20 }  // namespace internal
21 
22 class InputMethod;
23 
24 class UI_EXPORT InputMethodFactory {
25  public:
26   // Returns the current active factory.
27   // If no factory was set, sets the DefaultInputMethodFactory by default.  Once
28   // a factory was set, you cannot change the factory, and always the same
29   // factory is returned.
30   static InputMethodFactory* GetInstance();
31 
32   // Sets an InputMethodFactory to be used.
33   // This function must be called at most once.  |instance| is not owned by this
34   // class or marked automatically as a leaky object.  It's a caller's duty to
35   // destroy the object or mark it as leaky.
36   static void SetInstance(InputMethodFactory* instance);
37 
~InputMethodFactory()38   virtual ~InputMethodFactory() {}
39 
40   // Creates and returns an input method implementation.
41   virtual scoped_ptr<InputMethod> CreateInputMethod(
42       internal::InputMethodDelegate* delegate,
43       gfx::AcceleratedWidget widget) = 0;
44 
45  private:
46   static void ClearInstance();
47 
48   friend UI_EXPORT void ShutdownInputMethod();
49   friend UI_EXPORT void ShutdownInputMethodForTesting();
50 };
51 
52 class DefaultInputMethodFactory : public InputMethodFactory {
53  public:
54   // For Singleton
55   static DefaultInputMethodFactory* GetInstance();
56 
57   // Overridden from InputMethodFactory.
58   virtual scoped_ptr<InputMethod> CreateInputMethod(
59       internal::InputMethodDelegate* delegate,
60       gfx::AcceleratedWidget widget) OVERRIDE;
61 
62  private:
DefaultInputMethodFactory()63   DefaultInputMethodFactory() {}
64 
65   friend struct DefaultSingletonTraits<DefaultInputMethodFactory>;
66 
67   DISALLOW_COPY_AND_ASSIGN(DefaultInputMethodFactory);
68 };
69 
70 class MockInputMethodFactory : public InputMethodFactory {
71  public:
72   // For Singleton
73   static MockInputMethodFactory* GetInstance();
74 
75   // Overridden from InputMethodFactory.
76   virtual scoped_ptr<InputMethod> CreateInputMethod(
77       internal::InputMethodDelegate* delegate,
78       gfx::AcceleratedWidget widget) OVERRIDE;
79 
80  private:
81   MockInputMethodFactory() {}
82 
83   friend struct DefaultSingletonTraits<MockInputMethodFactory>;
84 
85   DISALLOW_COPY_AND_ASSIGN(MockInputMethodFactory);
86 };
87 
88 // Shorthand for
89 // InputMethodFactory::GetInstance()->CreateInputMethod(delegate, widget).
90 UI_EXPORT scoped_ptr<InputMethod> CreateInputMethod(
91     internal::InputMethodDelegate* delegate,
92     gfx::AcceleratedWidget widget);
93 
94 // Shorthand for InputMethodFactory::SetInstance(new MockInputMethodFactory()).
95 // TODO(yukishiino): Retires this shorthand, and makes ui::InitializeInputMethod
96 // and ui::InitializeInputMethodForTesting set the appropriate factory.
97 UI_EXPORT void SetUpInputMethodFactoryForTesting();
98 
99 #if defined(OS_WIN)
100 // Returns a shared input method object for the platform. Caller must not
101 // delete the object. Currently supported only on Windows. This method is
102 // for non-Aura environment, where only one input method object is created for
103 // the browser process.
104 UI_EXPORT InputMethod* GetSharedInputMethod();
105 
106 namespace internal {
107 // Destroys the shared input method object returned by GetSharedInputMethod().
108 // This function must be called only from input_method_initializer.cc.
109 void DestroySharedInputMethod();
110 }  // namespace internal
111 #endif
112 
113 }  // namespace ui;
114 
115 #endif  // UI_BASE_IME_INPUT_METHOD_FACTORY_H_
116