Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / input_method_event_router.cc
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 "input_method_event_router.h"
6
7 #include <algorithm>
8
9 #include "base/json/json_writer.h"
10 #include "base/values.h"
11 #include "chrome/browser/chromeos/extensions/input_method_api.h"
12 #include "chrome/browser/extensions/event_names.h"
13 #include "content/public/browser/browser_context.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/browser/extension_system.h"
16
17 namespace chromeos {
18
19 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter(
20     content::BrowserContext* context)
21     : context_(context) {
22   input_method::InputMethodManager::Get()->AddObserver(this);
23 }
24
25 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() {
26   input_method::InputMethodManager::Get()->RemoveObserver(this);
27 }
28
29 void ExtensionInputMethodEventRouter::InputMethodChanged(
30     input_method::InputMethodManager *manager,
31     bool show_message) {
32   extensions::EventRouter *router =
33       extensions::ExtensionSystem::Get(context_)->event_router();
34
35   if (!router->HasEventListener(extensions::event_names::kOnInputMethodChanged))
36     return;
37
38   scoped_ptr<base::ListValue> args(new base::ListValue());
39   base::StringValue *input_method_name = new base::StringValue(
40       extensions::InputMethodAPI::GetInputMethodForXkb(
41           manager->GetCurrentInputMethod().id()));
42   args->Append(input_method_name);
43
44   // The router will only send the event to extensions that are listening.
45   scoped_ptr<extensions::Event> event(new extensions::Event(
46       extensions::event_names::kOnInputMethodChanged, args.Pass()));
47   event->restrict_to_browser_context = context_;
48   router->BroadcastEvent(event.Pass());
49 }
50
51 }  // namespace chromeos