1 // Copyright 2015 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 #include "extensions/browser/extension_request_limiting_throttle.h"
7 #include "base/logging.h"
8 #include "content/public/browser/resource_controller.h"
9 #include "extensions/browser/extension_throttle_entry.h"
10 #include "extensions/browser/extension_throttle_manager.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/redirect_info.h"
13 #include "net/url_request/url_request.h"
15 namespace extensions
{
17 ExtensionRequestLimitingThrottle::ExtensionRequestLimitingThrottle(
18 const net::URLRequest
* request
,
19 ExtensionThrottleManager
* manager
)
20 : request_(request
), manager_(manager
) {
24 ExtensionRequestLimitingThrottle::~ExtensionRequestLimitingThrottle() {
27 void ExtensionRequestLimitingThrottle::WillStartRequest(bool* defer
) {
28 throttling_entry_
= manager_
->RegisterRequestUrl(request_
->url());
29 if (throttling_entry_
->ShouldRejectRequest(*request_
))
30 controller()->CancelWithError(net::ERR_TEMPORARILY_THROTTLED
);
33 void ExtensionRequestLimitingThrottle::WillRedirectRequest(
34 const net::RedirectInfo
& redirect_info
,
36 DCHECK_EQ(manager_
->GetIdFromUrl(request_
->url()),
37 throttling_entry_
->GetURLIdForDebugging());
39 throttling_entry_
->UpdateWithResponse(redirect_info
.status_code
);
41 throttling_entry_
= manager_
->RegisterRequestUrl(redirect_info
.new_url
);
42 if (throttling_entry_
->ShouldRejectRequest(*request_
))
43 controller()->CancelWithError(net::ERR_TEMPORARILY_THROTTLED
);
46 void ExtensionRequestLimitingThrottle::WillProcessResponse(bool* defer
) {
47 DCHECK_EQ(manager_
->GetIdFromUrl(request_
->url()),
48 throttling_entry_
->GetURLIdForDebugging());
50 if (!request_
->was_cached())
51 throttling_entry_
->UpdateWithResponse(request_
->GetResponseCode());
54 const char* ExtensionRequestLimitingThrottle::GetNameForLogging() const {
55 return "ExtensionRequestLimitingThrottle";
58 } // namespace extensions