• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chrome/browser/automation/automation_tab_tracker.h"
6 
7 #include "content/browser/tab_contents/navigation_controller.h"
8 #include "content/common/notification_source.h"
9 #include "content/common/notification_type.h"
10 
AutomationTabTracker(IPC::Message::Sender * automation)11 AutomationTabTracker::AutomationTabTracker(IPC::Message::Sender* automation)
12     : AutomationResourceTracker<NavigationController*>(automation) {
13 }
14 
~AutomationTabTracker()15 AutomationTabTracker::~AutomationTabTracker() {
16 }
17 
AddObserver(NavigationController * resource)18 void AutomationTabTracker::AddObserver(NavigationController* resource) {
19   // This tab could either be a regular tab or an external tab
20   // Register for both notifications.
21   registrar_.Add(this, NotificationType::TAB_CLOSING,
22                  Source<NavigationController>(resource));
23   registrar_.Add(this, NotificationType::EXTERNAL_TAB_CLOSED,
24                  Source<NavigationController>(resource));
25   // We also want to know about navigations so we can keep track of the last
26   // navigation time.
27   registrar_.Add(this, NotificationType::LOAD_STOP,
28                  Source<NavigationController>(resource));
29 }
30 
RemoveObserver(NavigationController * resource)31 void AutomationTabTracker::RemoveObserver(NavigationController* resource) {
32   registrar_.Remove(this, NotificationType::TAB_CLOSING,
33                     Source<NavigationController>(resource));
34   registrar_.Remove(this, NotificationType::EXTERNAL_TAB_CLOSED,
35                     Source<NavigationController>(resource));
36   registrar_.Remove(this, NotificationType::LOAD_STOP,
37                     Source<NavigationController>(resource));
38 }
39 
Observe(NotificationType type,const NotificationSource & source,const NotificationDetails & details)40 void AutomationTabTracker::Observe(NotificationType type,
41                                    const NotificationSource& source,
42                                    const NotificationDetails& details) {
43   switch (type.value) {
44     case NotificationType::LOAD_STOP:
45       last_navigation_times_[Source<NavigationController>(source).ptr()] =
46           base::Time::Now();
47       return;
48     case NotificationType::EXTERNAL_TAB_CLOSED:
49     case NotificationType::TAB_CLOSING:
50       {
51         std::map<NavigationController*, base::Time>::iterator iter =
52             last_navigation_times_.find(
53                 Source<NavigationController>(source).ptr());
54         if (iter != last_navigation_times_.end())
55           last_navigation_times_.erase(iter);
56       }
57       break;
58     default:
59       NOTREACHED();
60   }
61   AutomationResourceTracker<NavigationController*>::Observe(type, source,
62                                                             details);
63 }
64 
GetLastNavigationTime(int handle)65 base::Time AutomationTabTracker::GetLastNavigationTime(int handle) {
66   if (ContainsHandle(handle)) {
67     NavigationController* controller = GetResource(handle);
68     if (controller) {
69       std::map<NavigationController*, base::Time>::const_iterator iter =
70           last_navigation_times_.find(controller);
71       if (iter != last_navigation_times_.end())
72         return iter->second;
73     }
74   }
75   return base::Time();
76 }
77