Chromium Code Reviews| 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/message_loop.h" | |
| 16 #include "base/time.h" | |
| 17 #include "chrome/browser/net/chrome_url_request_context.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 class URLRequestContext; | |
| 30 } | |
| 31 | |
| 32 #if defined(COMPILER_GCC) | |
| 33 | |
| 34 namespace BASE_HASH_NAMESPACE { | |
| 35 template <> | |
| 36 struct hash<const net::URLRequestContext*> { | |
| 37 std::size_t operator()(const net::URLRequestContext* value) const { | |
| 38 return reinterpret_cast<std::size_t>(value); | |
| 39 } | |
| 40 }; | |
| 41 } | |
| 42 | |
| 43 #endif | |
| 44 | |
| 45 namespace chrome_browser_net { | |
| 46 | |
| 47 // This class collects UMA stats about cache performance. | |
| 48 class CacheStats { | |
| 49 public: | |
| 50 enum TabEvent { | |
| 51 SPINNER_START, | |
| 52 SPINNER_STOP | |
| 53 }; | |
| 54 void OnCacheWaitStateChange(const net::URLRequest& request, | |
| 55 net::NetworkDelegate::CacheWaitState state); | |
| 56 void OnTabEvent(std::pair<int, int> render_view_id, TabEvent event); | |
| 57 void RegisterURLRequestContext(const net::URLRequestContext* context, | |
| 58 ChromeURLRequestContext::ContextType type); | |
| 59 void UnregisterURLRequestContext(const net::URLRequestContext* context); | |
| 60 CacheStats(); | |
| 61 virtual ~CacheStats(); | |
| 62 | |
| 63 private: | |
| 64 // A map mapping a renderer's process id and route id to a TabLoadStats, | |
| 65 // representing that renderer's load statistics. | |
|
rvargas (doing something else)
2012/07/26 18:47:33
nit: move the comment one line down (to the map, n
tburkard
2012/07/26 19:12:49
Done.
| |
| 66 struct TabLoadStats; | |
| 67 typedef std::map<std::pair<int, int>, TabLoadStats*> TabLoadStatsMap; | |
| 68 | |
| 69 // Gets TabLoadStats for a given render view. | |
| 70 TabLoadStats* GetTabLoadStats(std::pair<int, int> render_view_id); | |
| 71 // Deletes TabLoadStats no longer needed for a render view. | |
| 72 void RemoveTabLoadStats(std::pair<int, int> render_view_id); | |
| 73 // Sets a timer for a given tab to collect stats. timer_index indicates | |
| 74 // how many times stats have been collected since the navigation has started | |
| 75 // for this tab. | |
| 76 void ScheduleTimer(TabLoadStats* stats, int timer_index); | |
| 77 // The callback when a timer fires to collect stats again. | |
| 78 void TimerCallback(TabLoadStats* stats, int timer_index); | |
| 79 // Helper function to put the current set of cache statistics into an UMA | |
| 80 // histogram. | |
| 81 void RecordCacheFractionHistogram(base::TimeDelta elapsed, | |
| 82 base::TimeDelta cache_time, | |
| 83 bool is_load_done); | |
| 84 | |
| 85 TabLoadStatsMap tab_load_stats_; | |
| 86 std::vector<base::Histogram*> final_histograms_; | |
| 87 std::vector<base::Histogram*> intermediate_histograms_; | |
| 88 base::hash_set<const net::URLRequestContext*> main_request_contexts_; | |
| 89 }; | |
| 90 | |
| 91 // A WebContentsObserver watching all tabs, notifying CacheStats | |
| 92 // whenever the spinner starts or stops for a given tab, and when a renderer | |
| 93 // is no longer used. | |
| 94 class CacheStatsTabHelper : public content::WebContentsObserver { | |
| 95 public: | |
| 96 explicit CacheStatsTabHelper(TabContents* tab); | |
| 97 virtual ~CacheStatsTabHelper(); | |
| 98 virtual void DidStartProvisionalLoadForFrame( | |
| 99 int64 frame_id, | |
| 100 bool is_main_frame, | |
| 101 const GURL& validated_url, | |
| 102 bool is_error_page, | |
| 103 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 104 virtual void DidStopLoading( | |
| 105 content::RenderViewHost* render_view_host) OVERRIDE; | |
| 106 | |
| 107 private: | |
| 108 // Calls into CacheStats to notify that a reportable event has occurred | |
| 109 // for the tab being observed. | |
| 110 void NotifyCacheStats(CacheStats::TabEvent event, | |
| 111 content::RenderViewHost* render_view_host); | |
| 112 | |
| 113 CacheStats* cache_stats_; | |
| 114 bool is_otr_profile_; | |
| 115 }; | |
| 116 | |
| 117 } // namespace chrome_browser_net | |
| 118 | |
| 119 #endif // CHROME_BROWSER_NET_CACHE_STATS_H_ | |
| OLD | NEW |