Home | History | Annotate | Download | only in network_time
      1 // Copyright 2013 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 #ifndef CHROME_BROWSER_NETWORK_TIME_NAVIGATION_TIME_HELPER_H_
      6 #define CHROME_BROWSER_NETWORK_TIME_NAVIGATION_TIME_HELPER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/time/time.h"
     11 #include "content/public/browser/notification_observer.h"
     12 #include "content/public/browser/notification_registrar.h"
     13 #include "content/public/browser/web_contents_user_data.h"
     14 
     15 namespace content {
     16 class WebContents;
     17 }
     18 
     19 // NavigationTimeHelper returns the navigation time in network time for
     20 // given navigation entry.
     21 class NavigationTimeHelper
     22     : public content::NotificationObserver,
     23       public content::WebContentsUserData<NavigationTimeHelper> {
     24  public:
     25   virtual ~NavigationTimeHelper();
     26 
     27   base::Time GetNavigationTime(const content::NavigationEntry* entry);
     28 
     29  protected:
     30   // Tests only.
     31   NavigationTimeHelper();
     32 
     33   // Return network time for given |local_time|.
     34   virtual base::Time GetNetworkTime(base::Time local_time);
     35 
     36  private:
     37   explicit NavigationTimeHelper(content::WebContents* web_contents);
     38   friend class content::WebContentsUserData<NavigationTimeHelper>;
     39 
     40   // content::NotificationObserver implementation.
     41   virtual void Observe(int type,
     42                        const content::NotificationSource& source,
     43                        const content::NotificationDetails& details) OVERRIDE;
     44 
     45   content::NotificationRegistrar registrar_;
     46 
     47   content::WebContents* web_contents_;
     48 
     49   // Map from navigation entries to the navigation times calculated for them.
     50   struct NavigationTimeInfo {
     51     NavigationTimeInfo(base::Time local_time, base::Time network_time)
     52         : local_time(local_time), network_time(network_time) {}
     53     base::Time local_time;
     54     base::Time network_time;
     55   };
     56   typedef std::map<const void*, NavigationTimeInfo> NavigationTimeCache;
     57   NavigationTimeCache time_cache_;
     58 
     59   DISALLOW_COPY_AND_ASSIGN(NavigationTimeHelper);
     60 };
     61 
     62 #endif  // CHROME_BROWSER_NETWORK_TIME_NAVIGATION_TIME_HELPER_H_
     63