summaryrefslogtreecommitdiffstats
path: root/chrome/test/automation/automation_handle_tracker.cc
blob: db688ccc5e212cd98552d33004c6438378275db2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/test/automation/automation_handle_tracker.h"

#include "chrome/test/automation/automation_messages.h"
#include "chrome/test/automation/automation_proxy.h"

AutomationResourceProxy::AutomationResourceProxy(
    AutomationHandleTracker* tracker, AutomationMessageSender* sender,
    AutomationHandle handle)
    : handle_(handle),
      tracker_(tracker),
      sender_(sender),
      is_valid_(true) {
      tracker_->Add(this);
}

AutomationResourceProxy::~AutomationResourceProxy() {
  if (tracker_)
    tracker_->Remove(this);
}

AutomationHandleTracker::~AutomationHandleTracker() {
  // Tell any live objects that the tracker is going away so they don't try to
  // call us when they are being destroyed.
  for (HandleToObjectMap::iterator iter = handle_to_object_.begin();
       iter != handle_to_object_.end(); ++iter) {
    iter->second->Invalidate();
    iter->second->TrackerGone();
  }
}

void AutomationHandleTracker::Add(AutomationResourceProxy* proxy) {
  AutoLock lock(map_lock_);
  handle_to_object_.insert(MapEntry(proxy->handle(), proxy));
}

void AutomationHandleTracker::Remove(AutomationResourceProxy* proxy) {
  AutoLock lock(map_lock_);
  HandleToObjectMap::iterator iter = handle_to_object_.find(proxy->handle());
  if (iter != handle_to_object_.end()) {
    handle_to_object_.erase(iter);
    sender_->Send(new AutomationMsg_HandleUnused(0, proxy->handle()));
  }
}

void AutomationHandleTracker::InvalidateHandle(AutomationHandle handle) {
  // Called in background thread.
  AutoLock lock(map_lock_);
  HandleToObjectMap::iterator iter = handle_to_object_.find(handle);
  if (iter != handle_to_object_.end()) {
    iter->second->Invalidate();
  }
}

AutomationResourceProxy* AutomationHandleTracker::GetResource(
    AutomationHandle handle) {
  DCHECK(handle);
  AutoLock lock(map_lock_);
  HandleToObjectMap::iterator iter = handle_to_object_.find(handle);
  if (iter == handle_to_object_.end())
    return NULL;

  iter->second->AddRef();
  return iter->second;
}