Allow overlapping sync and async startup requests
[chromium-blink-merge.git] / chrome / test / automation / automation_handle_tracker.h
bloba0befb44122a02544643c1f911fd0811c1c581d5
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 // This file defines a mapping between Automation Proxy objects and
6 // their associated app-side handles.
8 #ifndef CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__
9 #define CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__
11 #include <map>
13 #include "base/basictypes.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/synchronization/lock.h"
16 #include "ipc/ipc_channel.h"
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;
22 class AutomationHandleTracker;
23 class AutomationMessageSender;
25 class AutomationResourceProxy
26 : public base::RefCountedThreadSafe<AutomationResourceProxy> {
27 public:
28 AutomationResourceProxy(AutomationHandleTracker* tracker,
29 AutomationMessageSender* sender,
30 AutomationHandle handle);
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;
39 bool is_valid() const { return is_valid_; }
41 // Returns the handle that the app has generated to refer to this resource.
42 AutomationHandle handle() { return handle_; }
44 protected:
45 friend class AutomationHandleTracker;
46 friend class base::RefCountedThreadSafe<AutomationResourceProxy>;
48 virtual ~AutomationResourceProxy();
50 AutomationHandle handle_;
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_;
56 // Not owned by us.
57 AutomationMessageSender* sender_;
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_;
64 DISALLOW_COPY_AND_ASSIGN(AutomationResourceProxy);
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();
79 // Adds the specified proxy object to the tracker.
80 void Add(AutomationResourceProxy* proxy);
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);
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);
94 AutomationResourceProxy* GetResource(AutomationHandle handle);
96 void put_channel(IPC::Channel* channel) {
97 channel_ = channel;
100 private:
101 typedef
102 std::map<AutomationHandle, scoped_refptr<AutomationResourceProxy> >
103 HandleToObjectMap;
104 typedef std::pair<AutomationHandle, AutomationResourceProxy*> MapEntry;
106 HandleToObjectMap handle_to_object_;
108 base::Lock map_lock_;
109 IPC::Channel* channel_;
110 DISALLOW_COPY_AND_ASSIGN(AutomationHandleTracker);
113 #endif // CHROME_TEST_AUTOMATION_AUTOMATION_HANDLE_TRACKER_H__