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 <vector> | |
10 | |
11 #include "base/basictypes.h" | |
12 #include "base/compiler_specific.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 CacheStats(); | |
55 ~CacheStats(); | |
56 | |
57 void OnCacheWaitStateChange(const net::URLRequest& request, | |
58 net::NetworkDelegate::CacheWaitState state); | |
59 void OnTabEvent(std::pair<int, int> render_view_id, TabEvent event); | |
60 void RegisterURLRequestContext(const net::URLRequestContext* context, | |
61 ChromeURLRequestContext::ContextType type); | |
62 void UnregisterURLRequestContext(const net::URLRequestContext* context); | |
63 | |
64 private: | |
65 struct TabLoadStats; | |
66 // A map mapping a renderer's process id and route id to a TabLoadStats, | |
67 // representing that renderer's load statistics. | |
68 typedef std::map<std::pair<int, int>, TabLoadStats*> TabLoadStatsMap; | |
69 | |
70 // Gets TabLoadStats for a given RenderView. | |
71 TabLoadStats* GetTabLoadStats(std::pair<int, int> render_view_id); | |
72 // Deletes TabLoadStats no longer needed for a render view. | |
73 void RemoveTabLoadStats(std::pair<int, int> render_view_id); | |
74 // Sets a timer for a given tab to collect stats. |timer_index| indicates | |
75 // how many times stats have been collected since the navigation has started | |
76 // for this tab. | |
77 void ScheduleTimer(TabLoadStats* stats); | |
78 // The callback when a timer fires to collect stats again. | |
79 void TimerCallback(TabLoadStats* stats); | |
80 // Helper function to put the current set of cache statistics into an UMA | |
81 // histogram. | |
82 void RecordCacheFractionHistogram(base::TimeDelta elapsed, | |
83 base::TimeDelta cache_time, | |
84 bool is_load_done, | |
85 int timer_index); | |
86 | |
87 TabLoadStatsMap tab_load_stats_; | |
88 std::vector<base::Histogram*> final_histograms_; | |
89 std::vector<base::Histogram*> intermediate_histograms_; | |
90 base::hash_set<const net::URLRequestContext*> main_request_contexts_; | |
91 | |
92 DISALLOW_COPY_AND_ASSIGN(CacheStats); | |
93 }; | |
94 | |
95 // A WebContentsObserver watching all tabs, notifying CacheStats | |
96 // whenever the spinner starts or stops for a given tab, and when a renderer | |
97 // is no longer used. | |
98 class CacheStatsTabHelper : public content::WebContentsObserver { | |
99 public: | |
100 explicit CacheStatsTabHelper(TabContents* tab); | |
101 virtual ~CacheStatsTabHelper(); | |
102 | |
103 // content::WebContentsObserver implementation | |
104 virtual void DidStartProvisionalLoadForFrame( | |
105 int64 frame_id, | |
106 bool is_main_frame, | |
107 const GURL& validated_url, | |
108 bool is_error_page, | |
109 content::RenderViewHost* render_view_host) OVERRIDE; | |
110 virtual void DidStopLoading( | |
111 content::RenderViewHost* render_view_host) OVERRIDE; | |
112 | |
113 private: | |
114 // Calls into CacheStats to notify that a reportable event has occurred | |
115 // for the tab being observed. | |
116 void NotifyCacheStats(CacheStats::TabEvent event, | |
117 content::RenderViewHost* render_view_host); | |
118 | |
119 CacheStats* cache_stats_; | |
120 bool is_otr_profile_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(CacheStatsTabHelper); | |
123 }; | |
124 | |
125 } // namespace chrome_browser_net | |
126 | |
127 #endif // CHROME_BROWSER_NET_CACHE_STATS_H_ | |
OLD | NEW |