OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/extensions/user_script_listener.h" | 5 #include "chrome/browser/extensions/user_script_listener.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/metrics/histogram_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
10 #include "base/timer/elapsed_timer.h" | 10 #include "base/timer/elapsed_timer.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" | 13 #include "chrome/common/extensions/manifest_handlers/content_scripts_handler.h" |
14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
16 #include "content/public/browser/resource_controller.h" | |
17 #include "content/public/browser/resource_throttle.h" | 16 #include "content/public/browser/resource_throttle.h" |
18 #include "extensions/browser/extension_registry.h" | 17 #include "extensions/browser/extension_registry.h" |
19 #include "extensions/common/extension.h" | 18 #include "extensions/common/extension.h" |
20 #include "extensions/common/url_pattern.h" | 19 #include "extensions/common/url_pattern.h" |
21 #include "net/url_request/url_request.h" | 20 #include "net/url_request/url_request.h" |
22 | 21 |
23 using content::BrowserThread; | 22 using content::BrowserThread; |
24 using content::ResourceThrottle; | 23 using content::ResourceThrottle; |
25 using content::ResourceType; | 24 using content::ResourceType; |
26 | 25 |
27 namespace extensions { | 26 namespace extensions { |
28 | 27 |
29 class UserScriptListener::Throttle | 28 class UserScriptListener::Throttle |
30 : public ResourceThrottle, | 29 : public ResourceThrottle, |
31 public base::SupportsWeakPtr<UserScriptListener::Throttle> { | 30 public base::SupportsWeakPtr<UserScriptListener::Throttle> { |
32 public: | 31 public: |
33 Throttle() : should_defer_(true), did_defer_(false) { | 32 Throttle() : should_defer_(true), did_defer_(false) { |
34 } | 33 } |
35 | 34 |
36 void Resume() { | 35 void ResumeIfDeferred() { |
37 DCHECK(should_defer_); | 36 DCHECK(should_defer_); |
38 should_defer_ = false; | 37 should_defer_ = false; |
39 // Only resume the request if |this| has deferred it. | 38 // Only resume the request if |this| has deferred it. |
40 if (did_defer_) { | 39 if (did_defer_) { |
41 UMA_HISTOGRAM_TIMES("Extensions.ThrottledNetworkRequestDelay", | 40 UMA_HISTOGRAM_TIMES("Extensions.ThrottledNetworkRequestDelay", |
42 timer_->Elapsed()); | 41 timer_->Elapsed()); |
43 controller()->Resume(); | 42 Resume(); |
44 } | 43 } |
45 } | 44 } |
46 | 45 |
47 // ResourceThrottle implementation: | 46 // ResourceThrottle implementation: |
48 void WillStartRequest(bool* defer) override { | 47 void WillStartRequest(bool* defer) override { |
49 // Only defer requests if Resume has not yet been called. | 48 // Only defer requests if Resume has not yet been called. |
50 if (should_defer_) { | 49 if (should_defer_) { |
51 *defer = true; | 50 *defer = true; |
52 did_defer_ = true; | 51 did_defer_ = true; |
53 timer_.reset(new base::ElapsedTimer()); | 52 timer_.reset(new base::ElapsedTimer()); |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 | 138 |
140 return false; | 139 return false; |
141 } | 140 } |
142 | 141 |
143 void UserScriptListener::StartDelayedRequests() { | 142 void UserScriptListener::StartDelayedRequests() { |
144 UMA_HISTOGRAM_COUNTS_100("Extensions.ThrottledNetworkRequests", | 143 UMA_HISTOGRAM_COUNTS_100("Extensions.ThrottledNetworkRequests", |
145 throttles_.size()); | 144 throttles_.size()); |
146 WeakThrottleList::const_iterator it; | 145 WeakThrottleList::const_iterator it; |
147 for (it = throttles_.begin(); it != throttles_.end(); ++it) { | 146 for (it = throttles_.begin(); it != throttles_.end(); ++it) { |
148 if (it->get()) | 147 if (it->get()) |
149 (*it)->Resume(); | 148 (*it)->ResumeIfDeferred(); |
150 } | 149 } |
151 throttles_.clear(); | 150 throttles_.clear(); |
152 } | 151 } |
153 | 152 |
154 void UserScriptListener::CheckIfAllUserScriptsReady() { | 153 void UserScriptListener::CheckIfAllUserScriptsReady() { |
155 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 154 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
156 bool was_ready = user_scripts_ready_; | 155 bool was_ready = user_scripts_ready_; |
157 | 156 |
158 user_scripts_ready_ = true; | 157 user_scripts_ready_ = true; |
159 for (ProfileDataMap::const_iterator it = profile_data_.begin(); | 158 for (ProfileDataMap::const_iterator it = profile_data_.begin(); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 &UserScriptListener::ProfileDestroyed, this, profile)); | 270 &UserScriptListener::ProfileDestroyed, this, profile)); |
272 break; | 271 break; |
273 } | 272 } |
274 | 273 |
275 default: | 274 default: |
276 NOTREACHED(); | 275 NOTREACHED(); |
277 } | 276 } |
278 } | 277 } |
279 | 278 |
280 } // namespace extensions | 279 } // namespace extensions |
OLD | NEW |