Home | History | Annotate | Download | only in automation
      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.
      4 
      5 // This file defines a mapping between Automation Proxy objects and
      6 // their associated app-side handles.
      7 
      8 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__
      9 #define CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__
     10 
     11 #include <map>
     12 
     13 #include "base/basictypes.h"
     14 #include "base/memory/ref_counted.h"
     15 #include "base/synchronization/lock.h"
     16 #include "ipc/ipc_channel.h"
     17 
     18 // This represents a value that the app's AutomationProvider returns
     19 // when asked for a resource (like a window or tab).
     20 typedef int AutomationHandle;
     21 
     22 class AutomationHandleTracker;
     23 class AutomationMessageSender;
     24 
     25 class AutomationResourceProxy
     26     : public base::RefCountedThreadSafe<AutomationResourceProxy> {
     27  public:
     28   AutomationResourceProxy(AutomationHandleTracker* tracker,
     29                           AutomationMessageSender* sender,
     30                           AutomationHandle handle);
     31 
     32   // Marks this proxy object as no longer valid; this generally means
     33   // that the corresponding resource on the app side is gone.
     34   void Invalidate() {
     35     is_valid_ = false;
     36     tracker_ = NULL;
     37   }
     38 
     39   bool is_valid() const { return is_valid_; }
     40 
     41   // Returns the handle that the app has generated to refer to this resource.
     42   AutomationHandle handle() { return handle_; }
     43 
     44  protected:
     45   friend class AutomationHandleTracker;
     46   friend class base::RefCountedThreadSafe<AutomationResourceProxy>;
     47 
     48   virtual ~AutomationResourceProxy();
     49 
     50   AutomationHandle handle_;
     51 
     52   // Not owned by us, owned by the AutomationProxy object. May be NULL if the
     53   // tracker has been destroyed (and hence the object is invalid).
     54   AutomationHandleTracker* tracker_;
     55 
     56   // Not owned by us.
     57   AutomationMessageSender* sender_;
     58 
     59  private:
     60   // True if the resource that this object is a proxy for on the app side
     61   // still exists.
     62   bool is_valid_;
     63 
     64   DISALLOW_COPY_AND_ASSIGN(AutomationResourceProxy);
     65 };
     66 
     67 // This class keeps track of the mapping between AutomationHandles and
     68 // AutomationResourceProxy objects.  This is important because (1) multiple
     69 // AutomationResourceProxy objects can be generated for the same handle
     70 // (2) handles can be invalidated by the app, and all the associated
     71 // proxy objects then need to be invalidated, and (3) when a handle is no
     72 // longer being used on this end, we need to tell the app that it can
     73 // discard the handle.
     74 class AutomationHandleTracker {
     75  public:
     76   explicit AutomationHandleTracker();
     77   ~AutomationHandleTracker();
     78 
     79   // Adds the specified proxy object to the tracker.
     80   void Add(AutomationResourceProxy* proxy);
     81 
     82   // Removes a given proxy object from the mapping, and unregisters the
     83   // handle on the app side if this was the last proxy object that was using
     84   // that handle.  This is a no-op if the proxy object is not currently
     85   // in the tracker.
     86   void Remove(AutomationResourceProxy* proxy);
     87 
     88   // Marks all proxy objects related to a given handle invalid.  This is
     89   // used when a resource (like a window) on the app side is closed, meaning
     90   // that no further operations can be completed using the handle that
     91   // identified that resource.
     92   void InvalidateHandle(AutomationHandle handle);
     93 
     94   AutomationResourceProxy* GetResource(AutomationHandle handle);
     95 
     96   void put_channel(IPC::Channel* channel) {
     97     channel_ = channel;
     98   }
     99 
    100  private:
    101   typedef
    102     std::map<AutomationHandle, scoped_refptr<AutomationResourceProxy> >
    103         HandleToObjectMap;
    104   typedef std::pair<AutomationHandle, AutomationResourceProxy*> MapEntry;
    105 
    106   HandleToObjectMap handle_to_object_;
    107 
    108   base::Lock map_lock_;
    109   IPC::Channel* channel_;
    110   DISALLOW_COPY_AND_ASSIGN(AutomationHandleTracker);
    111 };
    112 
    113 #endif  // CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__
    114