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 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 AutomationEventRouter* AutomationEventRouter::GetInstance() { | 26 AutomationEventRouter* AutomationEventRouter::GetInstance() { |
| 27 return Singleton<AutomationEventRouter, | 27 return Singleton<AutomationEventRouter, |
| 28 LeakySingletonTraits<AutomationEventRouter>>::get(); | 28 LeakySingletonTraits<AutomationEventRouter>>::get(); |
| 29 } | 29 } |
| 30 | 30 |
| 31 AutomationEventRouter::AutomationEventRouter() { | 31 AutomationEventRouter::AutomationEventRouter() { |
| 32 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | 32 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 33 content::NotificationService::AllBrowserContextsAndSources()); | 33 content::NotificationService::AllBrowserContextsAndSources()); |
| 34 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | 34 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, |
| 35 content::NotificationService::AllBrowserContextsAndSources()); | 35 content::NotificationService::AllBrowserContextsAndSources()); |
| 36 active_profile_ = ProfileManager::GetLastUsedProfile(); | |
| 37 | |
| 38 #if defined(OS_CHROMEOS) | |
| 39 session_state_observer_.reset(new ash::ScopedSessionStateObserver(this)); | |
| 40 #endif | |
| 36 } | 41 } |
| 37 | 42 |
| 38 AutomationEventRouter::~AutomationEventRouter() { | 43 AutomationEventRouter::~AutomationEventRouter() { |
| 39 } | 44 } |
| 40 | 45 |
| 41 void AutomationEventRouter::RegisterListenerForOneTree( | 46 void AutomationEventRouter::RegisterListenerForOneTree( |
| 47 const ExtensionId& extension_id, | |
| 42 int listener_process_id, | 48 int listener_process_id, |
| 43 int listener_routing_id, | 49 int listener_routing_id, |
| 44 int source_ax_tree_id) { | 50 int source_ax_tree_id) { |
| 45 Register(listener_process_id, listener_routing_id, source_ax_tree_id, false); | 51 Register(extension_id, |
| 52 listener_process_id, | |
| 53 listener_routing_id, | |
| 54 source_ax_tree_id, | |
| 55 false); | |
| 46 } | 56 } |
| 47 | 57 |
| 48 void AutomationEventRouter::RegisterListenerWithDesktopPermission( | 58 void AutomationEventRouter::RegisterListenerWithDesktopPermission( |
| 59 const ExtensionId& extension_id, | |
| 49 int listener_process_id, | 60 int listener_process_id, |
| 50 int listener_routing_id) { | 61 int listener_routing_id) { |
| 51 Register(listener_process_id, listener_routing_id, 0, true); | 62 Register(extension_id, |
| 63 listener_process_id, | |
| 64 listener_routing_id, | |
| 65 0 /* desktop tree ID */, | |
| 66 true); | |
| 52 } | 67 } |
| 53 | 68 |
| 54 void AutomationEventRouter::DispatchAccessibilityEvent( | 69 void AutomationEventRouter::DispatchAccessibilityEvent( |
| 55 const ExtensionMsg_AccessibilityEventParams& params) { | 70 const ExtensionMsg_AccessibilityEventParams& params) { |
| 56 for (const auto& listener : listeners_) { | 71 for (const auto& listener : listeners_) { |
| 72 // Skip listeners that don't want to listen to this tree. | |
| 57 if (!listener.desktop && | 73 if (!listener.desktop && |
| 58 listener.tree_ids.find(params.tree_id) == listener.tree_ids.end()) { | 74 listener.tree_ids.find(params.tree_id) == listener.tree_ids.end()) { |
| 59 continue; | 75 continue; |
| 60 } | 76 } |
| 61 | 77 |
| 62 content::RenderProcessHost* rph = | 78 content::RenderProcessHost* rph = |
| 63 content::RenderProcessHost::FromID(listener.process_id); | 79 content::RenderProcessHost::FromID(listener.process_id); |
| 64 rph->Send(new ExtensionMsg_AccessibilityEvent(listener.routing_id, | 80 rph->Send(new ExtensionMsg_AccessibilityEvent(listener.routing_id, |
| 65 params)); | 81 params, |
| 82 listener.is_active_profile)); | |
| 66 } | 83 } |
| 67 } | 84 } |
| 68 | 85 |
| 69 void AutomationEventRouter::DispatchTreeDestroyedEvent( | 86 void AutomationEventRouter::DispatchTreeDestroyedEvent( |
| 70 int tree_id, | 87 int tree_id, |
| 71 content::BrowserContext* browser_context) { | 88 content::BrowserContext* browser_context) { |
| 72 scoped_ptr<base::ListValue> args( | 89 scoped_ptr<base::ListValue> args( |
| 73 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | 90 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); |
| 74 scoped_ptr<Event> event(new Event( | 91 scoped_ptr<Event> event(new Event( |
| 75 events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED, | 92 events::AUTOMATION_INTERNAL_ON_ACCESSIBILITY_TREE_DESTROYED, |
| 76 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, | 93 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName, |
| 77 args.Pass())); | 94 args.Pass())); |
| 78 event->restrict_to_browser_context = browser_context; | 95 event->restrict_to_browser_context = browser_context; |
| 79 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); | 96 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); |
| 80 } | 97 } |
| 81 | 98 |
| 82 AutomationEventRouter::AutomationListener::AutomationListener() { | 99 AutomationEventRouter::AutomationListener::AutomationListener() { |
| 83 } | 100 } |
| 84 | 101 |
| 85 AutomationEventRouter::AutomationListener::~AutomationListener() { | 102 AutomationEventRouter::AutomationListener::~AutomationListener() { |
| 86 } | 103 } |
| 87 | 104 |
| 88 void AutomationEventRouter::Register( | 105 void AutomationEventRouter::Register( |
| 106 const ExtensionId& extension_id, | |
| 89 int listener_process_id, | 107 int listener_process_id, |
| 90 int listener_routing_id, | 108 int listener_routing_id, |
| 91 int ax_tree_id, | 109 int ax_tree_id, |
| 92 bool desktop) { | 110 bool desktop) { |
| 93 auto iter = std::find_if( | 111 auto iter = std::find_if( |
| 94 listeners_.begin(), | 112 listeners_.begin(), |
| 95 listeners_.end(), | 113 listeners_.end(), |
| 96 [listener_process_id, listener_routing_id]( | 114 [listener_process_id, listener_routing_id]( |
| 97 const AutomationListener& item) { | 115 const AutomationListener& item) { |
| 98 return (item.process_id == listener_process_id && | 116 return (item.process_id == listener_process_id && |
| 99 item.routing_id == listener_routing_id); | 117 item.routing_id == listener_routing_id); |
| 100 }); | 118 }); |
| 101 | 119 |
| 102 // Add a new entry if we don't have one with that process and routing id. | 120 // Add a new entry if we don't have one with that process and routing id. |
| 103 if (iter == listeners_.end()) { | 121 if (iter == listeners_.end()) { |
| 104 AutomationListener listener; | 122 AutomationListener listener; |
| 123 listener.extension_id = extension_id; | |
| 105 listener.routing_id = listener_routing_id; | 124 listener.routing_id = listener_routing_id; |
| 106 listener.process_id = listener_process_id; | 125 listener.process_id = listener_process_id; |
| 107 listener.desktop = desktop; | 126 listener.desktop = desktop; |
| 108 listener.tree_ids.insert(ax_tree_id); | 127 listener.tree_ids.insert(ax_tree_id); |
| 109 listeners_.push_back(listener); | 128 listeners_.push_back(listener); |
| 129 UpdateActiveProfile(); | |
| 110 return; | 130 return; |
| 111 } | 131 } |
| 112 | 132 |
| 113 // We have an entry with that process and routing id, so update the set of | 133 // We have an entry with that process and routing id, so update the set of |
| 114 // tree ids it wants to listen to, and update its desktop permission. | 134 // tree ids it wants to listen to, and update its desktop permission. |
| 115 iter->tree_ids.insert(ax_tree_id); | 135 iter->tree_ids.insert(ax_tree_id); |
| 116 if (desktop) | 136 if (desktop) |
| 117 iter->desktop = true; | 137 iter->desktop = true; |
| 118 } | 138 } |
| 119 | 139 |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 131 content::Source<content::RenderProcessHost>(source).ptr(); | 151 content::Source<content::RenderProcessHost>(source).ptr(); |
| 132 int process_id = rph->GetID(); | 152 int process_id = rph->GetID(); |
| 133 listeners_.erase( | 153 listeners_.erase( |
| 134 std::remove_if( | 154 std::remove_if( |
| 135 listeners_.begin(), | 155 listeners_.begin(), |
| 136 listeners_.end(), | 156 listeners_.end(), |
| 137 [process_id](const AutomationListener& item) { | 157 [process_id](const AutomationListener& item) { |
| 138 return item.process_id == process_id; | 158 return item.process_id == process_id; |
| 139 }), | 159 }), |
| 140 listeners_.end()); | 160 listeners_.end()); |
| 161 UpdateActiveProfile(); | |
| 162 } | |
| 163 | |
| 164 #if defined(OS_CHROMEOS) | |
| 165 void AutomationEventRouter::ActiveUserChanged(const std::string& user_id) { | |
| 166 active_profile_ = ProfileManager::GetLastUsedProfile(); | |
| 167 UpdateActiveProfile(); | |
| 168 } | |
| 169 #endif | |
| 170 | |
| 171 void AutomationEventRouter::UpdateActiveProfile() { | |
| 172 for (auto& listener : listeners_) { | |
| 173 #if defined(OS_CHROMEOS) | |
| 174 int extension_id_count = 0; | |
| 175 for (auto listener2 : listeners_) { | |
|
dcheng
2015/08/07 21:36:09
const auto&
dmazzoni
2015/08/07 22:03:31
Done.
| |
| 176 if (listener2.extension_id == listener.extension_id) | |
| 177 extension_id_count++; | |
| 178 } | |
| 179 content::RenderProcessHost* rph = | |
| 180 content::RenderProcessHost::FromID(listener.process_id); | |
| 181 | |
| 182 // The purpose of is_active_profile is to ensure different instances of | |
| 183 // the same extension running in different profiles don't interfere with | |
| 184 // one another. If an automation extension is only running in one profile, | |
| 185 // always mark it as active. If it's running in two or more profiles, | |
| 186 // only mark one as active. | |
| 187 listener.is_active_profile = (extension_id_count == 1 || | |
| 188 rph->GetBrowserContext() == active_profile_); | |
| 189 #else | |
| 190 listener.is_active_profile = true; | |
| 191 #endif | |
| 192 } | |
| 141 } | 193 } |
| 142 | 194 |
| 143 } // namespace extensions | 195 } // namespace extensions |
| OLD | NEW |