OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_NET_CACHE_STATS_H_ |
| 6 #define CHROME_BROWSER_NET_CACHE_STATS_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/hash_tables.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/singleton.h" |
| 16 #include "base/message_loop.h" |
| 17 #include "base/time.h" |
| 18 #include "content/public/browser/web_contents_observer.h" |
| 19 #include "net/base/network_delegate.h" |
| 20 |
| 21 class TabContents; |
| 22 |
| 23 namespace base { |
| 24 class Histogram; |
| 25 } |
| 26 |
| 27 namespace net { |
| 28 class URLRequest; |
| 29 } |
| 30 |
| 31 namespace chrome_browser_net { |
| 32 |
| 33 // This class collects UMA stats about cache performance. It is a singleton |
| 34 // and lives on the IO thread. |
| 35 class CacheStats : public MessageLoop::DestructionObserver { |
| 36 public: |
| 37 enum TabEvent { |
| 38 SPINNER_START, |
| 39 SPINNER_STOP |
| 40 }; |
| 41 static CacheStats* GetInstance(); |
| 42 void OnCacheWaitStateChange(const net::URLRequest& request, |
| 43 net::NetworkDelegate::CacheWaitState state); |
| 44 void OnTabEvent(std::pair<int, int> render_view_id, TabEvent event); |
| 45 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 46 virtual ~CacheStats(); |
| 47 |
| 48 private: |
| 49 friend struct DefaultSingletonTraits<CacheStats>; |
| 50 struct TabLoadStats; |
| 51 typedef std::map<std::pair<int, int>, TabLoadStats*> TabLoadStatsMap; |
| 52 |
| 53 CacheStats(); |
| 54 // Gets TabLoadStats for a given render view. |
| 55 TabLoadStats* GetTabLoadStats(std::pair<int, int> render_view_id); |
| 56 // Deletes TabLoadStats no longer needed for a render view. |
| 57 void RemoveTabLoadStats(std::pair<int, int> render_view_id); |
| 58 // Sets a timer for a given tab to collect stats. timer_index indicates |
| 59 // how many times stats have been collected since the navigation has started |
| 60 // for this tab. |
| 61 void ScheduleTimer(TabLoadStats* stats, int timer_index); |
| 62 // The callback when a timer fires to collect stats again. |
| 63 void TimerCb(TabLoadStats* stats, int timer_index); |
| 64 // Helper function to put the current set of cache statistics into an UMA |
| 65 // histogram. |
| 66 void RecordCacheFractionHistogram(base::TimeDelta elapsed, |
| 67 base::TimeDelta cache_time, |
| 68 bool is_load_done); |
| 69 |
| 70 TabLoadStatsMap tab_load_stats_; |
| 71 std::vector<base::Histogram*> final_histograms_; |
| 72 std::vector<base::Histogram*> intermediate_histograms_; |
| 73 bool registered_message_loop_destruction_observer_; |
| 74 }; |
| 75 |
| 76 // A WebContentsObserver watching all tabs, notifying CacheStats |
| 77 // whenever the spinner starts or stops for a given tab, and when a renderer |
| 78 // is no longer used. |
| 79 class CacheStatsTabHelper : public content::WebContentsObserver { |
| 80 public: |
| 81 explicit CacheStatsTabHelper(TabContents* tab); |
| 82 virtual ~CacheStatsTabHelper(); |
| 83 virtual void DidStartProvisionalLoadForFrame( |
| 84 int64 frame_id, |
| 85 bool is_main_frame, |
| 86 const GURL& validated_url, |
| 87 bool is_error_page, |
| 88 content::RenderViewHost* render_view_host) OVERRIDE; |
| 89 virtual void DidStopLoading(content::RenderViewHost* render_view_host) |
| 90 OVERRIDE; |
| 91 |
| 92 private: |
| 93 // Calls into CacheStats to notify that a reportable event has occurred |
| 94 // for the tab being observed. |
| 95 void NotifyCacheStats(CacheStats::TabEvent event, |
| 96 content::RenderViewHost* render_view_host); |
| 97 |
| 98 CacheStats* cache_stats_; |
| 99 bool is_profile_otr_; |
| 100 }; |
| 101 |
| 102 } // namespace chrome_browser_net |
| 103 |
| 104 #endif // CHROME_BROWSER_NET_CACHE_STATS_H_ |
OLD | NEW |