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 #pragma once | |
8 | |
9 #include <map> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/basictypes.h" | |
14 #include "base/hash_tables.h" | |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/memory/singleton.h" | |
17 #include "base/message_loop.h" | |
18 #include "base/time.h" | |
19 #include "content/public/browser/render_view_host_observer.h" | |
20 #include "net/base/network_delegate.h" | |
21 | |
22 class TabContents; | |
23 | |
24 namespace base { | |
25 class Histogram; | |
26 } | |
27 | |
28 namespace net { | |
29 class URLRequest; | |
30 } | |
31 | |
32 namespace chrome_browser_net { | |
33 | |
34 // This class collects UMA stats about cache performance. It is a singleton | |
35 // and lives on the IO thread. | |
36 | |
37 class CacheStats : public MessageLoop::DestructionObserver { | |
38 public: | |
39 enum TabEvent { | |
40 SPINNER_START, | |
41 SPINNER_STOP, | |
42 RENDERER_DESTROY | |
43 }; | |
44 static CacheStats* GetInstance(); | |
45 void OnCacheWaitStateChange(const net::URLRequest& request, | |
46 net::NetworkDelegate::CacheWaitState state); | |
47 void OnTabEvent(std::pair<int, int> render_view_id, TabEvent event); | |
48 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | |
49 virtual ~CacheStats(); | |
50 | |
51 private: | |
52 friend struct DefaultSingletonTraits<CacheStats>; | |
53 struct TabLoadStats; | |
54 typedef std::map< std::pair<int, int>, TabLoadStats*> TabLoadStatsMap; | |
55 | |
56 CacheStats(); | |
57 // Gets TabLoadStats for a given render view. | |
58 TabLoadStats* GetTabLoadStats(std::pair<int, int> render_view_id); | |
59 // Deletes TabLoadStats no longer needed for a render view. | |
60 void RemoveTabLoadStats(std::pair<int, int> render_view_id); | |
61 // Sets a timer for a given tab to collect stats. timer_index indicates | |
62 // how many times stats have been collected since the navigation has started | |
63 // for this tab. | |
64 void ScheduleTimer(TabLoadStats* stats, int timer_index); | |
65 // The callback when a timer fires to collect stats again. | |
66 void TimerCb(TabLoadStats* stats, int timer_index); | |
67 // Helper function to put the current set of cache statistics into an UMA | |
68 // histogram. | |
69 void RecordCacheFractionHistogram(base::TimeDelta elapsed, | |
70 base::TimeDelta cache_time, | |
71 bool is_load_done); | |
72 | |
73 TabLoadStatsMap tab_load_stats_; | |
74 std::vector<base::Histogram*> final_histograms_; | |
75 std::vector<base::Histogram*> intermediate_histograms_; | |
76 bool registered_message_loop_destruction_observer_; | |
77 }; | |
78 | |
79 // Helper class observing all RenderViewHosts, notifying CacheStats | |
80 // whenever the spinner starts or stops for a given tab, and when a renderer | |
81 // is no longer used. | |
82 | |
83 class CacheStatsRenderViewHostObserver : | |
84 public content::RenderViewHostObserver { | |
mmenke
2012/07/20 18:37:44
Fix indent. Also, I believe the more common style
tburkard
2012/07/20 19:36:42
Done.
| |
85 public: | |
86 explicit CacheStatsRenderViewHostObserver(content::RenderViewHost* host); | |
87 | |
88 protected: | |
89 virtual ~CacheStatsRenderViewHostObserver(); | |
90 virtual bool OnMessageReceived(const IPC::Message& message) 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 void OnDidStartProvisionalLoadForFrame( | |
97 int64 frame_id, | |
98 bool is_main_frame, | |
99 const GURL& opener_url, | |
100 const GURL& url); | |
101 void OnDidStopLoading(); | |
102 | |
103 CacheStats* cache_stats_; | |
104 std::pair<int, int> render_view_id_; | |
105 bool is_loading_; | |
106 }; | |
107 | |
108 } // namespace chrome_browser_net | |
109 | |
110 #endif // CHROME_BROWSER_NET_CACHE_STATS_H_ | |
OLD | NEW |