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 <string> | |
8 #include <utility> | |
9 | |
10 #include "base/stl_util.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/accessibility/ax_tree_id_registry.h" | |
13 #include "chrome/common/extensions/api/automation_internal.h" | |
14 #include "chrome/common/extensions/chrome_extension_messages.h" | |
15 #include "content/public/browser/notification_service.h" | |
16 #include "content/public/browser/notification_source.h" | |
17 #include "content/public/browser/notification_types.h" | |
18 #include "content/public/browser/render_process_host.h" | |
19 #include "extensions/browser/event_router.h" | |
20 #include "ui/accessibility/ax_enums.h" | |
21 #include "ui/accessibility/ax_node_data.h" | |
22 | |
23 namespace extensions { | |
24 | |
25 struct AutomationListener { | |
26 int routing_id; | |
27 int process_id; | |
28 bool desktop; | |
29 std::set<int> tree_ids; | |
30 }; | |
31 | |
32 // static | |
33 AutomationEventRouter* AutomationEventRouter::GetInstance() { | |
34 return Singleton<AutomationEventRouter, | |
35 LeakySingletonTraits<AutomationEventRouter>>::get(); | |
36 } | |
37 | |
38 AutomationEventRouter::AutomationEventRouter() { | |
39 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
40 content::NotificationService::AllBrowserContextsAndSources()); | |
41 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CLOSED, | |
42 content::NotificationService::AllBrowserContextsAndSources()); | |
43 } | |
44 | |
45 AutomationEventRouter::~AutomationEventRouter() { | |
46 STLDeleteContainerPairSecondPointers(listener_map_.begin(), | |
47 listener_map_.end()); | |
48 } | |
49 | |
50 void AutomationEventRouter::AddListener( | |
51 ExtensionId extension_id, | |
52 int ax_tree_id, | |
53 int process_id, | |
54 int routing_id, | |
55 bool desktop) { | |
56 auto iter = listener_map_.find(extension_id); | |
57 AutomationListener* listener; | |
not at google - send to devlin
2015/06/02 23:59:24
it's nice to initialize pointers to nullptr
dmazzoni
2015/06/04 20:07:39
Done.
| |
58 if (iter != listener_map_.end()) { | |
59 listener = iter->second; | |
not at google - send to devlin
2015/06/02 23:59:24
What happens if the listener that is already there
dmazzoni
2015/06/04 20:07:39
OK, I tried to clarify it. I realized it was a mis
| |
60 } else { | |
61 listener = new AutomationListener(); | |
62 listener->routing_id = routing_id; | |
63 listener->process_id = process_id; | |
64 listener->desktop = desktop; | |
65 listener_map_.insert(std::make_pair(extension_id, listener)); | |
not at google - send to devlin
2015/06/02 23:59:24
I would have thought that listener_map_[extension_
dmazzoni
2015/06/04 20:07:39
Doesn't compile, but moot as I simplified it.
| |
66 } | |
67 listener->tree_ids.insert(ax_tree_id); | |
68 } | |
69 | |
70 void AutomationEventRouter::DispatchAccessibilityEventToAutomation( | |
71 const ExtensionMsg_AccessibilityEventParams& params) { | |
72 for (auto iter = listener_map_.begin(); | |
73 iter != listener_map_.end(); | |
74 ++iter) { | |
not at google - send to devlin
2015/06/02 23:59:24
You can use a range-based for loop here, I think?
dmazzoni
2015/06/04 20:07:39
Done.
| |
75 AutomationListener* listener = iter->second; | |
76 if (!listener->desktop && | |
77 listener->tree_ids.find(params.tree_id) == listener->tree_ids.end()) { | |
78 continue; | |
79 } | |
80 | |
81 content::RenderProcessHost* rph = | |
82 content::RenderProcessHost::FromID(listener->process_id); | |
83 rph->Send( | |
84 new ExtensionMsg_AccessibilityEvent(listener->routing_id, params)); | |
85 } | |
86 } | |
87 | |
88 void AutomationEventRouter::DispatchTreeDestroyedEventToAutomation( | |
89 int process_id, | |
90 int routing_id, | |
91 content::BrowserContext* browser_context) { | |
92 int tree_id = AXTreeIDRegistry::GetInstance()->GetOrCreateAXTreeID( | |
93 process_id, routing_id); | |
94 | |
95 std::string event_name( | |
96 api::automation_internal::OnAccessibilityTreeDestroyed::kEventName); | |
97 scoped_ptr<base::ListValue> args( | |
98 api::automation_internal::OnAccessibilityTreeDestroyed::Create(tree_id)); | |
99 if (browser_context && EventRouter::Get(browser_context)) { | |
not at google - send to devlin
2015/06/02 23:59:24
Checking for |browser_context| here implies it can
dmazzoni
2015/06/04 20:07:39
Hmmm, I can't see a good reason why. The caller sh
| |
100 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | |
101 event->restrict_to_browser_context = browser_context; | |
102 EventRouter::Get(browser_context)->BroadcastEvent(event.Pass()); | |
103 } | |
104 AXTreeIDRegistry::GetInstance()->RemoveAXTreeID(tree_id); | |
105 } | |
106 | |
107 void AutomationEventRouter::Observe( | |
108 int type, | |
109 const content::NotificationSource& source, | |
110 const content::NotificationDetails& details) { | |
111 if (type != content::NOTIFICATION_RENDERER_PROCESS_TERMINATED && | |
112 type != content::NOTIFICATION_RENDERER_PROCESS_CLOSED) { | |
113 NOTREACHED(); | |
114 return; | |
115 } | |
116 | |
117 content::RenderProcessHost* rph = | |
118 content::Source<content::RenderProcessHost>(source).ptr(); | |
119 int process_id = rph->GetID(); | |
120 | |
121 auto iter = listener_map_.begin(); | |
122 while (iter != listener_map_.end()) { | |
123 auto temp = iter; | |
not at google - send to devlin
2015/06/02 23:59:24
apologies for nit, but for what it's worth I've mo
dmazzoni
2015/06/04 20:07:39
Thanks, that's cleaner.
| |
124 iter++; | |
125 if (temp->second->process_id == process_id) { | |
126 delete temp->second; | |
127 listener_map_.erase(temp); | |
128 } | |
129 } | |
130 } | |
131 | |
132 } // namespace extensions | |
OLD | NEW |