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/extension_system.h"
     14 #include "content/public/browser/browser_context.h"
     15 #include "extensions/browser/event_router.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::GetForBrowserContext(context_)->
     34           event_router();
     35 
     36   if (!router->HasEventListener(extensions::event_names::kOnInputMethodChanged))
     37     return;
     38 
     39   scoped_ptr<ListValue> args(new ListValue());
     40   StringValue *input_method_name = new StringValue(
     41       extensions::InputMethodAPI::GetInputMethodForXkb(
     42           manager->GetCurrentInputMethod().id()));
     43   args->Append(input_method_name);
     44 
     45   // The router will only send the event to extensions that are listening.
     46   scoped_ptr<extensions::Event> event(new extensions::Event(
     47       extensions::event_names::kOnInputMethodChanged, args.Pass()));
     48   event->restrict_to_browser_context = context_;
     49   router->BroadcastEvent(event.Pass());
     50 }
     51 
     52 }  // namespace chromeos
     53