OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ |
6 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ | 6 #define CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/time.h" | 11 #include "base/time.h" |
12 #include "chrome/browser/automation/automation_resource_tracker.h" | 12 #include "chrome/browser/automation/automation_resource_tracker.h" |
13 #include "chrome/browser/tab_contents/navigation_controller.h" | 13 |
14 #include "chrome/common/notification_registrar.h" | 14 class NavigationController; |
15 #include "chrome/common/notification_type.h" | 15 class NotificationType; |
16 | 16 |
17 class AutomationTabTracker | 17 class AutomationTabTracker |
18 : public AutomationResourceTracker<NavigationController*> { | 18 : public AutomationResourceTracker<NavigationController*> { |
19 public: | 19 public: |
20 explicit AutomationTabTracker(IPC::Message::Sender* automation) | 20 explicit AutomationTabTracker(IPC::Message::Sender* automation); |
21 : AutomationResourceTracker<NavigationController*>(automation) {} | 21 virtual ~AutomationTabTracker(); |
22 | 22 |
23 virtual ~AutomationTabTracker() { | 23 virtual void AddObserver(NavigationController* resource); |
24 } | 24 virtual void RemoveObserver(NavigationController* resource); |
25 | |
26 virtual void AddObserver(NavigationController* resource) { | |
27 // This tab could either be a regular tab or an external tab | |
28 // Register for both notifications. | |
29 registrar_.Add(this, NotificationType::TAB_CLOSING, | |
30 Source<NavigationController>(resource)); | |
31 registrar_.Add(this, NotificationType::EXTERNAL_TAB_CLOSED, | |
32 Source<NavigationController>(resource)); | |
33 // We also want to know about navigations so we can keep track of the last | |
34 // navigation time. | |
35 registrar_.Add(this, NotificationType::LOAD_STOP, | |
36 Source<NavigationController>(resource)); | |
37 } | |
38 | |
39 virtual void RemoveObserver(NavigationController* resource) { | |
40 registrar_.Remove(this, NotificationType::TAB_CLOSING, | |
41 Source<NavigationController>(resource)); | |
42 registrar_.Remove(this, NotificationType::EXTERNAL_TAB_CLOSED, | |
43 Source<NavigationController>(resource)); | |
44 registrar_.Remove(this, NotificationType::LOAD_STOP, | |
45 Source<NavigationController>(resource)); | |
46 } | |
47 | 25 |
48 virtual void Observe(NotificationType type, | 26 virtual void Observe(NotificationType type, |
49 const NotificationSource& source, | 27 const NotificationSource& source, |
50 const NotificationDetails& details) { | 28 const NotificationDetails& details); |
51 switch (type.value) { | |
52 case NotificationType::LOAD_STOP: | |
53 last_navigation_times_[Source<NavigationController>(source).ptr()] = | |
54 base::Time::Now(); | |
55 return; | |
56 case NotificationType::EXTERNAL_TAB_CLOSED: | |
57 case NotificationType::TAB_CLOSING: | |
58 { | |
59 std::map<NavigationController*, base::Time>::iterator iter = | |
60 last_navigation_times_.find( | |
61 Source<NavigationController>(source).ptr()); | |
62 if (iter != last_navigation_times_.end()) | |
63 last_navigation_times_.erase(iter); | |
64 } | |
65 break; | |
66 default: | |
67 NOTREACHED(); | |
68 } | |
69 AutomationResourceTracker<NavigationController*>::Observe(type, source, | |
70 details); | |
71 } | |
72 | 29 |
73 base::Time GetLastNavigationTime(int handle) { | 30 base::Time GetLastNavigationTime(int handle); |
74 if (ContainsHandle(handle)) { | |
75 NavigationController* controller = GetResource(handle); | |
76 if (controller) { | |
77 std::map<NavigationController*, base::Time>::const_iterator iter = | |
78 last_navigation_times_.find(controller); | |
79 if (iter != last_navigation_times_.end()) | |
80 return iter->second; | |
81 } | |
82 } | |
83 return base::Time(); | |
84 } | |
85 | 31 |
86 private: | 32 private: |
87 // Last time a navigation occurred. | 33 // Last time a navigation occurred. |
88 std::map<NavigationController*, base::Time> last_navigation_times_; | 34 std::map<NavigationController*, base::Time> last_navigation_times_; |
89 | 35 |
90 DISALLOW_COPY_AND_ASSIGN(AutomationTabTracker); | 36 DISALLOW_COPY_AND_ASSIGN(AutomationTabTracker); |
91 }; | 37 }; |
92 | 38 |
93 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ | 39 #endif // CHROME_BROWSER_AUTOMATION_AUTOMATION_TAB_TRACKER_H_ |
OLD | NEW |