blob: 1559670e5335d9aad6f9ef9a3ae539c535b11075 [file] [log] [blame]
[email protected]f20d7332011-03-08 21:11:531// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
5#ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__
6#define CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
9#include <map>
10
11#include "base/basictypes.h"
[email protected]f20d7332011-03-08 21:11:5312#include "content/common/notification_observer.h"
13#include "content/common/notification_registrar.h"
14#include "content/common/notification_source.h"
[email protected]0d6e9bd2011-10-18 04:29:1615#include "content/public/browser/notification_types.h"
[email protected]946d1b22009-07-22 23:57:2116#include "ipc/ipc_message.h"
[email protected]ce560f82009-06-03 09:39:4417
initial.commit09911bf2008-07-26 23:55:2918// Template trick so that AutomationResourceTracker can be used with non-pointer
19// types.
20template <class T>
21struct AutomationResourceTraits {
22 typedef T ValueType;
23};
24
25template <class T>
26struct AutomationResourceTraits<T*> {
27 typedef T ValueType;
28};
29
30// This class exists for the sole purpose of allowing some of the implementation
31// of AutomationResourceTracker to live in a .cc file.
32class AutomationResourceTrackerImpl {
[email protected]11f4857282009-11-13 19:56:1733 public:
[email protected]7e4468d52010-09-22 19:42:0034 explicit AutomationResourceTrackerImpl(IPC::Message::Sender* sender);
35 virtual ~AutomationResourceTrackerImpl();
initial.commit09911bf2008-07-26 23:55:2936
[email protected]416ad1e2010-12-08 07:35:2737 protected:
initial.commit09911bf2008-07-26 23:55:2938 // These need to be implemented in AutomationResourceTracker,
39 // since it needs to call the subclass's type-specific notification
40 // registration functions.
[email protected]9adb9692010-10-29 23:14:0241 virtual void AddObserverTypeProxy(const void* resource) = 0;
42 virtual void RemoveObserverTypeProxy(const void* resource) = 0;
initial.commit09911bf2008-07-26 23:55:2943
[email protected]9adb9692010-10-29 23:14:0244 int AddImpl(const void* resource);
45 void RemoveImpl(const void* resource);
initial.commit09911bf2008-07-26 23:55:2946 int GenerateHandle();
[email protected]9adb9692010-10-29 23:14:0247 bool ContainsResourceImpl(const void* resource);
initial.commit09911bf2008-07-26 23:55:2948 bool ContainsHandleImpl(int handle);
[email protected]9adb9692010-10-29 23:14:0249 const void* GetResourceImpl(int handle);
50 int GetHandleImpl(const void* resource);
51 void HandleCloseNotification(const void* resource);
initial.commit09911bf2008-07-26 23:55:2952
[email protected]416ad1e2010-12-08 07:35:2753 private:
[email protected]9adb9692010-10-29 23:14:0254 typedef std::map<const void*, int> ResourceToHandleMap;
55 typedef std::map<int, const void*> HandleToResourceMap;
[email protected]416ad1e2010-12-08 07:35:2756
initial.commit09911bf2008-07-26 23:55:2957 ResourceToHandleMap resource_to_handle_;
58 HandleToResourceMap handle_to_resource_;
59
initial.commit09911bf2008-07-26 23:55:2960 IPC::Message::Sender* sender_;
[email protected]416ad1e2010-12-08 07:35:2761
62 DISALLOW_COPY_AND_ASSIGN(AutomationResourceTrackerImpl);
initial.commit09911bf2008-07-26 23:55:2963};
64
65// This template defines a superclass for an object that wants to track
66// a particular kind of application resource (like windows or tabs) for
67// automation purposes. The only things that a subclass should need to
68// define are AddObserver and RemoveObserver for the given resource's
[email protected]1c58a5c2009-05-21 18:47:1469// close notifications.
initial.commit09911bf2008-07-26 23:55:2970template <class T>
[email protected]416ad1e2010-12-08 07:35:2771class AutomationResourceTracker : public AutomationResourceTrackerImpl,
72 public NotificationObserver {
initial.commit09911bf2008-07-26 23:55:2973 public:
[email protected]11f4857282009-11-13 19:56:1774 explicit AutomationResourceTracker(IPC::Message::Sender* automation)
initial.commit09911bf2008-07-26 23:55:2975 : AutomationResourceTrackerImpl(automation) {}
76
initial.commit09911bf2008-07-26 23:55:2977 // The implementations for these should call the NotificationService
78 // to add and remove this object as an observer for the appropriate
79 // resource closing notification.
80 virtual void AddObserver(T resource) = 0;
81 virtual void RemoveObserver(T resource) = 0;
82
83 // Adds the given resource to this tracker, and returns a handle that
84 // can be used to refer to that resource. If the resource is already
85 // being tracked, the handle may be the same as one returned previously.
86 int Add(T resource) {
87 return AddImpl(resource);
88 }
89
90 // Removes the given resource from this tracker. If the resource is not
91 // currently present in the tracker, this is a no-op.
92 void Remove(T resource) {
93 RemoveImpl(resource);
94 }
95
96 // Returns true if this tracker currently tracks the resource pointed to
97 // by the parameter.
98 bool ContainsResource(T resource) {
99 return ContainsResourceImpl(resource);
100 }
101
102 // Returns true if this tracker currently tracks the given handle.
103 bool ContainsHandle(int handle) {
104 return ContainsHandleImpl(handle);
105 }
106
107 // Returns the resource pointer associated with a given handle, or NULL
108 // if that handle is not present in the mapping.
[email protected]9adb9692010-10-29 23:14:02109 // The casts here allow this to compile with both T = Foo and T = const Foo.
initial.commit09911bf2008-07-26 23:55:29110 T GetResource(int handle) {
[email protected]9adb9692010-10-29 23:14:02111 return static_cast<T>(const_cast<void*>(GetResourceImpl(handle)));
initial.commit09911bf2008-07-26 23:55:29112 }
113
114 // Returns the handle associated with a given resource pointer, or 0 if
115 // the resource is not currently in the mapping.
116 int GetHandle(T resource) {
117 return GetHandleImpl(resource);
118 }
119
120 // NotificationObserver implementation--the only thing that this tracker
121 // does in response to notifications is to tell the AutomationProxy
122 // that the associated handle is now invalid.
[email protected]432115822011-07-10 15:52:27123 virtual void Observe(int type,
initial.commit09911bf2008-07-26 23:55:29124 const NotificationSource& source,
[email protected]11f4857282009-11-13 19:56:17125 const NotificationDetails& details) {
initial.commit09911bf2008-07-26 23:55:29126 T resource =
127 Source<typename AutomationResourceTraits<T>::ValueType>(source).ptr();
128
[email protected]790788ac2010-04-06 17:52:19129 CloseResource(resource);
initial.commit09911bf2008-07-26 23:55:29130 }
initial.commit09911bf2008-07-26 23:55:29131
[email protected]1c58a5c2009-05-21 18:47:14132 protected:
[email protected]790788ac2010-04-06 17:52:19133 // Removes |resource| from the tracker, and handles sending the close
134 // notification back to the client. This typically should not be called
135 // directly, unless there is no appropriate notification available
136 // for the resource type.
137 void CloseResource(T resource) {
138 HandleCloseNotification(resource);
139 }
140
[email protected]1c58a5c2009-05-21 18:47:14141 // These proxy calls from the base Impl class to the template's subclss.
[email protected]9adb9692010-10-29 23:14:02142 // The casts here allow this to compile with both T = Foo and T = const Foo.
143 virtual void AddObserverTypeProxy(const void* resource) {
144 AddObserver(static_cast<T>(const_cast<void*>(resource)));
[email protected]1c58a5c2009-05-21 18:47:14145 }
[email protected]9adb9692010-10-29 23:14:02146 virtual void RemoveObserverTypeProxy(const void* resource) {
147 RemoveObserver(static_cast<T>(const_cast<void*>(resource)));
[email protected]1c58a5c2009-05-21 18:47:14148 }
149
[email protected]416ad1e2010-12-08 07:35:27150 NotificationRegistrar registrar_;
151
152 private:
[email protected]1c58a5c2009-05-21 18:47:14153 DISALLOW_COPY_AND_ASSIGN(AutomationResourceTracker);
initial.commit09911bf2008-07-26 23:55:29154};
155
156#endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__