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. It is a singleton | |
48 // and lives on the IO thread. | |
49 class CacheStats { | |
50 public: | |
51 enum TabEvent { | |
52 SPINNER_START, | |
53 SPINNER_STOP | |
54 }; | |
55 void OnCacheWaitStateChange(const net::URLRequest& request, | |
56 net::NetworkDelegate::CacheWaitState state); | |
57 void OnTabEvent(std::pair<int, int> render_view_id, TabEvent event); | |
58 void RegisterURLRequestContext(const net::URLRequestContext* context, | |
59 ChromeURLRequestContext::ContextType type); | |
60 void UnregisterURLRequestContext(const net::URLRequestContext* context); | |
61 CacheStats(); | |
62 virtual ~CacheStats(); | |
63 | |
64 private: | |
65 struct TabLoadStats; | |
66 typedef std::map<std::pair<int, int>, TabLoadStats*> TabLoadStatsMap; | |
67 | |
68 // Gets TabLoadStats for a given render view. | |
69 TabLoadStats* GetTabLoadStats(std::pair<int, int> render_view_id); | |
70 // Deletes TabLoadStats no longer needed for a render view. | |
71 void RemoveTabLoadStats(std::pair<int, int> render_view_id); | |
72 // Sets a timer for a given tab to collect stats. timer_index indicates | |
73 // how many times stats have been collected since the navigation has started | |
74 // for this tab. | |
75 void ScheduleTimer(TabLoadStats* stats, int timer_index); | |
76 // The callback when a timer fires to collect stats again. | |
77 void TimerCb(TabLoadStats* stats, int timer_index); | |
willchan no longer on Chromium
2012/07/26 01:39:37
No abbreviation please, TimerCallback.
tburkard
2012/07/26 02:10:40
Done.
| |
78 // Helper function to put the current set of cache statistics into an UMA | |
79 // histogram. | |
80 void RecordCacheFractionHistogram(base::TimeDelta elapsed, | |
81 base::TimeDelta cache_time, | |
82 bool is_load_done); | |
83 | |
84 TabLoadStatsMap tab_load_stats_; | |
85 std::vector<base::Histogram*> final_histograms_; | |
86 std::vector<base::Histogram*> intermediate_histograms_; | |
87 base::hash_set<const net::URLRequestContext*> main_request_contexts_; | |
88 }; | |
89 | |
90 // A WebContentsObserver watching all tabs, notifying CacheStats | |
91 // whenever the spinner starts or stops for a given tab, and when a renderer | |
92 // is no longer used. | |
93 class CacheStatsTabHelper : public content::WebContentsObserver { | |
94 public: | |
95 explicit CacheStatsTabHelper(TabContents* tab); | |
96 virtual ~CacheStatsTabHelper(); | |
97 virtual void DidStartProvisionalLoadForFrame( | |
98 int64 frame_id, | |
99 bool is_main_frame, | |
100 const GURL& validated_url, | |
101 bool is_error_page, | |
102 content::RenderViewHost* render_view_host) OVERRIDE; | |
103 virtual void DidStopLoading(content::RenderViewHost* render_view_host) | |
104 OVERRIDE; | |
105 | |
106 private: | |
107 // Calls into CacheStats to notify that a reportable event has occurred | |
108 // for the tab being observed. | |
109 void NotifyCacheStats(CacheStats::TabEvent event, | |
110 content::RenderViewHost* render_view_host); | |
111 | |
112 CacheStats* cache_stats_; | |
113 bool is_profile_otr_; | |
willchan no longer on Chromium
2012/07/26 01:39:37
Is this still needed?
tburkard
2012/07/26 02:10:40
Yes, so that WebContents observed events that are
| |
114 }; | |
115 | |
116 } // namespace chrome_browser_net | |
117 | |
118 #endif // CHROME_BROWSER_NET_CACHE_STATS_H_ | |
OLD | NEW |