Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <set> | 11 #include <set> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <set> | |
| 13 | 14 |
| 14 #include "base/time.h" | 15 #include "base/time.h" |
| 15 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 16 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class Time; | 20 class Time; |
| 20 } | 21 } |
| 21 | 22 |
| 23 class ExtensionWebRequestTimeTrackerDelegate { | |
|
Matt Perry
2011/10/06 22:55:54
This is good for testing, but I don't think the Ex
battre
2011/10/07 14:09:24
Done.
| |
| 24 public: | |
| 25 virtual ~ExtensionWebRequestTimeTrackerDelegate() {} | |
| 26 | |
| 27 // Notifies the delegate that |num_delayed_messages| of the last | |
| 28 // |total_num_messages| inspected messages were excessively/moderately | |
| 29 // delayed. Every excessively delayed message is also counted as a moderately | |
| 30 // delayed message. | |
| 31 virtual void NotifyExcessiveDelays( | |
| 32 void* profile, | |
| 33 size_t num_delayed_messages, | |
| 34 size_t total_num_messages, | |
| 35 const std::set<std::string>& extension_ids) = 0; | |
| 36 virtual void NotifyModerateDelays( | |
| 37 void* profile, | |
| 38 size_t num_delayed_messages, | |
| 39 size_t total_num_messages, | |
| 40 const std::set<std::string>& extension_ids) = 0; | |
| 41 }; | |
| 42 | |
| 22 // This class keeps monitors how much delay extensions add to network requests | 43 // This class keeps monitors how much delay extensions add to network requests |
| 23 // by using the webRequest API. If the delay is sufficient, we will warn the | 44 // by using the webRequest API. If the delay is sufficient, we will warn the |
| 24 // user that extensions are slowing down the browser. | 45 // user that extensions are slowing down the browser. |
| 25 class ExtensionWebRequestTimeTracker { | 46 class ExtensionWebRequestTimeTracker { |
| 26 public: | 47 public: |
| 27 ExtensionWebRequestTimeTracker(); | 48 ExtensionWebRequestTimeTracker(); |
| 28 ~ExtensionWebRequestTimeTracker(); | 49 ~ExtensionWebRequestTimeTracker(); |
| 29 | 50 |
| 30 // Records the time that a request was created. | 51 // Records the time that a request was created. |
| 31 void LogRequestStartTime(int64 request_id, const base::Time& start_time, | 52 void LogRequestStartTime(int64 request_id, const base::Time& start_time, |
| 32 const GURL& url); | 53 const GURL& url, void* profile); |
| 33 | 54 |
| 34 // Records the time that a request either completed or encountered an error. | 55 // Records the time that a request either completed or encountered an error. |
| 35 void LogRequestEndTime(int64 request_id, const base::Time& end_time); | 56 void LogRequestEndTime(int64 request_id, const base::Time& end_time); |
| 36 | 57 |
| 37 // Records an additional delay for the given request caused by the given | 58 // Records an additional delay for the given request caused by the given |
| 38 // extension. | 59 // extension. |
| 39 void IncrementExtensionBlockTime( | 60 void IncrementExtensionBlockTime( |
| 40 const std::string& extension_id, | 61 const std::string& extension_id, |
| 41 int64 request_id, | 62 int64 request_id, |
| 42 const base::TimeDelta& block_time); | 63 const base::TimeDelta& block_time); |
| 43 | 64 |
| 44 // Records an additional delay for the given request caused by all extensions | 65 // Records an additional delay for the given request caused by all extensions |
| 45 // combined. | 66 // combined. |
| 46 void IncrementTotalBlockTime( | 67 void IncrementTotalBlockTime( |
| 47 int64 request_id, | 68 int64 request_id, |
| 48 const base::TimeDelta& block_time); | 69 const base::TimeDelta& block_time); |
| 49 | 70 |
| 50 // Called when an extension has canceled the given request. | 71 // Called when an extension has canceled the given request. |
| 51 void SetRequestCanceled(int64 request_id); | 72 void SetRequestCanceled(int64 request_id); |
| 52 | 73 |
| 53 // Called when an extension has redirected the given request to another URL. | 74 // Called when an extension has redirected the given request to another URL. |
| 54 void SetRequestRedirected(int64 request_id); | 75 void SetRequestRedirected(int64 request_id); |
| 55 | 76 |
| 77 void SetDelegate(ExtensionWebRequestTimeTrackerDelegate* delegate); | |
| 78 | |
| 56 private: | 79 private: |
| 57 // Timing information for a single request. | 80 // Timing information for a single request. |
| 58 struct RequestTimeLog { | 81 struct RequestTimeLog { |
| 59 GURL url; // used for debug purposes only | 82 GURL url; // used for debug purposes only |
| 83 void* profile; // profile that created the request | |
| 60 bool completed; | 84 bool completed; |
| 61 base::Time request_start_time; | 85 base::Time request_start_time; |
| 62 base::TimeDelta request_duration; | 86 base::TimeDelta request_duration; |
| 63 base::TimeDelta block_duration; | 87 base::TimeDelta block_duration; |
| 64 std::map<std::string, base::TimeDelta> extension_block_durations; | 88 std::map<std::string, base::TimeDelta> extension_block_durations; |
| 65 RequestTimeLog(); | 89 RequestTimeLog(); |
| 66 ~RequestTimeLog(); | 90 ~RequestTimeLog(); |
| 67 }; | 91 }; |
| 68 | 92 |
| 69 // Called after a request finishes, to analyze the delays and warn the user | 93 // Called after a request finishes, to analyze the delays and warn the user |
| 70 // if necessary. | 94 // if necessary. |
| 71 void Analyze(int64 request_id); | 95 void Analyze(int64 request_id); |
| 72 | 96 |
| 97 // Returns a list of all extension IDs that contributed to delay for |log|. | |
| 98 std::set<std::string> GetExtensionIds(const RequestTimeLog& log) const; | |
| 99 | |
| 73 // A map of recent request IDs to timing info for each request. | 100 // A map of recent request IDs to timing info for each request. |
| 74 std::map<int64, RequestTimeLog> request_time_logs_; | 101 std::map<int64, RequestTimeLog> request_time_logs_; |
| 75 | 102 |
| 76 // A list of recent request IDs that we know about. Used to limit the size of | 103 // A list of recent request IDs that we know about. Used to limit the size of |
| 77 // the logs. | 104 // the logs. |
| 78 std::queue<int64> request_ids_; | 105 std::queue<int64> request_ids_; |
| 79 | 106 |
| 80 // The set of recent requests that have been delayed either a large or | 107 // The set of recent requests that have been delayed either a large or |
| 81 // moderate amount by extensions. | 108 // moderate amount by extensions. |
| 82 std::set<int64> excessive_delays_; | 109 std::set<int64> excessive_delays_; |
| 83 std::set<int64> moderate_delays_; | 110 std::set<int64> moderate_delays_; |
| 84 | 111 |
| 112 // Weak pointer. Must outlive the ExtensionWebRequestTimeTracker. | |
| 113 ExtensionWebRequestTimeTrackerDelegate* delegate_; | |
| 114 | |
| 85 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Basic); | 115 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Basic); |
| 86 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, | 116 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, |
| 87 IgnoreFastRequests); | 117 IgnoreFastRequests); |
| 88 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, | 118 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, |
| 89 CancelOrRedirect); | 119 CancelOrRedirect); |
| 90 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Delays); | 120 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Delays); |
| 91 | 121 |
| 92 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker); | 122 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker); |
| 93 }; | 123 }; |
| 94 | 124 |
| 95 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ | 125 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ |
| OLD | NEW |