Home | History | Annotate | Download | only in extensions
      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 "chrome/browser/extensions/event_router.h"
     14 #include "chrome/browser/extensions/extension_system.h"
     15 #include "chrome/browser/profiles/profile_manager.h"
     16 #include "chrome/browser/profiles/profile.h"
     17 
     18 namespace chromeos {
     19 
     20 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter() {
     21   input_method::InputMethodManager::Get()->AddObserver(this);
     22 }
     23 
     24 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() {
     25   input_method::InputMethodManager::Get()->RemoveObserver(this);
     26 }
     27 
     28 void ExtensionInputMethodEventRouter::InputMethodChanged(
     29     input_method::InputMethodManager *manager,
     30     bool show_message) {
     31   Profile *profile = ProfileManager::GetDefaultProfile();
     32   extensions::EventRouter *router =
     33       extensions::ExtensionSystem::Get(profile)->event_router();
     34 
     35   if (!router->HasEventListener(extensions::event_names::kOnInputMethodChanged))
     36     return;
     37 
     38   scoped_ptr<ListValue> args(new ListValue());
     39   StringValue *input_method_name = new 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_profile = profile;
     48   extensions::ExtensionSystem::Get(profile)->event_router()->
     49       BroadcastEvent(event.Pass());
     50 }
     51 
     52 }  // namespace chromeos
     53