| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/browser_accessibility_manager.h" | 5 #include "chrome/browser/browser_accessibility_manager.h" |
| 6 | 6 |
| 7 #include "base/scoped_comptr_win.h" | 7 #include "base/scoped_comptr_win.h" |
| 8 #include "chrome/browser/browser_accessibility.h" | 8 #include "chrome/browser/browser_accessibility.h" |
| 9 #include "chrome/browser/renderer_host/render_process_host.h" | 9 #include "chrome/browser/renderer_host/render_process_host.h" |
| 10 #include "chrome/browser/renderer_host/render_view_host.h" | 10 #include "chrome/browser/renderer_host/render_view_host.h" |
| 11 #include "chrome/common/notification_service.h" |
| 11 #include "chrome/common/render_messages.h" | 12 #include "chrome/common/render_messages.h" |
| 12 | 13 |
| 13 using webkit_glue::WebAccessibility; | 14 using webkit_glue::WebAccessibility; |
| 14 | 15 |
| 15 // Factory method to create an instance of BrowserAccessibility | 16 // The time in ms after which we give up and return an error when processing an |
| 16 BrowserAccessibility* BrowserAccessibilityFactory::Create() { | 17 // accessibility message and no response has been received from the renderer. |
| 17 CComObject<BrowserAccessibility>* instance; | 18 static const int kAccessibilityMessageTimeOut = 10000; |
| 18 HRESULT hr = CComObject<BrowserAccessibility>::CreateInstance(&instance); | 19 |
| 19 DCHECK(SUCCEEDED(hr)); | 20 // static |
| 20 return instance->NewReference(); | 21 BrowserAccessibilityManager* BrowserAccessibilityManager::GetInstance() { |
| 22 return Singleton<BrowserAccessibilityManager>::get(); |
| 21 } | 23 } |
| 22 | 24 |
| 23 // static | 25 BrowserAccessibilityManager::BrowserAccessibilityManager() { |
| 24 // Start child IDs at -1 and decrement each time, because clients use | 26 registrar_.Add(this, NotificationType::RENDERER_PROCESS_TERMINATED, |
| 25 // child IDs of 1, 2, 3, ... to access the children of an object by | 27 NotificationService::AllSources()); |
| 26 // index, so we use negative IDs to clearly distinguish between indices | |
| 27 // and unique IDs. | |
| 28 LONG BrowserAccessibilityManager::next_child_id_ = -1; | |
| 29 | |
| 30 BrowserAccessibilityManager::BrowserAccessibilityManager( | |
| 31 HWND parent_hwnd, | |
| 32 const webkit_glue::WebAccessibility& src, | |
| 33 BrowserAccessibilityFactory* factory) | |
| 34 : parent_hwnd_(parent_hwnd), | |
| 35 factory_(factory) { | |
| 36 HRESULT hr = ::CreateStdAccessibleObject( | |
| 37 parent_hwnd_, OBJID_WINDOW, IID_IAccessible, | |
| 38 reinterpret_cast<void **>(&window_iaccessible_)); | |
| 39 DCHECK(SUCCEEDED(hr)); | |
| 40 root_ = CreateAccessibilityTree(NULL, src, 0); | |
| 41 if (!focus_) | |
| 42 focus_ = root_; | |
| 43 } | 28 } |
| 44 | 29 |
| 45 BrowserAccessibilityManager::~BrowserAccessibilityManager() { | 30 BrowserAccessibilityManager::~BrowserAccessibilityManager() { |
| 46 // Clients could still hold references to some nodes of the tree, so | 31 // Clear hashmap. |
| 47 // calling Inactivate will make sure that as many nodes as possible are | 32 render_process_host_map_.clear(); |
| 48 // released now, and remaining nodes are marked as inactive so that | |
| 49 // calls to any methods on them will return E_FAIL; | |
| 50 root_->InactivateTree(); | |
| 51 root_->Release(); | |
| 52 } | 33 } |
| 53 | 34 |
| 54 BrowserAccessibility* BrowserAccessibilityManager::GetRoot() { | 35 STDMETHODIMP BrowserAccessibilityManager::CreateAccessibilityInstance( |
| 55 return root_; | 36 REFIID iid, int acc_obj_id, int routing_id, int process_id, |
| 37 HWND parent_hwnd, void** interface_ptr) { |
| 38 if (IID_IUnknown == iid || IID_IDispatch == iid || IID_IAccessible == iid) { |
| 39 CComObject<BrowserAccessibility>* instance = NULL; |
| 40 |
| 41 HRESULT hr = CComObject<BrowserAccessibility>::CreateInstance(&instance); |
| 42 DCHECK(SUCCEEDED(hr)); |
| 43 |
| 44 if (!instance) |
| 45 return E_FAIL; |
| 46 |
| 47 ScopedComPtr<IAccessible> accessibility_instance(instance); |
| 48 |
| 49 // Set class member variables. |
| 50 instance->Initialize(acc_obj_id, routing_id, process_id, parent_hwnd); |
| 51 |
| 52 // Retrieve the RenderViewHost connected to this request. |
| 53 RenderViewHost* rvh = RenderViewHost::FromID(process_id, routing_id); |
| 54 |
| 55 // Update cache with RenderProcessHost/BrowserAccessibility pair. |
| 56 if (rvh && rvh->process()) { |
| 57 render_process_host_map_.insert(MapEntry(rvh->process()->id(), instance)); |
| 58 } else { |
| 59 // No RenderProcess active for this instance. |
| 60 return E_FAIL; |
| 61 } |
| 62 |
| 63 // All is well, assign the temp instance to the output pointer. |
| 64 *interface_ptr = accessibility_instance.Detach(); |
| 65 return S_OK; |
| 66 } |
| 67 // No supported interface found, return error. |
| 68 *interface_ptr = NULL; |
| 69 return E_NOINTERFACE; |
| 56 } | 70 } |
| 57 | 71 |
| 58 BrowserAccessibility* BrowserAccessibilityManager::GetFromChildID( | 72 bool BrowserAccessibilityManager::RequestAccessibilityInfo( |
| 59 LONG child_id) { | 73 WebAccessibility::InParams* in, int routing_id, int process_id) { |
| 60 base::hash_map<LONG, BrowserAccessibility*>::iterator iter = | 74 // Create and populate IPC message structure, for retrieval of accessibility |
| 61 child_id_map_.find(child_id); | 75 // information from the renderer. |
| 62 if (iter != child_id_map_.end()) { | 76 WebAccessibility::InParams in_params; |
| 63 return iter->second; | 77 in_params.object_id = in->object_id; |
| 64 } else { | 78 in_params.function_id = in->function_id; |
| 65 return NULL; | 79 in_params.child_id = in->child_id; |
| 80 in_params.input_long1 = in->input_long1; |
| 81 in_params.input_long2 = in->input_long2; |
| 82 |
| 83 // Retrieve the RenderViewHost connected to this request. |
| 84 RenderViewHost* rvh = RenderViewHost::FromID(process_id, routing_id); |
| 85 |
| 86 // Send accessibility information retrieval message to the renderer. |
| 87 bool success = false; |
| 88 if (rvh && rvh->process() && rvh->process()->HasConnection()) { |
| 89 IPC::SyncMessage* msg = |
| 90 new ViewMsg_GetAccessibilityInfo(routing_id, in_params, &out_params_); |
| 91 // Necessary for the send to keep the UI responsive. |
| 92 msg->EnableMessagePumping(); |
| 93 success = rvh->process()->SendWithTimeout(msg, |
| 94 kAccessibilityMessageTimeOut); |
| 66 } | 95 } |
| 96 return success; |
| 67 } | 97 } |
| 68 | 98 |
| 69 IAccessible* BrowserAccessibilityManager::GetParentWindowIAccessible() { | 99 bool BrowserAccessibilityManager::ChangeAccessibilityFocus(int acc_obj_id, |
| 70 return window_iaccessible_; | 100 int process_id, |
| 101 int routing_id) { |
| 102 BrowserAccessibility* browser_acc = |
| 103 GetBrowserAccessibility(process_id, routing_id); |
| 104 if (browser_acc) { |
| 105 // Notify Access Technology that there was a change in keyboard focus. |
| 106 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, browser_acc->parent_hwnd(), |
| 107 OBJID_CLIENT, static_cast<LONG>(acc_obj_id)); |
| 108 return true; |
| 109 } |
| 110 return false; |
| 71 } | 111 } |
| 72 | 112 |
| 73 HWND BrowserAccessibilityManager::GetParentHWND() { | 113 bool BrowserAccessibilityManager::OnAccessibilityObjectStateChange( |
| 74 return parent_hwnd_; | 114 int acc_obj_id, int process_id, int routing_id) { |
| 115 BrowserAccessibility* browser_acc = |
| 116 GetBrowserAccessibility(process_id, routing_id); |
| 117 if (browser_acc) { |
| 118 // Notify Access Technology that there was a change in state. |
| 119 ::NotifyWinEvent(EVENT_OBJECT_STATECHANGE, browser_acc->parent_hwnd(), |
| 120 OBJID_CLIENT, static_cast<LONG>(acc_obj_id)); |
| 121 return true; |
| 122 } |
| 123 return false; |
| 75 } | 124 } |
| 76 | 125 |
| 77 BrowserAccessibility* BrowserAccessibilityManager::GetFocus( | 126 const WebAccessibility::OutParams& BrowserAccessibilityManager::response() { |
| 78 BrowserAccessibility* root) { | 127 return out_params_; |
| 79 if (focus_ && (!root || focus_->IsDescendantOf(root))) | 128 } |
| 80 return focus_; | |
| 81 | 129 |
| 130 BrowserAccessibility* BrowserAccessibilityManager::GetBrowserAccessibility( |
| 131 int process_id, int routing_id) { |
| 132 // Retrieve the BrowserAccessibility connected to the requester's id. There |
| 133 // could be multiple BrowserAccessibility connected to the given |process_id|, |
| 134 // but they all have the same parent HWND, so using the first hit is fine. |
| 135 RenderProcessHostMap::iterator it = |
| 136 render_process_host_map_.lower_bound(process_id); |
| 137 |
| 138 RenderProcessHostMap::iterator end_of_matching_objects = |
| 139 render_process_host_map_.upper_bound(process_id); |
| 140 |
| 141 for (; it != end_of_matching_objects; ++it) { |
| 142 if (it->second && it->second->routing_id() == routing_id) |
| 143 return it->second; |
| 144 } |
| 82 return NULL; | 145 return NULL; |
| 83 } | 146 } |
| 84 | 147 |
| 85 void BrowserAccessibilityManager::OnAccessibilityFocusChange(int renderer_id) { | 148 void BrowserAccessibilityManager::Observe(NotificationType type, |
| 86 base::hash_map<int, LONG>::iterator iter = | 149 const NotificationSource& source, |
| 87 renderer_id_to_child_id_map_.find(renderer_id); | 150 const NotificationDetails& details) { |
| 88 if (iter == renderer_id_to_child_id_map_.end()) | 151 DCHECK(type == NotificationType::RENDERER_PROCESS_TERMINATED); |
| 89 return; | 152 RenderProcessHost* rph = Source<RenderProcessHost>(source).ptr(); |
| 153 DCHECK(rph); |
| 90 | 154 |
| 91 LONG child_id = iter->second; | 155 RenderProcessHostMap::iterator it = |
| 92 base::hash_map<LONG, BrowserAccessibility*>::iterator uniq_iter = | 156 render_process_host_map_.lower_bound(rph->id()); |
| 93 child_id_map_.find(child_id); | 157 |
| 94 if (uniq_iter != child_id_map_.end()) | 158 RenderProcessHostMap::iterator end_of_matching_objects = |
| 95 focus_ = uniq_iter->second; | 159 render_process_host_map_.upper_bound(rph->id()); |
| 96 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, child_id); | 160 |
| 161 for (; it != end_of_matching_objects; ++it) { |
| 162 if (it->second) { |
| 163 // Set all matching BrowserAccessibility instances to inactive state. |
| 164 // TODO(klink): Do more active memory cleanup as well. |
| 165 it->second->set_instance_active(false); |
| 166 } |
| 167 } |
| 97 } | 168 } |
| 98 | |
| 99 void BrowserAccessibilityManager::OnAccessibilityObjectStateChange( | |
| 100 int renderer_id) { | |
| 101 base::hash_map<int, LONG>::iterator iter = | |
| 102 renderer_id_to_child_id_map_.find(renderer_id); | |
| 103 if (iter == renderer_id_to_child_id_map_.end()) | |
| 104 return; | |
| 105 | |
| 106 LONG child_id = iter->second; | |
| 107 ::NotifyWinEvent(EVENT_OBJECT_FOCUS, parent_hwnd_, OBJID_CLIENT, child_id); | |
| 108 } | |
| 109 | |
| 110 BrowserAccessibility* BrowserAccessibilityManager::CreateAccessibilityTree( | |
| 111 BrowserAccessibility* parent, | |
| 112 const webkit_glue::WebAccessibility& src, | |
| 113 int index_in_parent) { | |
| 114 BrowserAccessibility* instance = factory_->Create(); | |
| 115 | |
| 116 // Get the next child ID, and wrap around when we get near the end | |
| 117 // of a 32-bit integer range. It's okay to wrap around; we just want | |
| 118 // to avoid it as long as possible because clients may cache the ID of | |
| 119 // an object for a while to determine if they've seen it before. | |
| 120 LONG child_id = next_child_id_; | |
| 121 next_child_id_--; | |
| 122 if (next_child_id_ == -2000000000) | |
| 123 next_child_id_ = -1; | |
| 124 | |
| 125 instance->Initialize(this, parent, child_id, index_in_parent, src); | |
| 126 child_id_map_[child_id] = instance; | |
| 127 renderer_id_to_child_id_map_[src.id] = child_id; | |
| 128 if ((src.state >> WebAccessibility::STATE_FOCUSED) & 1) | |
| 129 focus_ = instance; | |
| 130 for (int i = 0; i < static_cast<int>(src.children.size()); ++i) { | |
| 131 BrowserAccessibility* child = CreateAccessibilityTree( | |
| 132 instance, src.children[i], i); | |
| 133 instance->AddChild(child); | |
| 134 } | |
| 135 | |
| 136 return instance; | |
| 137 } | |
| OLD | NEW |