| 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 | 13 |
| 14 #include "base/gtest_prod_util.h" |
| 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/time.h" | 16 #include "base/time.h" |
| 15 #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 { |
| 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 // Takes ownership of |delegate|. |
| 78 void SetDelegate(ExtensionWebRequestTimeTrackerDelegate* delegate); |
| 79 |
| 56 private: | 80 private: |
| 57 // Timing information for a single request. | 81 // Timing information for a single request. |
| 58 struct RequestTimeLog { | 82 struct RequestTimeLog { |
| 59 GURL url; // used for debug purposes only | 83 GURL url; // used for debug purposes only |
| 84 void* profile; // profile that created the request |
| 60 bool completed; | 85 bool completed; |
| 61 base::Time request_start_time; | 86 base::Time request_start_time; |
| 62 base::TimeDelta request_duration; | 87 base::TimeDelta request_duration; |
| 63 base::TimeDelta block_duration; | 88 base::TimeDelta block_duration; |
| 64 std::map<std::string, base::TimeDelta> extension_block_durations; | 89 std::map<std::string, base::TimeDelta> extension_block_durations; |
| 65 RequestTimeLog(); | 90 RequestTimeLog(); |
| 66 ~RequestTimeLog(); | 91 ~RequestTimeLog(); |
| 67 }; | 92 }; |
| 68 | 93 |
| 69 // Called after a request finishes, to analyze the delays and warn the user | 94 // Called after a request finishes, to analyze the delays and warn the user |
| 70 // if necessary. | 95 // if necessary. |
| 71 void Analyze(int64 request_id); | 96 void Analyze(int64 request_id); |
| 72 | 97 |
| 98 // Returns a list of all extension IDs that contributed to delay for |log|. |
| 99 std::set<std::string> GetExtensionIds(const RequestTimeLog& log) const; |
| 100 |
| 73 // A map of recent request IDs to timing info for each request. | 101 // A map of recent request IDs to timing info for each request. |
| 74 std::map<int64, RequestTimeLog> request_time_logs_; | 102 std::map<int64, RequestTimeLog> request_time_logs_; |
| 75 | 103 |
| 76 // A list of recent request IDs that we know about. Used to limit the size of | 104 // A list of recent request IDs that we know about. Used to limit the size of |
| 77 // the logs. | 105 // the logs. |
| 78 std::queue<int64> request_ids_; | 106 std::queue<int64> request_ids_; |
| 79 | 107 |
| 80 // The set of recent requests that have been delayed either a large or | 108 // The set of recent requests that have been delayed either a large or |
| 81 // moderate amount by extensions. | 109 // moderate amount by extensions. |
| 82 std::set<int64> excessive_delays_; | 110 std::set<int64> excessive_delays_; |
| 83 std::set<int64> moderate_delays_; | 111 std::set<int64> moderate_delays_; |
| 84 | 112 |
| 113 // Defaults to a delegate that sets warnings in the extension service. |
| 114 scoped_ptr<ExtensionWebRequestTimeTrackerDelegate> delegate_; |
| 115 |
| 85 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Basic); | 116 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Basic); |
| 86 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, | 117 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, |
| 87 IgnoreFastRequests); | 118 IgnoreFastRequests); |
| 88 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, | 119 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, |
| 89 CancelOrRedirect); | 120 CancelOrRedirect); |
| 90 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Delays); | 121 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Delays); |
| 91 | 122 |
| 92 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker); | 123 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker); |
| 93 }; | 124 }; |
| 94 | 125 |
| 95 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ | 126 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_ |
| OLD | NEW |