[email protected] | f20d733 | 2011-03-08 21:11:53 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__ |
| 6 | #define CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | |
| 9 | #include <map> |
| 10 | |
| 11 | #include "base/basictypes.h" |
[email protected] | f20d733 | 2011-03-08 21:11:53 | [diff] [blame] | 12 | #include "content/common/notification_observer.h" |
| 13 | #include "content/common/notification_registrar.h" |
| 14 | #include "content/common/notification_source.h" |
[email protected] | 0d6e9bd | 2011-10-18 04:29:16 | [diff] [blame^] | 15 | #include "content/public/browser/notification_types.h" |
[email protected] | 946d1b2 | 2009-07-22 23:57:21 | [diff] [blame] | 16 | #include "ipc/ipc_message.h" |
[email protected] | ce560f8 | 2009-06-03 09:39:44 | [diff] [blame] | 17 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | // Template trick so that AutomationResourceTracker can be used with non-pointer |
| 19 | // types. |
| 20 | template <class T> |
| 21 | struct AutomationResourceTraits { |
| 22 | typedef T ValueType; |
| 23 | }; |
| 24 | |
| 25 | template <class T> |
| 26 | struct 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. |
| 32 | class AutomationResourceTrackerImpl { |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 33 | public: |
[email protected] | 7e4468d5 | 2010-09-22 19:42:00 | [diff] [blame] | 34 | explicit AutomationResourceTrackerImpl(IPC::Message::Sender* sender); |
| 35 | virtual ~AutomationResourceTrackerImpl(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 36 | |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 37 | protected: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | // These need to be implemented in AutomationResourceTracker, |
| 39 | // since it needs to call the subclass's type-specific notification |
| 40 | // registration functions. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 41 | virtual void AddObserverTypeProxy(const void* resource) = 0; |
| 42 | virtual void RemoveObserverTypeProxy(const void* resource) = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 43 | |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 44 | int AddImpl(const void* resource); |
| 45 | void RemoveImpl(const void* resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 46 | int GenerateHandle(); |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 47 | bool ContainsResourceImpl(const void* resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | bool ContainsHandleImpl(int handle); |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 49 | const void* GetResourceImpl(int handle); |
| 50 | int GetHandleImpl(const void* resource); |
| 51 | void HandleCloseNotification(const void* resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 53 | private: |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 54 | typedef std::map<const void*, int> ResourceToHandleMap; |
| 55 | typedef std::map<int, const void*> HandleToResourceMap; |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 56 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | ResourceToHandleMap resource_to_handle_; |
| 58 | HandleToResourceMap handle_to_resource_; |
| 59 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 60 | IPC::Message::Sender* sender_; |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(AutomationResourceTrackerImpl); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | }; |
| 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] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 69 | // close notifications. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 70 | template <class T> |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 71 | class AutomationResourceTracker : public AutomationResourceTrackerImpl, |
| 72 | public NotificationObserver { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 73 | public: |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 74 | explicit AutomationResourceTracker(IPC::Message::Sender* automation) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | : AutomationResourceTrackerImpl(automation) {} |
| 76 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 77 | // 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] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 109 | // The casts here allow this to compile with both T = Foo and T = const Foo. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | T GetResource(int handle) { |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 111 | return static_cast<T>(const_cast<void*>(GetResourceImpl(handle))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | } |
| 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] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 123 | virtual void Observe(int type, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | const NotificationSource& source, |
[email protected] | 11f485728 | 2009-11-13 19:56:17 | [diff] [blame] | 125 | const NotificationDetails& details) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 126 | T resource = |
| 127 | Source<typename AutomationResourceTraits<T>::ValueType>(source).ptr(); |
| 128 | |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 129 | CloseResource(resource); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 130 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 131 | |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 132 | protected: |
[email protected] | 790788ac | 2010-04-06 17:52:19 | [diff] [blame] | 133 | // 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] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 141 | // These proxy calls from the base Impl class to the template's subclss. |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 142 | // 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] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 145 | } |
[email protected] | 9adb969 | 2010-10-29 23:14:02 | [diff] [blame] | 146 | virtual void RemoveObserverTypeProxy(const void* resource) { |
| 147 | RemoveObserver(static_cast<T>(const_cast<void*>(resource))); |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 148 | } |
| 149 | |
[email protected] | 416ad1e | 2010-12-08 07:35:27 | [diff] [blame] | 150 | NotificationRegistrar registrar_; |
| 151 | |
| 152 | private: |
[email protected] | 1c58a5c | 2009-05-21 18:47:14 | [diff] [blame] | 153 | DISALLOW_COPY_AND_ASSIGN(AutomationResourceTracker); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__ |