summaryrefslogtreecommitdiffstats
path: root/chromeos/ime/ibus_daemon_controller.cc
blob: 83504255da2d4c3f61e662a42320e9977431e985 (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// 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 "chromeos/ime/ibus_daemon_controller.h"

#include "base/bind.h"
#include "base/chromeos/chromeos_version.h"
#include "base/environment.h"
#include "base/files/file_path_watcher.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/observer_list.h"
#include "base/process_util.h"
#include "base/rand_util.h"
#include "base/string_util.h"
#include "base/stringprintf.h"
#include "base/threading/thread_checker.h"
#include "chromeos/dbus/dbus_thread_manager.h"

namespace chromeos {

namespace {

IBusDaemonController* g_ibus_daemon_controller = NULL;
base::FilePathWatcher* g_file_path_watcher = NULL;

// Called when the ibus-daemon address file is modified.
static void OnFilePathChanged(
    const scoped_refptr<base::SequencedTaskRunner> ui_task_runner,
    const base::Closure& closure,
    const base::FilePath& file_path,
    bool failed) {
  if (failed)
    return;  // Can't recover, do nothing.
  if (!g_file_path_watcher)
    return;  // Already discarded watch task.

  ui_task_runner->PostTask(FROM_HERE, closure);
  ui_task_runner->DeleteSoon(FROM_HERE, g_file_path_watcher);
  g_file_path_watcher = NULL;
}

// Start watching |address_file_path|. If the target file is changed, |callback|
// is called on UI thread. This function should be called on FILE thread.
void StartWatch(
    const std::string& address_file_path,
    const base::Closure& closure,
    const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner) {
  // Before start watching, discard on-going watching task.
  delete g_file_path_watcher;
  g_file_path_watcher = new base::FilePathWatcher;
  bool result = g_file_path_watcher->Watch(
      base::FilePath::FromUTF8Unsafe(address_file_path),
      false,  // do not watch child directory.
      base::Bind(&OnFilePathChanged,
                 ui_task_runner,
                 closure));
  DCHECK(result);
}

// The implementation of IBusDaemonController.
class IBusDaemonControllerImpl : public IBusDaemonController {
 public:
  // Represents current ibus-daemon status.
  enum IBusDaemonStatus {
    IBUS_DAEMON_INITIALIZING,
    IBUS_DAEMON_RUNNING,
    IBUS_DAEMON_SHUTTING_DOWN,
    IBUS_DAEMON_STOP,
  };

  IBusDaemonControllerImpl(
      const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
      const scoped_refptr<base::SequencedTaskRunner>& file_task_runner)
      : process_handle_(base::kNullProcessHandle),
      ibus_daemon_status_(IBUS_DAEMON_STOP),
      ui_task_runner_(ui_task_runner),
      file_task_runner_(file_task_runner),
      weak_ptr_factory_(this) {
  }

  virtual ~IBusDaemonControllerImpl() {}

  // IBusDaemonController override:
  virtual void AddObserver(Observer* observer) OVERRIDE {
    DCHECK(thread_checker_.CalledOnValidThread());
    observers_.AddObserver(observer);
  }

  // IBusDaemonController override:
  virtual void RemoveObserver(Observer* observer) OVERRIDE {
    DCHECK(thread_checker_.CalledOnValidThread());
    observers_.RemoveObserver(observer);
  }

  // IBusDaemonController override:
  virtual bool Start() OVERRIDE {
    DCHECK(thread_checker_.CalledOnValidThread());
    if (ibus_daemon_status_ == IBUS_DAEMON_RUNNING)
      return true;
    if (ibus_daemon_status_ == IBUS_DAEMON_STOP ||
        ibus_daemon_status_ == IBUS_DAEMON_SHUTTING_DOWN) {
      return StartIBusDaemon();
    }
    return true;
  }

  // IBusDaemonController override:
  virtual bool Stop() OVERRIDE {
    DCHECK(thread_checker_.CalledOnValidThread());
    NOTREACHED() << "Termination of ibus-daemon is not supported"
                 << "http://crosbug.com/27051";
    return false;
  }

 private:
  // Starts ibus-daemon service.
  bool StartIBusDaemon() {
    if (ibus_daemon_status_ == IBUS_DAEMON_INITIALIZING ||
        ibus_daemon_status_ == IBUS_DAEMON_RUNNING) {
      DVLOG(1) << "MaybeLaunchIBusDaemon: ibus-daemon is already running.";
      return false;
    }

    ibus_daemon_status_ = IBUS_DAEMON_INITIALIZING;
    ibus_daemon_address_ = base::StringPrintf(
        "unix:abstract=ibus-%d",
        base::RandInt(0, std::numeric_limits<int>::max()));

    scoped_ptr<base::Environment> env(base::Environment::Create());
    std::string address_file_path;
    env->GetVar("IBUS_ADDRESS_FILE", &address_file_path);
    DCHECK(!address_file_path.empty());

    // Set up ibus-daemon address file watcher before launching ibus-daemon,
    // because if watcher starts after ibus-daemon, we may miss the ibus
    // connection initialization.
    bool success = file_task_runner_->PostTaskAndReply(
        FROM_HERE,
        base::Bind(&StartWatch,
                   address_file_path,
                   base::Bind(&IBusDaemonControllerImpl::FilePathChanged,
                              weak_ptr_factory_.GetWeakPtr(),
                              ibus_daemon_address_),
                   ui_task_runner_),
        base::Bind(&IBusDaemonControllerImpl::LaunchIBusDaemon,
                   weak_ptr_factory_.GetWeakPtr(),
                   ibus_daemon_address_));
    DCHECK(success);
    return true;
  }

  // Launhes actual ibus-daemon process.
  void LaunchIBusDaemon(const std::string& ibus_address) {
    DCHECK(thread_checker_.CalledOnValidThread());
    DCHECK_EQ(base::kNullProcessHandle, process_handle_);
    static const char kIBusDaemonPath[] = "/usr/bin/ibus-daemon";
    // TODO(zork): Send output to /var/log/ibus.log
    std::vector<std::string> ibus_daemon_command_line;
    ibus_daemon_command_line.push_back(kIBusDaemonPath);
    ibus_daemon_command_line.push_back("--panel=disable");
    ibus_daemon_command_line.push_back("--cache=none");
    ibus_daemon_command_line.push_back("--restart");
    ibus_daemon_command_line.push_back("--replace");
    ibus_daemon_command_line.push_back("--address=" + ibus_address);

    if (!base::LaunchProcess(ibus_daemon_command_line,
                             base::LaunchOptions(),
                             &process_handle_)) {
      LOG(WARNING) << "Could not launch: "
                   << JoinString(ibus_daemon_command_line, " ");
    }
  }

  // Called by FilePathWatcher when the ibus-daemon address file is changed.
  // This function will be called on FILE thread.
  void FilePathChanged(const std::string& ibus_address) {
    ui_task_runner_->PostTask(
        FROM_HERE,
        base::Bind(&IBusDaemonControllerImpl::IBusDaemonInitializationDone,
                   weak_ptr_factory_.GetWeakPtr(),
                   ibus_address));
  }

  // Called by FilePathChaged function, this function should be called on UI
  // thread.
  void IBusDaemonInitializationDone(const std::string& ibus_address) {
    if (ibus_daemon_address_ != ibus_address)
      return;

    if (ibus_daemon_status_ != IBUS_DAEMON_INITIALIZING) {
      // Stop() or OnIBusDaemonExit() has already been called.
      return;
    }

    DBusThreadManager::Get()->InitIBusBus(
        ibus_address,
        base::Bind(&IBusDaemonControllerImpl::OnIBusDaemonDisconnected,
                   weak_ptr_factory_.GetWeakPtr(),
                   base::GetProcId(process_handle_)));
    ibus_daemon_status_ = IBUS_DAEMON_RUNNING;
    FOR_EACH_OBSERVER(Observer, observers_, OnConnected());

    VLOG(1) << "The ibus-daemon initialization is done.";
  }

  // Called when the connection with ibus-daemon is disconnected.
  void OnIBusDaemonDisconnected(base::ProcessId pid) {
    if (!chromeos::DBusThreadManager::Get())
      return;  // Expected disconnection at shutting down. do nothing.

    if (process_handle_ != base::kNullProcessHandle) {
      if (base::GetProcId(process_handle_) == pid) {
        // ibus-daemon crashed.
        // TODO(nona): Shutdown ibus-bus connection.
        process_handle_ = base::kNullProcessHandle;
      } else {
        // This condition is as follows.
        // 1. Called Stop (process_handle_ becomes null)
        // 2. Called LaunchProcess (process_handle_ becomes new instance)
        // 3. Callbacked OnIBusDaemonExit for old instance and reach here.
        // In this case, we should not reset process_handle_ as null, and do not
        // re-launch ibus-daemon.
        return;
      }
    }

    const IBusDaemonStatus on_exit_state = ibus_daemon_status_;
    ibus_daemon_status_ = IBUS_DAEMON_STOP;
    FOR_EACH_OBSERVER(Observer, observers_, OnDisconnected());

    if (on_exit_state == IBUS_DAEMON_SHUTTING_DOWN)
      return;  // Normal exitting, so do nothing.

    LOG(ERROR) << "The ibus-daemon crashed. Re-launching...";
    StartIBusDaemon();
  }

  // The current ibus_daemon address. This value is assigned at the launching
  // ibus-daemon and used in bus connection initialization.
  std::string ibus_daemon_address_;

  // The process handle of the IBus daemon. kNullProcessHandle if it's not
  // running.
  base::ProcessHandle process_handle_;

  // Represents ibus-daemon's status.
  IBusDaemonStatus ibus_daemon_status_;

  // The task runner of UI thread.
  scoped_refptr<base::SequencedTaskRunner> ui_task_runner_;

  // The task runner of FILE thread.
  scoped_refptr<base::SequencedTaskRunner> file_task_runner_;

  ObserverList<Observer> observers_;
  base::ThreadChecker thread_checker_;

  // Used for making callbacks for PostTask.
  base::WeakPtrFactory<IBusDaemonControllerImpl> weak_ptr_factory_;

  DISALLOW_COPY_AND_ASSIGN(IBusDaemonControllerImpl);
};

// An implementation of IBusDaemonController without ibus-daemon interaction.
// Currently this class is used only on linux desktop.
// TODO(nona): Remove IBusDaemonControlelr this once crbug.com/171351 is fixed.
class IBusDaemonControllerDaemonlessImpl : public IBusDaemonController {
 public:
  IBusDaemonControllerDaemonlessImpl()
      : is_started_(false) {}
  virtual ~IBusDaemonControllerDaemonlessImpl() {}

  // IBusDaemonController overrides:
  virtual void AddObserver(Observer* observer) OVERRIDE {
    observers_.AddObserver(observer);
  }

  virtual void RemoveObserver(Observer* observer) OVERRIDE {
    observers_.RemoveObserver(observer);
  }

  virtual bool Start() OVERRIDE {
    if (is_started_)
      return false;
    // IBusBus should be initialized but it is okay to pass "dummy address" as
    // the bus address because the actual dbus implementation is stub if the
    // Chrome OS is working on Linux desktop. This path is not used in
    // production at this moment, only for Chrome OS on Linux Desktop.
    // TODO(nona): Remove InitIBusBus oncer all legacy ime is migrated to IME
    // extension API.
    DCHECK(!base::chromeos::IsRunningOnChromeOS());
    DBusThreadManager::Get()->InitIBusBus("dummy address",
                                          base::Bind(&base::DoNothing));
    is_started_ = true;
    FOR_EACH_OBSERVER(Observer, observers_, OnConnected());
    return true;
  }
  virtual bool Stop() OVERRIDE {
    if (!is_started_)
      return false;
    is_started_ = false;
    FOR_EACH_OBSERVER(Observer, observers_, OnDisconnected());
    return true;
  }

 private:
  ObserverList<Observer> observers_;
  bool is_started_;
  DISALLOW_COPY_AND_ASSIGN(IBusDaemonControllerDaemonlessImpl);
};

}  // namespace

///////////////////////////////////////////////////////////////////////////////
// IBusDaemonController

IBusDaemonController::IBusDaemonController() {
}

IBusDaemonController::~IBusDaemonController() {
}

// static
void IBusDaemonController::Initialize(
    const scoped_refptr<base::SequencedTaskRunner>& ui_task_runner,
    const scoped_refptr<base::SequencedTaskRunner>& file_task_runner) {
  DCHECK(g_ibus_daemon_controller == NULL)
      << "Do not call Initialize function multiple times.";
  if (base::chromeos::IsRunningOnChromeOS()) {
    g_ibus_daemon_controller = new IBusDaemonControllerImpl(ui_task_runner,
                                                            file_task_runner);
  } else {
    g_ibus_daemon_controller = new IBusDaemonControllerDaemonlessImpl();
  }
}

// static
void IBusDaemonController::InitializeForTesting(
    IBusDaemonController* controller) {
  DCHECK(g_ibus_daemon_controller == NULL);
  DCHECK(controller);
  g_ibus_daemon_controller = controller;
}

// static
void IBusDaemonController::Shutdown() {
  delete g_ibus_daemon_controller;
  g_ibus_daemon_controller = NULL;
}

// static
IBusDaemonController* IBusDaemonController::GetInstance() {
  return g_ibus_daemon_controller;
}

}  // namespace chromeos