Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/automation_internal/automation_event_rou ter.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/accessibility/ax_tree_id_registry.h" | |
| 14 #include "chrome/common/extensions/api/automation_internal.h" | |
| 15 #include "chrome/common/extensions/chrome_extension_messages.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "content/public/browser/notification_source.h" | |
| 18 #include "content/public/browser/notification_types.h" | |
| 19 #include "content/public/browser/render_process_host.h" | |
| 20 #include "extensions/browser/event_router.h" | |
| 21 #include "ui/accessibility/ax_enums.h" | |
| 22 #include "ui/accessibility/ax_node_data.h" | |
| 23 | |
| 24 namespace extensions { | |
| 25 | |
| 26 // static | |
| 27 AutomationEventRouter* AutomationEventRouter::GetInstance() { | |
| 28 return Singleton<AutomationEventRouter, | |
| 29 LeakySingletonTraits<AutomationEventRouter>>::get(); | |
| 30 } | |
| 31 | |
| 32 AutomationEventRouter::AutomationEventRouter() { | |
| 33 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 34 content::NotificationService::AllBrowserContextsAndSources()); | |
| 35 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
| 36 content::NotificationService::AllBrowserContextsAndSources()); | |
| 37 } | |
| 38 | |
| 39 AutomationEventRouter::~AutomationEventRouter() { | |
| 40 } | |
| 41 | |
| 42 void AutomationEventRouter::AddListener( | |
| 43 int listener_process_id, | |
| 44 int listener_routing_id, | |
| 45 int ax_tree_id, | |
| 46 bool desktop) { | |
| 47 auto iter = std::find_if( | |
| 48 listeners_.begin(), | |
| 49 listeners_.end(), | |
| 50 [=](AutomationListener& item) { | |
|
dcheng
2015/06/06 00:00:52
We don't currently permit default captures: http:/
dmazzoni
2015/06/08 18:02:57
Thanks. Still getting used to C++11 style rules.
| |
| 51 return (item.process_id == listener_process_id && | |
| 52 item.routing_id == listener_routing_id); | |
| 53 }); | |
| 54 | |
| 55 // Add a new entry if we don't have one with that process and routing id. | |
| 56 if (iter == listeners_.end()) { | |
| 57 AutomationListener listener; | |
| 58 listener.routing_id = listener_routing_id; | |
| 59 listener.process_id = listener_process_id; | |
| 60 listener.desktop = desktop; | |
| 61 listener.tree_ids.insert(ax_tree_id); | |
| 62 listeners_.push_back(listener); | |
| 63 return; | |
| 64 } | |
| 65 | |
| 66 // We have an entry with that process and routing id, so update the set of | |
| 67 // tree ids it wants to listen to, and update its desktop permission. | |
| 68 iter->tree_ids.insert(ax_tree_id); | |
| 69 if (desktop) | |
| 70 iter->desktop = true; | |
| 71 } | |
| 72 | |
| 73 void AutomationEventRouter::DispatchAccessibilityEventToAutomation( | |
| 74 const ExtensionMsg_AccessibilityEventParams& params) { | |
| 75 for (auto iter : listeners_) { | |
|
dcheng
2015/06/06 00:00:52
Doesn't this make a copy of AutomationListener on
dmazzoni
2015/06/08 18:02:57
Done.
| |
| 76 if (!iter.desktop && | |
| 77 iter.tree_ids.find(params.tree_id) == iter.tree_ids.end()) { | |
| 78 continue; | |
| 79 } | |
| 80 | |
| 81 content::RenderProcessHost* rph = | |
| 82 content::RenderProcessHost::FromID(iter.process_id); | |
| 83 rph->Send(new ExtensionMsg_AccessibilityEvent(iter.routing_id, params)); | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 void AutomationEventRouter::DispatchTreeDestroyedEventToAutomation( | |
| 88 int tree_id, | |
| 89 content::BrowserContext* browser_context) { | |
| 90 std::string event_name( | |
| 91 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName); | |
| 92 scoped_ptr<base::ListValue> args( | |
| 93 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | |
| 94 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | |
| 95 event->restrict_to_browser_context = browser_context; | |
| 96 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); | |
| 97 } | |
| 98 | |
| 99 AutomationEventRouter::AutomationListener::AutomationListener() { | |
| 100 } | |
| 101 | |
| 102 AutomationEventRouter::AutomationListener::~AutomationListener() { | |
| 103 } | |
| 104 | |
| 105 void AutomationEventRouter::Observe( | |
| 106 int type, | |
| 107 const content::NotificationSource& source, | |
| 108 const content::NotificationDetails& details) { | |
| 109 if (type != content::NOTIFICATION_RENDERER_PROCESS_TERMINATED && | |
| 110 type != content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { | |
| 111 NOTREACHED(); | |
| 112 return; | |
| 113 } | |
| 114 | |
| 115 content::RenderProcessHost* rph = | |
| 116 content::Source<content::RenderProcessHost>(source).ptr(); | |
| 117 int process_id = rph->GetID(); | |
| 118 std::remove_if( | |
| 119 listeners_.begin(), | |
| 120 listeners_.end(), | |
| 121 [=](AutomationListener& item) { | |
| 122 return item.process_id = process_id; | |
| 123 }); | |
| 124 } | |
| 125 | |
| 126 } // namespace extensions | |
| OLD | NEW |