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.
5 #include "chrome/browser/automation/automation_resource_tracker.h"
7 #include "chrome/common/automation_messages.h"
9 AutomationResourceTrackerImpl::AutomationResourceTrackerImpl(
14 AutomationResourceTrackerImpl::~AutomationResourceTrackerImpl() {
17 int AutomationResourceTrackerImpl::AddImpl(const void* resource
) {
18 if (ContainsResourceImpl(resource
))
19 return resource_to_handle_
[resource
];
21 int handle
= GenerateHandle();
22 DCHECK(!ContainsHandleImpl(handle
));
24 resource_to_handle_
[resource
] = handle
;
25 handle_to_resource_
[handle
] = resource
;
27 AddObserverTypeProxy(resource
);
32 void AutomationResourceTrackerImpl::RemoveImpl(const void* resource
) {
33 if (!ContainsResourceImpl(resource
))
36 int handle
= resource_to_handle_
[resource
];
37 DCHECK_EQ(resource
, handle_to_resource_
[handle
]);
39 RemoveObserverTypeProxy(resource
);
41 resource_to_handle_
.erase(resource
);
42 handle_to_resource_
.erase(handle
);
45 int AutomationResourceTrackerImpl::GenerateHandle() {
46 static int handle
= 0;
50 bool AutomationResourceTrackerImpl::ContainsResourceImpl(const void* resource
) {
51 return resource_to_handle_
.find(resource
) != resource_to_handle_
.end();
54 bool AutomationResourceTrackerImpl::ContainsHandleImpl(int handle
) {
55 return handle_to_resource_
.find(handle
) != handle_to_resource_
.end();
58 const void* AutomationResourceTrackerImpl::GetResourceImpl(int handle
) {
59 HandleToResourceMap::const_iterator iter
= handle_to_resource_
.find(handle
);
60 if (iter
== handle_to_resource_
.end())
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())
75 void AutomationResourceTrackerImpl::HandleCloseNotification(
76 const void* resource
) {
77 if (!ContainsResourceImpl(resource
))
81 new AutomationMsg_InvalidateHandle(resource_to_handle_
[resource
]));