| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "input_method_event_router.h" | 5 #include "input_method_event_router.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/json/json_writer.h" | 9 #include "base/json/json_writer.h" |
| 10 #include "base/lazy_instance.h" | |
| 11 #include "base/values.h" | 10 #include "base/values.h" |
| 12 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" | 11 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" |
| 13 #include "chrome/browser/extensions/extension_event_names.h" | 12 #include "chrome/browser/extensions/extension_event_names.h" |
| 14 #include "chrome/browser/extensions/extension_event_router.h" | 13 #include "chrome/browser/extensions/extension_event_router.h" |
| 15 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 17 | 16 |
| 18 namespace { | 17 namespace { |
| 19 | 18 |
| 20 // Prefix, which is used by XKB. | 19 // Prefix, which is used by XKB. |
| 21 const char kXkbPrefix[] = "xkb:"; | 20 const char kXkbPrefix[] = "xkb:"; |
| 22 | 21 |
| 23 // Extension ID which is used in browser_tests. | |
| 24 const char kInputMethodTestExtensionID[] = "ilanclmaeigfpnmdlgelmhkpkegdioip"; | |
| 25 | |
| 26 class InputMethodPrivateExtensionsWhitelist { | |
| 27 public: | |
| 28 InputMethodPrivateExtensionsWhitelist() { | |
| 29 chromeos::FillWithExtensionsIdsWithPrivateAccess(&ids_); | |
| 30 ids_.push_back(kInputMethodTestExtensionID); | |
| 31 std::sort(ids_.begin(), ids_.end()); | |
| 32 } | |
| 33 | |
| 34 bool HasId(const std::string& id) { | |
| 35 return std::binary_search(ids_.begin(), ids_.end(), id); | |
| 36 } | |
| 37 | |
| 38 const std::vector<std::string>& ids() { return ids_; } | |
| 39 | |
| 40 private: | |
| 41 std::vector<std::string> ids_; | |
| 42 }; | |
| 43 | |
| 44 base::LazyInstance<InputMethodPrivateExtensionsWhitelist> | |
| 45 g_input_method_private_extensions_whitelist = LAZY_INSTANCE_INITIALIZER; | |
| 46 | |
| 47 } // namespace | 22 } // namespace |
| 48 | 23 |
| 49 namespace chromeos { | 24 namespace chromeos { |
| 50 | 25 |
| 51 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter() { | 26 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter() { |
| 52 input_method::InputMethodManager::GetInstance()->AddObserver(this); | 27 input_method::InputMethodManager::GetInstance()->AddObserver(this); |
| 53 } | 28 } |
| 54 | 29 |
| 55 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() { | 30 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() { |
| 56 input_method::InputMethodManager::GetInstance()->RemoveObserver(this); | 31 input_method::InputMethodManager::GetInstance()->RemoveObserver(this); |
| 57 } | 32 } |
| 58 | 33 |
| 59 void ExtensionInputMethodEventRouter::InputMethodChanged( | 34 void ExtensionInputMethodEventRouter::InputMethodChanged( |
| 60 input_method::InputMethodManager *manager, | 35 input_method::InputMethodManager *manager, |
| 61 const input_method::InputMethodDescriptor ¤t_input_method, | 36 const input_method::InputMethodDescriptor ¤t_input_method, |
| 62 size_t num_active_input_methods) { | 37 size_t num_active_input_methods) { |
| 63 Profile *profile = ProfileManager::GetDefaultProfile(); | 38 Profile *profile = ProfileManager::GetDefaultProfile(); |
| 64 ExtensionEventRouter *router = profile->GetExtensionEventRouter(); | 39 ExtensionEventRouter *router = profile->GetExtensionEventRouter(); |
| 65 | 40 |
| 66 if (!router->HasEventListener(extension_event_names::kOnInputMethodChanged)) | 41 if (!router->HasEventListener(extension_event_names::kOnInputMethodChanged)) |
| 67 return; | 42 return; |
| 68 | 43 |
| 69 ListValue args; | 44 ListValue args; |
| 70 StringValue *input_method_name = | 45 StringValue *input_method_name = |
| 71 new StringValue(GetInputMethodForXkb(current_input_method.id())); | 46 new StringValue(GetInputMethodForXkb(current_input_method.id())); |
| 72 args.Append(input_method_name); | 47 args.Append(input_method_name); |
| 73 std::string args_json; | 48 std::string args_json; |
| 74 base::JSONWriter::Write(&args, false, &args_json); | 49 base::JSONWriter::Write(&args, false, &args_json); |
| 75 | 50 |
| 76 const std::vector<std::string>& ids = | 51 // The router will only send the event to extensions that are listening. |
| 77 g_input_method_private_extensions_whitelist.Get().ids(); | 52 router->DispatchEventToRenderers( |
| 78 | 53 extension_event_names::kOnInputMethodChanged, |
| 79 for (size_t i = 0; i < ids.size(); ++i) { | 54 args_json, profile, GURL()); |
| 80 // ExtensionEventRoutner will check that the extension is listening for the | |
| 81 // event. | |
| 82 router->DispatchEventToExtension( | |
| 83 ids[i], extension_event_names::kOnInputMethodChanged, | |
| 84 args_json, profile, GURL()); | |
| 85 } | |
| 86 } | 55 } |
| 87 | 56 |
| 88 void ExtensionInputMethodEventRouter::ActiveInputMethodsChanged( | 57 void ExtensionInputMethodEventRouter::ActiveInputMethodsChanged( |
| 89 input_method::InputMethodManager *manager, | 58 input_method::InputMethodManager *manager, |
| 90 const input_method::InputMethodDescriptor & current_input_method, | 59 const input_method::InputMethodDescriptor & current_input_method, |
| 91 size_t num_active_input_methods) { | 60 size_t num_active_input_methods) { |
| 92 } | 61 } |
| 93 | 62 |
| 94 void ExtensionInputMethodEventRouter::PropertyListChanged( | 63 void ExtensionInputMethodEventRouter::PropertyListChanged( |
| 95 input_method::InputMethodManager *manager, | 64 input_method::InputMethodManager *manager, |
| 96 const input_method::ImePropertyList & current_ime_properties) { | 65 const input_method::ImePropertyList & current_ime_properties) { |
| 97 } | 66 } |
| 98 | 67 |
| 99 std::string ExtensionInputMethodEventRouter::GetInputMethodForXkb( | 68 std::string ExtensionInputMethodEventRouter::GetInputMethodForXkb( |
| 100 const std::string& xkb_id) { | 69 const std::string& xkb_id) { |
| 101 size_t prefix_length = std::string(kXkbPrefix).length(); | 70 size_t prefix_length = std::string(kXkbPrefix).length(); |
| 102 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); | 71 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); |
| 103 return xkb_id.substr(prefix_length); | 72 return xkb_id.substr(prefix_length); |
| 104 } | 73 } |
| 105 | 74 |
| 106 bool ExtensionInputMethodEventRouter::IsExtensionWhitelisted( | |
| 107 const std::string& extension_id) { | |
| 108 return g_input_method_private_extensions_whitelist.Get().HasId(extension_id); | |
| 109 } | |
| 110 | |
| 111 } // namespace chromeos | 75 } // namespace chromeos |
| OLD | NEW |