1 // Copyright 2016 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 "chrome/browser/chromeos/extensions/ime_menu_event_router.h"
6 
7 #include <memory>
8 
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/extensions/input_method_api.h"
11 #include "chrome/common/extensions/api/input_method_private.h"
12 #include "content/public/browser/browser_context.h"
13 #include "extensions/browser/event_router.h"
14 
15 namespace input_method_private = extensions::api::input_method_private;
16 namespace OnImeMenuActivationChanged =
17     extensions::api::input_method_private::OnImeMenuActivationChanged;
18 namespace OnImeMenuListChanged =
19     extensions::api::input_method_private::OnImeMenuListChanged;
20 namespace OnImeMenuItemsChanged =
21     extensions::api::input_method_private::OnImeMenuItemsChanged;
22 
23 namespace chromeos {
24 
ExtensionImeMenuEventRouter(content::BrowserContext * context)25 ExtensionImeMenuEventRouter::ExtensionImeMenuEventRouter(
26     content::BrowserContext* context)
27     : context_(context) {
28   input_method::InputMethodManager::Get()->AddImeMenuObserver(this);
29 }
30 
~ExtensionImeMenuEventRouter()31 ExtensionImeMenuEventRouter::~ExtensionImeMenuEventRouter() {
32   input_method::InputMethodManager::Get()->RemoveImeMenuObserver(this);
33 }
34 
ImeMenuActivationChanged(bool activation)35 void ExtensionImeMenuEventRouter::ImeMenuActivationChanged(bool activation) {
36   extensions::EventRouter* router = extensions::EventRouter::Get(context_);
37 
38   if (!router->HasEventListener(OnImeMenuActivationChanged::kEventName))
39     return;
40 
41   std::unique_ptr<base::ListValue> args(new base::ListValue());
42   args->AppendBoolean(activation);
43 
44   // The router will only send the event to extensions that are listening.
45   auto event = std::make_unique<extensions::Event>(
46       extensions::events::INPUT_METHOD_PRIVATE_ON_IME_MENU_ACTIVATION_CHANGED,
47       OnImeMenuActivationChanged::kEventName, std::move(args), context_);
48   router->BroadcastEvent(std::move(event));
49 }
50 
ImeMenuListChanged()51 void ExtensionImeMenuEventRouter::ImeMenuListChanged() {
52   extensions::EventRouter* router = extensions::EventRouter::Get(context_);
53 
54   if (!router->HasEventListener(OnImeMenuListChanged::kEventName))
55     return;
56 
57   std::unique_ptr<base::ListValue> args(new base::ListValue());
58 
59   // The router will only send the event to extensions that are listening.
60   auto event = std::make_unique<extensions::Event>(
61       extensions::events::INPUT_METHOD_PRIVATE_ON_IME_MENU_LIST_CHANGED,
62       OnImeMenuListChanged::kEventName, std::move(args), context_);
63   router->BroadcastEvent(std::move(event));
64 }
65 
ImeMenuItemsChanged(const std::string & engine_id,const std::vector<input_method::InputMethodManager::MenuItem> & items)66 void ExtensionImeMenuEventRouter::ImeMenuItemsChanged(
67     const std::string& engine_id,
68     const std::vector<input_method::InputMethodManager::MenuItem>& items) {
69   extensions::EventRouter* router = extensions::EventRouter::Get(context_);
70 
71   if (!router->HasEventListener(OnImeMenuItemsChanged::kEventName))
72     return;
73 
74   std::vector<input_method_private::MenuItem> menu_items;
75   for (const auto& item : items) {
76     input_method_private::MenuItem menu_item;
77     menu_item.id = item.id;
78     menu_item.label.reset(new std::string(item.label));
79     switch (item.style) {
80       case input_method::InputMethodManager::MENU_ITEM_STYLE_CHECK:
81         menu_item.style = input_method_private::ParseMenuItemStyle("check");
82         break;
83       case input_method::InputMethodManager::MENU_ITEM_STYLE_RADIO:
84         menu_item.style = input_method_private::ParseMenuItemStyle("radio");
85         break;
86       case input_method::InputMethodManager::MENU_ITEM_STYLE_SEPARATOR:
87         menu_item.style = input_method_private::ParseMenuItemStyle("separator");
88         break;
89       default:
90         menu_item.style = input_method_private::ParseMenuItemStyle("");
91     }
92     menu_item.visible.reset(new bool(item.visible));
93     menu_item.checked.reset(new bool(item.checked));
94     menu_item.enabled.reset(new bool(item.enabled));
95     menu_items.push_back(std::move(menu_item));
96   }
97 
98   std::unique_ptr<base::ListValue> args =
99       OnImeMenuItemsChanged::Create(engine_id, menu_items);
100 
101   // The router will only send the event to extensions that are listening.
102   auto event = std::make_unique<extensions::Event>(
103       extensions::events::INPUT_METHOD_PRIVATE_ON_IME_MENU_ITEMS_CHANGED,
104       OnImeMenuItemsChanged::kEventName, std::move(args), context_);
105   router->BroadcastEvent(std::move(event));
106 }
107 
108 }  // namespace chromeos
109