1 // Copyright (c) 2011 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/automation/automation_resource_tracker.h" 6 7 #include "chrome/common/automation_messages.h" 8 9 AutomationResourceTrackerImpl::AutomationResourceTrackerImpl( 10 IPC::Message::Sender* sender) 11 : sender_(sender) { 12 } 13 14 AutomationResourceTrackerImpl::~AutomationResourceTrackerImpl() { 15 } 16 17 int AutomationResourceTrackerImpl::AddImpl(const void* resource) { 18 if (ContainsResourceImpl(resource)) 19 return resource_to_handle_[resource]; 20 21 int handle = GenerateHandle(); 22 DCHECK(!ContainsHandleImpl(handle)); 23 24 resource_to_handle_[resource] = handle; 25 handle_to_resource_[handle] = resource; 26 27 AddObserverTypeProxy(resource); 28 29 return handle; 30 } 31 32 void AutomationResourceTrackerImpl::RemoveImpl(const void* resource) { 33 if (!ContainsResourceImpl(resource)) 34 return; 35 36 int handle = resource_to_handle_[resource]; 37 DCHECK_EQ(resource, handle_to_resource_[handle]); 38 39 RemoveObserverTypeProxy(resource); 40 41 resource_to_handle_.erase(resource); 42 handle_to_resource_.erase(handle); 43 } 44 45 int AutomationResourceTrackerImpl::GenerateHandle() { 46 static int handle = 0; 47 return ++handle; 48 } 49 50 bool AutomationResourceTrackerImpl::ContainsResourceImpl(const void* resource) { 51 return resource_to_handle_.find(resource) != resource_to_handle_.end(); 52 } 53 54 bool AutomationResourceTrackerImpl::ContainsHandleImpl(int handle) { 55 return handle_to_resource_.find(handle) != handle_to_resource_.end(); 56 } 57 58 const void* AutomationResourceTrackerImpl::GetResourceImpl(int handle) { 59 HandleToResourceMap::const_iterator iter = handle_to_resource_.find(handle); 60 if (iter == handle_to_resource_.end()) 61 return NULL; 62 63 return iter->second; 64 } 65 66 int AutomationResourceTrackerImpl::GetHandleImpl(const void* resource) { 67 ResourceToHandleMap::const_iterator iter = 68 resource_to_handle_.find(resource); 69 if (iter == resource_to_handle_.end()) 70 return 0; 71 72 return iter->second; 73 } 74 75 void AutomationResourceTrackerImpl::HandleCloseNotification( 76 const void* resource) { 77 if (!ContainsResourceImpl(resource)) 78 return; 79 80 sender_->Send( 81 new AutomationMsg_InvalidateHandle(resource_to_handle_[resource])); 82 83 RemoveImpl(resource); 84 } 85