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.
5 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_TIME_TRACKER_H_
6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_TIME_TRACKER_H_
13 #include "base/gtest_prod_util.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/time/time.h"
22 class ExtensionWebRequestTimeTrackerDelegate
{
24 virtual ~ExtensionWebRequestTimeTrackerDelegate() {}
26 // Notifies the delegate that |num_delayed_messages| of the last
27 // |total_num_messages| inspected messages were excessively/moderately
28 // delayed. Every excessively delayed message is also counted as a moderately
30 virtual void NotifyExcessiveDelays(
32 size_t num_delayed_messages
,
33 size_t total_num_messages
,
34 const std::set
<std::string
>& extension_ids
) = 0;
35 virtual void NotifyModerateDelays(
37 size_t num_delayed_messages
,
38 size_t total_num_messages
,
39 const std::set
<std::string
>& extension_ids
) = 0;
42 // This class keeps monitors how much delay extensions add to network requests
43 // by using the webRequest API. If the delay is sufficient, we will warn the
44 // user that extensions are slowing down the browser.
45 class ExtensionWebRequestTimeTracker
{
47 ExtensionWebRequestTimeTracker();
48 ~ExtensionWebRequestTimeTracker();
50 // Records the time that a request was created.
51 void LogRequestStartTime(int64 request_id
, const base::Time
& start_time
,
52 const GURL
& url
, void* profile
);
54 // Records the time that a request either completed or encountered an error.
55 void LogRequestEndTime(int64 request_id
, const base::Time
& end_time
);
57 // Records an additional delay for the given request caused by the given
59 void IncrementExtensionBlockTime(
60 const std::string
& extension_id
,
62 const base::TimeDelta
& block_time
);
64 // Records an additional delay for the given request caused by all extensions
66 void IncrementTotalBlockTime(
68 const base::TimeDelta
& block_time
);
70 // Called when an extension has canceled the given request.
71 void SetRequestCanceled(int64 request_id
);
73 // Called when an extension has redirected the given request to another URL.
74 void SetRequestRedirected(int64 request_id
);
76 // Takes ownership of |delegate|.
77 void SetDelegate(ExtensionWebRequestTimeTrackerDelegate
* delegate
);
80 // Timing information for a single request.
81 struct RequestTimeLog
{
82 GURL url
; // used for debug purposes only
83 void* profile
; // profile that created the request
85 base::Time request_start_time
;
86 base::TimeDelta request_duration
;
87 base::TimeDelta block_duration
;
88 std::map
<std::string
, base::TimeDelta
> extension_block_durations
;
93 // Called after a request finishes, to analyze the delays and warn the user
95 void Analyze(int64 request_id
);
97 // Returns a list of all extension IDs that contributed to delay for |log|.
98 std::set
<std::string
> GetExtensionIds(const RequestTimeLog
& log
) const;
100 // A map of recent request IDs to timing info for each request.
101 std::map
<int64
, RequestTimeLog
> request_time_logs_
;
103 // A list of recent request IDs that we know about. Used to limit the size of
105 std::queue
<int64
> request_ids_
;
107 // The set of recent requests that have been delayed either a large or
108 // moderate amount by extensions.
109 std::set
<int64
> excessive_delays_
;
110 std::set
<int64
> moderate_delays_
;
112 // Defaults to a delegate that sets warnings in the extension service.
113 scoped_ptr
<ExtensionWebRequestTimeTrackerDelegate
> delegate_
;
115 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest
, Basic
);
116 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest
,
118 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest
,
120 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest
, Delays
);
122 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker
);
125 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_TIME_TRACKER_H_