summaryrefslogtreecommitdiffstats
path: root/chrome/browser/automation/automation_resource_tracker.h
diff options
context:
space:
mode:
Diffstat (limited to 'chrome/browser/automation/automation_resource_tracker.h')
-rw-r--r--chrome/browser/automation/automation_resource_tracker.h180
1 files changed, 180 insertions, 0 deletions
diff --git a/chrome/browser/automation/automation_resource_tracker.h b/chrome/browser/automation/automation_resource_tracker.h
new file mode 100644
index 0000000..f2380e9
--- /dev/null
+++ b/chrome/browser/automation/automation_resource_tracker.h
@@ -0,0 +1,180 @@
+// Copyright 2008, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__
+#define CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__
+
+#include <map>
+
+#include "base/basictypes.h"
+#include "chrome/common/ipc_message.h"
+#include "chrome/common/notification_service.h"
+
+// Template trick so that AutomationResourceTracker can be used with non-pointer
+// types.
+template <class T>
+struct AutomationResourceTraits {
+ typedef T ValueType;
+};
+
+template <class T>
+struct AutomationResourceTraits<T*> {
+ typedef T ValueType;
+};
+
+// This class exists for the sole purpose of allowing some of the implementation
+// of AutomationResourceTracker to live in a .cc file.
+class AutomationResourceTrackerImpl {
+public:
+ AutomationResourceTrackerImpl(IPC::Message::Sender* sender)
+ :sender_(sender), cleared_mappings_(false) {}
+
+ virtual ~AutomationResourceTrackerImpl() {}
+
+ // These need to be implemented in AutomationResourceTracker,
+ // since it needs to call the subclass's type-specific notification
+ // registration functions.
+ virtual void AddObserverTypeProxy(void* resource) = 0;
+ virtual void RemoveObserverTypeProxy(void* resource) = 0;
+
+ int AddImpl(void* resource);
+ void RemoveImpl(void* resource);
+ int GenerateHandle();
+ bool ContainsResourceImpl(void* resource);
+ bool ContainsHandleImpl(int handle);
+ void ClearAllMappingsImpl();
+ void* GetResourceImpl(int handle);
+ int GetHandleImpl(void* resource);
+ void HandleCloseNotification(void* resource);
+
+protected:
+ bool cleared_mappings_;
+ typedef std::map<void*, int> ResourceToHandleMap;
+ typedef std::map<int, void*> HandleToResourceMap;
+ ResourceToHandleMap resource_to_handle_;
+ HandleToResourceMap handle_to_resource_;
+
+private:
+ DISALLOW_EVIL_CONSTRUCTORS(AutomationResourceTrackerImpl);
+
+ IPC::Message::Sender* sender_;
+};
+
+// This template defines a superclass for an object that wants to track
+// a particular kind of application resource (like windows or tabs) for
+// automation purposes. The only things that a subclass should need to
+// define are AddObserver and RemoveObserver for the given resource's
+// close notifications, ***and a destructor that calls ClearAllMappings***.
+template <class T>
+class AutomationResourceTracker : public NotificationObserver,
+ private AutomationResourceTrackerImpl {
+ public:
+ AutomationResourceTracker(IPC::Message::Sender* automation)
+ : AutomationResourceTrackerImpl(automation) {}
+
+ virtual ~AutomationResourceTracker() {
+ // NOTE: Be sure to call ClearAllMappings() from the destructor of your
+ // subclass! It can't be called here, because it eventually uses
+ // the subclass's RemoveObserver, which no longer exists by the time
+ // this base class destructor is executed.
+ DCHECK(cleared_mappings_);
+ }
+
+ // Removes all mappings from this tracker, including unregistering from
+ // any associated resource notifications (via Remove calling RemoveObserver).
+ void ClearAllMappings() {
+ ClearAllMappingsImpl();
+ }
+
+ // The implementations for these should call the NotificationService
+ // to add and remove this object as an observer for the appropriate
+ // resource closing notification.
+ virtual void AddObserver(T resource) = 0;
+ virtual void RemoveObserver(T resource) = 0;
+
+ // Adds the given resource to this tracker, and returns a handle that
+ // can be used to refer to that resource. If the resource is already
+ // being tracked, the handle may be the same as one returned previously.
+ int Add(T resource) {
+ return AddImpl(resource);
+ }
+
+ // Removes the given resource from this tracker. If the resource is not
+ // currently present in the tracker, this is a no-op.
+ void Remove(T resource) {
+ RemoveImpl(resource);
+ }
+
+ // Returns true if this tracker currently tracks the resource pointed to
+ // by the parameter.
+ bool ContainsResource(T resource) {
+ return ContainsResourceImpl(resource);
+ }
+
+ // Returns true if this tracker currently tracks the given handle.
+ bool ContainsHandle(int handle) {
+ return ContainsHandleImpl(handle);
+ }
+
+ // Returns the resource pointer associated with a given handle, or NULL
+ // if that handle is not present in the mapping.
+ T GetResource(int handle) {
+ return static_cast<T>(GetResourceImpl(handle));
+ }
+
+ // Returns the handle associated with a given resource pointer, or 0 if
+ // the resource is not currently in the mapping.
+ int GetHandle(T resource) {
+ return GetHandleImpl(resource);
+ }
+
+ // NotificationObserver implementation--the only thing that this tracker
+ // does in response to notifications is to tell the AutomationProxy
+ // that the associated handle is now invalid.
+ virtual void Observe(NotificationType type,
+ const NotificationSource& source,
+ const NotificationDetails& details){
+ T resource =
+ Source<typename AutomationResourceTraits<T>::ValueType>(source).ptr();
+
+ HandleCloseNotification(resource);
+ }
+ private:
+ // These proxy calls from the base Impl class to the template's subclss.
+ virtual void AddObserverTypeProxy(void* resource) {
+ AddObserver(static_cast<T>(resource));
+ }
+ virtual void RemoveObserverTypeProxy(void* resource) {
+ RemoveObserver(static_cast<T>(resource));
+ }
+
+ DISALLOW_EVIL_CONSTRUCTORS(AutomationResourceTracker);
+};
+
+#endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_RESOURCE_TRACKER_H__