OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/values.h" | 10 #include "base/values.h" |
11 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" | 11 #include "chrome/browser/chromeos/web_socket_proxy_controller.h" |
12 #include "chrome/browser/extensions/extension_event_names.h" | 12 #include "chrome/browser/extensions/event_names.h" |
13 #include "chrome/browser/extensions/extension_event_router.h" | 13 #include "chrome/browser/extensions/event_router.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Prefix, which is used by XKB. | 19 // Prefix, which is used by XKB. |
20 const char kXkbPrefix[] = "xkb:"; | 20 const char kXkbPrefix[] = "xkb:"; |
21 | 21 |
22 } // namespace | 22 } // namespace |
23 | 23 |
24 namespace chromeos { | 24 namespace chromeos { |
25 | 25 |
26 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter() { | 26 ExtensionInputMethodEventRouter::ExtensionInputMethodEventRouter() { |
27 input_method::InputMethodManager::GetInstance()->AddObserver(this); | 27 input_method::InputMethodManager::GetInstance()->AddObserver(this); |
28 } | 28 } |
29 | 29 |
30 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() { | 30 ExtensionInputMethodEventRouter::~ExtensionInputMethodEventRouter() { |
31 input_method::InputMethodManager::GetInstance()->RemoveObserver(this); | 31 input_method::InputMethodManager::GetInstance()->RemoveObserver(this); |
32 } | 32 } |
33 | 33 |
34 void ExtensionInputMethodEventRouter::InputMethodChanged( | 34 void ExtensionInputMethodEventRouter::InputMethodChanged( |
35 input_method::InputMethodManager *manager, | 35 input_method::InputMethodManager *manager, |
36 bool show_message) { | 36 bool show_message) { |
37 Profile *profile = ProfileManager::GetDefaultProfile(); | 37 Profile *profile = ProfileManager::GetDefaultProfile(); |
38 ExtensionEventRouter *router = profile->GetExtensionEventRouter(); | 38 extensions::EventRouter *router = profile->GetExtensionEventRouter(); |
39 | 39 |
40 if (!router->HasEventListener(extension_event_names::kOnInputMethodChanged)) | 40 if (!router->HasEventListener(extensions::event_names::kOnInputMethodChanged)) |
41 return; | 41 return; |
42 | 42 |
43 ListValue args; | 43 ListValue args; |
44 StringValue *input_method_name = new StringValue( | 44 StringValue *input_method_name = new StringValue( |
45 GetInputMethodForXkb(manager->GetCurrentInputMethod().id())); | 45 GetInputMethodForXkb(manager->GetCurrentInputMethod().id())); |
46 args.Append(input_method_name); | 46 args.Append(input_method_name); |
47 std::string args_json; | 47 std::string args_json; |
48 base::JSONWriter::Write(&args, &args_json); | 48 base::JSONWriter::Write(&args, &args_json); |
49 | 49 |
50 // The router will only send the event to extensions that are listening. | 50 // The router will only send the event to extensions that are listening. |
51 router->DispatchEventToRenderers( | 51 router->DispatchEventToRenderers( |
52 extension_event_names::kOnInputMethodChanged, | 52 extensions::event_names::kOnInputMethodChanged, |
53 args_json, profile, GURL()); | 53 args_json, profile, GURL()); |
54 } | 54 } |
55 | 55 |
56 std::string ExtensionInputMethodEventRouter::GetInputMethodForXkb( | 56 std::string ExtensionInputMethodEventRouter::GetInputMethodForXkb( |
57 const std::string& xkb_id) { | 57 const std::string& xkb_id) { |
58 size_t prefix_length = std::string(kXkbPrefix).length(); | 58 size_t prefix_length = std::string(kXkbPrefix).length(); |
59 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); | 59 DCHECK(xkb_id.substr(0, prefix_length) == kXkbPrefix); |
60 return xkb_id.substr(prefix_length); | 60 return xkb_id.substr(prefix_length); |
61 } | 61 } |
62 | 62 |
63 } // namespace chromeos | 63 } // namespace chromeos |
OLD | NEW |