Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / automation / automation_resource_tracker.cc
bloba224393ffb99cd64781e0ab37f4488f2bffd075b
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(
10 IPC::Sender* sender)
11 : sender_(sender) {
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);
29 return handle;
32 void AutomationResourceTrackerImpl::RemoveImpl(const void* resource) {
33 if (!ContainsResourceImpl(resource))
34 return;
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;
47 return ++handle;
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())
61 return NULL;
63 return iter->second;
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;
72 return iter->second;
75 void AutomationResourceTrackerImpl::HandleCloseNotification(
76 const void* resource) {
77 if (!ContainsResourceImpl(resource))
78 return;
80 sender_->Send(
81 new AutomationMsg_InvalidateHandle(resource_to_handle_[resource]));
83 RemoveImpl(resource);