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
3 // found in the LICENSE file.
5 #include "chrome/test/automation/automation_handle_tracker.h"
7 #include "chrome/common/automation_messages.h"
8 #include "chrome/test/automation/automation_proxy.h"
10 AutomationResourceProxy::AutomationResourceProxy(
11 AutomationHandleTracker
* tracker
, AutomationMessageSender
* sender
,
12 AutomationHandle handle
)
20 AutomationResourceProxy::~AutomationResourceProxy() {
22 tracker_
->Remove(this);
25 AutomationHandleTracker::AutomationHandleTracker() : channel_(NULL
) {}
27 AutomationHandleTracker::~AutomationHandleTracker() {
28 // Tell any live objects that the tracker is going away so they don't try to
29 // call us when they are being destroyed.
30 for (HandleToObjectMap::iterator iter
= handle_to_object_
.begin();
31 iter
!= handle_to_object_
.end(); ++iter
) {
32 iter
->second
->Invalidate();
36 void AutomationHandleTracker::Add(AutomationResourceProxy
* proxy
) {
37 base::AutoLock
lock(map_lock_
);
38 handle_to_object_
.insert(MapEntry(proxy
->handle(), proxy
));
41 void AutomationHandleTracker::Remove(AutomationResourceProxy
* proxy
) {
42 base::AutoLock
lock(map_lock_
);
43 HandleToObjectMap::iterator iter
= handle_to_object_
.find(proxy
->handle());
44 if (iter
!= handle_to_object_
.end()) {
45 AutomationHandle proxy_handle
= proxy
->handle();
46 handle_to_object_
.erase(iter
);
48 channel_
->Send(new AutomationMsg_HandleUnused(proxy_handle
));
52 void AutomationHandleTracker::InvalidateHandle(AutomationHandle handle
) {
53 // Called in background thread.
54 base::AutoLock
lock(map_lock_
);
55 HandleToObjectMap::iterator iter
= handle_to_object_
.find(handle
);
56 if (iter
!= handle_to_object_
.end()) {
57 scoped_refptr
<AutomationResourceProxy
> proxy
= iter
->second
;
58 handle_to_object_
.erase(iter
);
63 AutomationResourceProxy
* AutomationHandleTracker::GetResource(
64 AutomationHandle handle
) {
65 base::AutoLock
lock(map_lock_
);
66 HandleToObjectMap::iterator iter
= handle_to_object_
.find(handle
);
67 if (iter
== handle_to_object_
.end())
70 iter
->second
->AddRef();
71 return iter
->second
.get();