blob: b1141415c8c625d10c52b437da21b9db8b700419 [file] [log] [blame]
[email protected]70049d92011-08-23 07:02:531// 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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_
7#pragma once
8
9#include <map>
10#include <queue>
11#include <set>
12#include <string>
13
[email protected]70049d92011-08-23 07:02:5314#include "base/gtest_prod_util.h"
[email protected]7c1490da2011-10-11 18:53:2515#include "base/memory/scoped_ptr.h"
16#include "base/time.h"
[email protected]70049d92011-08-23 07:02:5317#include "googleurl/src/gurl.h"
18
19namespace base {
20class Time;
21}
22
[email protected]7c1490da2011-10-11 18:53:2523class 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
[email protected]70049d92011-08-23 07:02:5343// This class keeps monitors how much delay extensions add to network requests
44// by using the webRequest API. If the delay is sufficient, we will warn the
45// user that extensions are slowing down the browser.
46class ExtensionWebRequestTimeTracker {
47 public:
48 ExtensionWebRequestTimeTracker();
49 ~ExtensionWebRequestTimeTracker();
50
51 // Records the time that a request was created.
52 void LogRequestStartTime(int64 request_id, const base::Time& start_time,
[email protected]7c1490da2011-10-11 18:53:2553 const GURL& url, void* profile);
[email protected]70049d92011-08-23 07:02:5354
55 // Records the time that a request either completed or encountered an error.
56 void LogRequestEndTime(int64 request_id, const base::Time& end_time);
57
58 // Records an additional delay for the given request caused by the given
59 // extension.
60 void IncrementExtensionBlockTime(
61 const std::string& extension_id,
62 int64 request_id,
63 const base::TimeDelta& block_time);
64
65 // Records an additional delay for the given request caused by all extensions
66 // combined.
67 void IncrementTotalBlockTime(
68 int64 request_id,
69 const base::TimeDelta& block_time);
70
71 // Called when an extension has canceled the given request.
72 void SetRequestCanceled(int64 request_id);
73
74 // Called when an extension has redirected the given request to another URL.
75 void SetRequestRedirected(int64 request_id);
76
[email protected]7c1490da2011-10-11 18:53:2577 // Takes ownership of |delegate|.
78 void SetDelegate(ExtensionWebRequestTimeTrackerDelegate* delegate);
79
[email protected]70049d92011-08-23 07:02:5380 private:
81 // Timing information for a single request.
82 struct RequestTimeLog {
83 GURL url; // used for debug purposes only
[email protected]7c1490da2011-10-11 18:53:2584 void* profile; // profile that created the request
[email protected]70049d92011-08-23 07:02:5385 bool completed;
86 base::Time request_start_time;
87 base::TimeDelta request_duration;
88 base::TimeDelta block_duration;
89 std::map<std::string, base::TimeDelta> extension_block_durations;
90 RequestTimeLog();
91 ~RequestTimeLog();
92 };
93
94 // Called after a request finishes, to analyze the delays and warn the user
95 // if necessary.
96 void Analyze(int64 request_id);
97
[email protected]7c1490da2011-10-11 18:53:2598 // Returns a list of all extension IDs that contributed to delay for |log|.
99 std::set<std::string> GetExtensionIds(const RequestTimeLog& log) const;
100
[email protected]70049d92011-08-23 07:02:53101 // A map of recent request IDs to timing info for each request.
102 std::map<int64, RequestTimeLog> request_time_logs_;
103
104 // A list of recent request IDs that we know about. Used to limit the size of
105 // the logs.
106 std::queue<int64> request_ids_;
107
108 // The set of recent requests that have been delayed either a large or
109 // moderate amount by extensions.
110 std::set<int64> excessive_delays_;
111 std::set<int64> moderate_delays_;
112
[email protected]7c1490da2011-10-11 18:53:25113 // Defaults to a delegate that sets warnings in the extension service.
114 scoped_ptr<ExtensionWebRequestTimeTrackerDelegate> delegate_;
115
[email protected]70049d92011-08-23 07:02:53116 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Basic);
117 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest,
118 IgnoreFastRequests);
119 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest,
120 CancelOrRedirect);
121 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTimeTrackerTest, Delays);
122
123 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestTimeTracker);
124};
125
126#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_TIME_TRACKER_H_