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.
5 #ifndef CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_CONTROLLER_IMPL_H_
6 #define CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_CONTROLLER_IMPL_H_
8 #include <gio/gio.h> // GAsyncResult and related types.
9 #include <glib-object.h>
14 #include "base/process_util.h"
15 #include "chrome/browser/chromeos/input_method/ibus_controller_base.h"
16 #include "chrome/browser/chromeos/input_method/input_method_whitelist.h"
17 #include "ui/base/glib/glib_signal.h"
19 // Do not #include ibus.h here. That makes it impossible to compile unit tests
23 struct _IBusPanelService
;
26 typedef struct _IBusBus IBusBus
;
27 typedef struct _IBusConfig IBusConfig
;
28 typedef struct _IBusPanelService IBusPanelService
;
29 typedef struct _IBusPropList IBusPropList
;
30 typedef struct _IBusProperty IBusProperty
;
33 class InputMethodIBus
;
37 namespace input_method
{
39 struct InputMethodConfigValue
;
40 struct InputMethodProperty
;
41 typedef std::vector
<InputMethodProperty
> InputMethodPropertyList
;
43 // The IBusController implementation.
44 class IBusControllerImpl
: public IBusControllerBase
{
47 virtual ~IBusControllerImpl();
49 // IBusController overrides:
50 virtual bool Start() OVERRIDE
;
51 virtual void Reset() OVERRIDE
;
52 virtual bool Stop() OVERRIDE
;
53 virtual bool ChangeInputMethod(const std::string
& id
) OVERRIDE
;
54 virtual bool ActivateInputMethodProperty(const std::string
& key
) OVERRIDE
;
56 // Calls <anonymous_namespace>::FindAndUpdateProperty. This method is just for
58 static bool FindAndUpdatePropertyForTesting(
59 const InputMethodProperty
& new_prop
,
60 InputMethodPropertyList
* prop_list
);
62 static void IBusDaemonInitializationDone(IBusControllerImpl
* controller
,
63 const std::string
& ibus_address
);
66 enum IBusDaemonStatus
{
67 IBUS_DAEMON_INITIALIZING
,
69 IBUS_DAEMON_SHUTTING_DOWN
,
73 // Functions that end with Thunk are used to deal with glib callbacks.
74 CHROMEG_CALLBACK_0(IBusControllerImpl
, void, BusConnected
, IBusBus
*);
75 CHROMEG_CALLBACK_0(IBusControllerImpl
, void, BusDisconnected
, IBusBus
*);
76 CHROMEG_CALLBACK_3(IBusControllerImpl
, void, BusNameOwnerChanged
,
77 IBusBus
*, const gchar
*, const gchar
*, const gchar
*);
78 CHROMEG_CALLBACK_1(IBusControllerImpl
, void, FocusIn
,
79 IBusPanelService
*, const gchar
*);
80 CHROMEG_CALLBACK_1(IBusControllerImpl
, void, RegisterProperties
,
81 IBusPanelService
*, IBusPropList
*);
82 CHROMEG_CALLBACK_1(IBusControllerImpl
, void, UpdateProperty
,
83 IBusPanelService
*, IBusProperty
*);
85 // IBusControllerBase overrides:
86 virtual bool SetInputMethodConfigInternal(
87 const ConfigKeyType
& key
,
88 const InputMethodConfigValue
& value
) OVERRIDE
;
90 // Checks if |ibus_| and |ibus_config_| connections are alive.
91 bool IBusConnectionsAreAlive();
93 // Restores connections to ibus-daemon and ibus-memconf if they are not ready.
94 void MaybeRestoreConnections();
96 // Initializes IBusBus object if it's not yet done.
97 void MaybeInitializeIBusBus();
99 // Creates IBusConfig object if it's not created yet AND |ibus_| connection
101 void MaybeRestoreIBusConfig();
103 // Destroys IBusConfig object if |ibus_| connection is not ready. This
104 // function does nothing if |ibus_config_| is NULL or |ibus_| connection is
105 // still alive. Note that the IBusConfig object can't be used when |ibus_|
106 // connection is not ready.
107 void MaybeDestroyIBusConfig();
109 // Just calls ibus_bus_set_global_engine_async() with the |id|.
110 void SendChangeInputMethodRequest(const std::string
& id
);
112 // Calls SetInputMethodConfigInternal() for each |current_config_values_|.
113 void SendAllInputMethodConfigs();
115 // Starts listening to the "connected", "disconnected", and
116 // "name-owner-changed" D-Bus signals from ibus-daemon.
117 void ConnectBusSignals();
119 // Starts listening to the "focus-in", "register-properties", and
120 // "update-property" D-Bus signals from ibus-daemon.
121 void ConnectPanelServiceSignals();
123 // Adds address file watcher in FILE thread and then calls LaunchIBusDaemon.
124 bool StartIBusDaemon();
126 // Starts ibus-daemon.
127 void LaunchIBusDaemon(const std::string
& ibus_address
);
129 // Launches an input method procsess specified by the given command
130 // line. On success, returns true and stores the process handle in
131 // |process_handle|. Otherwise, returns false, and the contents of
132 // |process_handle| is untouched. |watch_func| will be called when the
133 // process terminates.
134 bool LaunchProcess(const std::string
& command_line
,
135 base::ProcessHandle
* process_handle
,
136 GChildWatchFunc watch_func
);
138 // Returns pointer to InputMethod object.
139 ui::InputMethodIBus
* GetInputMethod();
141 // Injects an alternative ui::InputMethod for testing.
142 // The injected object must be released by caller.
143 void set_input_method_for_testing(ui::InputMethodIBus
* input_method
);
145 // A callback function that will be called when ibus_config_set_value_async()
146 // request is finished.
147 static void SetInputMethodConfigCallback(GObject
* source_object
,
151 // Called when the input method process is shut down.
152 static void OnIBusDaemonExit(GPid pid
,
154 IBusControllerImpl
* controller
);
156 // Connection to the ibus-daemon via IBus API. These objects are used to
157 // call ibus-daemon's API (e.g. activate input methods, set config, ...)
159 IBusConfig
* ibus_config_
;
161 // The current ibus_daemon address. This value is assigned at the launching
162 // ibus-daemon and used in bus connection initialization.
163 std::string ibus_daemon_address_
;
165 // The process handle of the IBus daemon. kNullProcessHandle if it's not
167 base::ProcessHandle process_handle_
;
169 // Current input context path.
170 std::string current_input_context_path_
;
172 // The input method ID which is currently selected. The ID is sent to the
173 // daemon when |ibus_| and |ibus_config_| connections are both established.
174 std::string current_input_method_id_
;
176 // An object which knows all valid input methods and layout IDs.
177 InputMethodWhitelist whitelist_
;
179 // Represents ibus-daemon's status.
180 IBusDaemonStatus ibus_daemon_status_
;
182 // The pointer to global input method. We can inject this value for testing.
183 ui::InputMethodIBus
* input_method_
;
185 // Used for making callbacks for PostTask.
186 base::WeakPtrFactory
<IBusControllerImpl
> weak_ptr_factory_
;
188 DISALLOW_COPY_AND_ASSIGN(IBusControllerImpl
);
191 } // namespace input_method
192 } // namespace chromeos
194 #endif // CHROME_BROWSER_CHROMEOS_INPUT_METHOD_IBUS_CONTROLLER_IMPL_H_