Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "chrome/browser/extensions/api/automation_internal/automation_event_rou ter.h" | 5 #include "chrome/browser/extensions/api/automation_internal/automation_event_rou ter.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/stl_util.h" | 11 #include "base/stl_util.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/accessibility/ax_tree_id_registry.h" | 13 #include "chrome/browser/accessibility/ax_tree_id_registry.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | |
| 14 #include "chrome/common/extensions/api/automation_internal.h" | 15 #include "chrome/common/extensions/api/automation_internal.h" |
| 15 #include "chrome/common/extensions/chrome_extension_messages.h" | 16 #include "chrome/common/extensions/chrome_extension_messages.h" |
| 16 #include "content/public/browser/notification_service.h" | 17 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/notification_source.h" | 18 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/notification_types.h" | 19 #include "content/public/browser/notification_types.h" |
| 19 #include "content/public/browser/render_process_host.h" | 20 #include "content/public/browser/render_process_host.h" |
| 20 #include "extensions/browser/event_router.h" | 21 #include "extensions/browser/event_router.h" |
| 21 #include "ui/accessibility/ax_enums.h" | 22 #include "ui/accessibility/ax_enums.h" |
| 22 #include "ui/accessibility/ax_node_data.h" | 23 #include "ui/accessibility/ax_node_data.h" |
| 23 | 24 |
| 24 namespace extensions { | 25 namespace extensions { |
| 25 | 26 |
| 26 // static | 27 // static |
| 27 AutomationEventRouter* AutomationEventRouter::GetInstance() { | 28 AutomationEventRouter* AutomationEventRouter::GetInstance() { |
| 28 return Singleton<AutomationEventRouter, | 29 return Singleton<AutomationEventRouter, |
| 29 LeakySingletonTraits<AutomationEventRouter>>::get(); | 30 LeakySingletonTraits<AutomationEventRouter>>::get(); |
| 30 } | 31 } |
| 31 | 32 |
| 32 AutomationEventRouter::AutomationEventRouter() { | 33 AutomationEventRouter::AutomationEventRouter() { |
| 33 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 34 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 34 content::NotificationService::AllBrowserContextsAndSources()); | 35 content::NotificationService::AllBrowserContextsAndSources()); |
| 35 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 36 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 36 content::NotificationService::AllBrowserContextsAndSources()); | 37 content::NotificationService::AllBrowserContextsAndSources()); |
| 38 active_profile_ = ProfileManager::GetLastUsedProfile(); | |
| 39 | |
| 40 #if defined(OS_CHROMEOS) | |
| 41 session_state_observer_.reset(new ash::ScopedSessionStateObserver(this)); | |
| 42 #endif | |
| 37 } | 43 } |
| 38 | 44 |
| 39 AutomationEventRouter::~AutomationEventRouter() { | 45 AutomationEventRouter::~AutomationEventRouter() { |
| 40 } | 46 } |
| 41 | 47 |
| 42 void AutomationEventRouter::RegisterListenerForOneTree( | 48 void AutomationEventRouter::RegisterListenerForOneTree( |
| 49 const ExtensionId& extension_id, | |
| 43 int listener_process_id, | 50 int listener_process_id, |
| 44 int listener_routing_id, | 51 int listener_routing_id, |
| 45 int source_ax_tree_id) { | 52 int source_ax_tree_id) { |
| 46 Register(listener_process_id, listener_routing_id, source_ax_tree_id, false); | 53 Register(extension_id, |
| 54 listener_process_id, | |
| 55 listener_routing_id, | |
| 56 source_ax_tree_id, | |
| 57 false); | |
| 47 } | 58 } |
| 48 | 59 |
| 49 void AutomationEventRouter::RegisterListenerWithDesktopPermission( | 60 void AutomationEventRouter::RegisterListenerWithDesktopPermission( |
| 61 const ExtensionId& extension_id, | |
| 50 int listener_process_id, | 62 int listener_process_id, |
| 51 int listener_routing_id) { | 63 int listener_routing_id) { |
| 52 Register(listener_process_id, listener_routing_id, 0, true); | 64 Register(extension_id, |
| 65 listener_process_id, | |
| 66 listener_routing_id, | |
| 67 0, | |
|
David Tseng
2015/08/05 22:24:36
nit: /* desktop tree id */
dmazzoni
2015/08/05 22:53:39
Done.
| |
| 68 true); | |
| 53 } | 69 } |
| 54 | 70 |
| 55 void AutomationEventRouter::DispatchAccessibilityEvent( | 71 void AutomationEventRouter::DispatchAccessibilityEvent( |
| 56 const ExtensionMsg_AccessibilityEventParams& params) { | 72 const ExtensionMsg_AccessibilityEventParams& params) { |
| 57 for (const auto& listener : listeners_) { | 73 for (const auto& listener : listeners_) { |
| 74 // Reject listeners that don't want to listen to this tree. | |
|
David Tseng
2015/08/05 22:24:36
nit: Skip?
dmazzoni
2015/08/05 22:53:39
Done.
| |
| 58 if (!listener.desktop && | 75 if (!listener.desktop && |
| 59 listener.tree_ids.find(params.tree_id) == listener.tree_ids.end()) { | 76 listener.tree_ids.find(params.tree_id) == listener.tree_ids.end()) { |
| 60 continue; | 77 continue; |
| 61 } | 78 } |
| 62 | 79 |
| 63 content::RenderProcessHost* rph = | 80 content::RenderProcessHost* rph = |
| 64 content::RenderProcessHost::FromID(listener.process_id); | 81 content::RenderProcessHost::FromID(listener.process_id); |
| 65 rph->Send(new ExtensionMsg_AccessibilityEvent(listener.routing_id, | 82 rph->Send(new ExtensionMsg_AccessibilityEvent(listener.routing_id, |
| 66 params)); | 83 params, |
| 84 listener.is_active_profile)); | |
| 67 } | 85 } |
| 68 } | 86 } |
| 69 | 87 |
| 70 void AutomationEventRouter::DispatchTreeDestroyedEvent( | 88 void AutomationEventRouter::DispatchTreeDestroyedEvent( |
| 71 int tree_id, | 89 int tree_id, |
| 72 content::BrowserContext* browser_context) { | 90 content::BrowserContext* browser_context) { |
| 73 scoped_ptr<base::ListValue> args( | 91 scoped_ptr<base::ListValue> args( |
| 74 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | 92 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); |
| 75 scoped_ptr<Event> event(new Event( | 93 scoped_ptr<Event> event(new Event( |
| 76 events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED, | 94 events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED, |
| 77 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, | 95 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, |
| 78 args.Pass())); | 96 args.Pass())); |
| 79 event->restrict_to_browser_context = browser_context; | 97 event->restrict_to_browser_context = browser_context; |
| 80 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); | 98 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); |
| 81 } | 99 } |
| 82 | 100 |
| 83 AutomationEventRouter::AutomationListener::AutomationListener() { | 101 AutomationEventRouter::AutomationListener::AutomationListener() { |
| 84 } | 102 } |
| 85 | 103 |
| 86 AutomationEventRouter::AutomationListener::~AutomationListener() { | 104 AutomationEventRouter::AutomationListener::~AutomationListener() { |
| 87 } | 105 } |
| 88 | 106 |
| 89 void AutomationEventRouter::Register( | 107 void AutomationEventRouter::Register( |
| 108 const ExtensionId& extension_id, | |
| 90 int listener_process_id, | 109 int listener_process_id, |
| 91 int listener_routing_id, | 110 int listener_routing_id, |
| 92 int ax_tree_id, | 111 int ax_tree_id, |
| 93 bool desktop) { | 112 bool desktop) { |
| 94 auto iter = std::find_if( | 113 auto iter = std::find_if( |
| 95 listeners_.begin(), | 114 listeners_.begin(), |
| 96 listeners_.end(), | 115 listeners_.end(), |
| 97 [listener_process_id, listener_routing_id]( | 116 [listener_process_id, listener_routing_id]( |
| 98 const AutomationListener& item) { | 117 const AutomationListener& item) { |
| 99 return (item.process_id == listener_process_id && | 118 return (item.process_id == listener_process_id && |
| 100 item.routing_id == listener_routing_id); | 119 item.routing_id == listener_routing_id); |
| 101 }); | 120 }); |
| 102 | 121 |
| 103 // Add a new entry if we don't have one with that process and routing id. | 122 // Add a new entry if we don't have one with that process and routing id. |
| 104 if (iter == listeners_.end()) { | 123 if (iter == listeners_.end()) { |
| 105 AutomationListener listener; | 124 AutomationListener listener; |
| 125 listener.extension_id = extension_id; | |
| 106 listener.routing_id = listener_routing_id; | 126 listener.routing_id = listener_routing_id; |
| 107 listener.process_id = listener_process_id; | 127 listener.process_id = listener_process_id; |
| 108 listener.desktop = desktop; | 128 listener.desktop = desktop; |
| 109 listener.tree_ids.insert(ax_tree_id); | 129 listener.tree_ids.insert(ax_tree_id); |
| 130 listener.is_active_profile = true; | |
|
David Tseng
2015/08/05 22:24:36
Does this default make sense?
dmazzoni
2015/08/05 22:53:39
Deleted because we just call UpdateActiveProfile,
| |
| 110 listeners_.push_back(listener); | 131 listeners_.push_back(listener); |
| 132 UpdateActiveProfile(); | |
| 111 return; | 133 return; |
| 112 } | 134 } |
| 113 | 135 |
| 114 // We have an entry with that process and routing id, so update the set of | 136 // We have an entry with that process and routing id, so update the set of |
| 115 // tree ids it wants to listen to, and update its desktop permission. | 137 // tree ids it wants to listen to, and update its desktop permission. |
| 116 iter->tree_ids.insert(ax_tree_id); | 138 iter->tree_ids.insert(ax_tree_id); |
| 117 if (desktop) | 139 if (desktop) |
| 118 iter->desktop = true; | 140 iter->desktop = true; |
| 119 } | 141 } |
| 120 | 142 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 132 content::Source<content::RenderProcessHost>(source).ptr(); | 154 content::Source<content::RenderProcessHost>(source).ptr(); |
| 133 int process_id = rph->GetID(); | 155 int process_id = rph->GetID(); |
| 134 listeners_.erase( | 156 listeners_.erase( |
| 135 std::remove_if( | 157 std::remove_if( |
| 136 listeners_.begin(), | 158 listeners_.begin(), |
| 137 listeners_.end(), | 159 listeners_.end(), |
| 138 [process_id](const AutomationListener& item) { | 160 [process_id](const AutomationListener& item) { |
| 139 return item.process_id == process_id; | 161 return item.process_id == process_id; |
| 140 }), | 162 }), |
| 141 listeners_.end()); | 163 listeners_.end()); |
| 164 UpdateActiveProfile(); | |
| 165 } | |
| 166 | |
| 167 void AutomationEventRouter::ActiveUserChanged(const std::string& user_id) { | |
| 168 active_profile_ = ProfileManager::GetLastUsedProfile(); | |
| 169 UpdateActiveProfile(); | |
| 170 } | |
| 171 | |
| 172 void AutomationEventRouter::UpdateActiveProfile() { | |
| 173 for (auto& listener : listeners_) { | |
| 174 #if defined(OS_CHROMEOS) | |
|
David Tseng
2015/08/05 22:24:36
Can this be USE_ASH instead?
dmazzoni
2015/08/05 22:53:39
No, because that'd be true on Windows, which uses
| |
| 175 int extension_id_count = 0; | |
| 176 for (auto listener2 : listeners_) { | |
| 177 if (listener2.extension_id == listener.extension_id) | |
| 178 extension_id_count++; | |
| 179 } | |
| 180 content::RenderProcessHost* rph = | |
| 181 content::RenderProcessHost::FromID(listener.process_id); | |
| 182 listener.is_active_profile = (extension_id_count == 1 || | |
|
David Tseng
2015/08/05 22:24:36
Does this condition make sense? If I understand co
dmazzoni
2015/08/05 22:53:39
Yes, that's intentional. The idea is only to preve
| |
| 183 rph->GetBrowserContext() == active_profile_); | |
| 184 #else | |
| 185 listener.is_active_profile = true; | |
| 186 #endif | |
| 187 } | |
| 142 } | 188 } |
| 143 | 189 |
| 144 } // namespace extensions | 190 } // namespace extensions |
| OLD | NEW |