Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 #include "content/browser/loader/content_size_resource_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "content/browser/loader/loader_io_thread_notifier.h" | |
| 9 #include "content/browser/loader/resource_request_info_impl.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 #include "net/url_request/url_request.h" | |
| 12 | |
| 13 namespace content { | |
| 14 | |
| 15 ContentSizeResourceHandlerManager::ContentSizeResourceHandlerManager() | |
| 16 : pending_updates_(new std::map<GlobalFrameRoutingId, int>) {} | |
| 17 ContentSizeResourceHandlerManager::~ContentSizeResourceHandlerManager() {} | |
| 18 | |
| 19 std::unique_ptr<ResourceHandler> | |
| 20 ContentSizeResourceHandlerManager::MaybeAddHandler( | |
| 21 std::unique_ptr<ResourceHandler> next_handler, | |
| 22 net::URLRequest* request) { | |
| 23 ResourceRequestInfoImpl* info = ResourceRequestInfoImpl::ForRequest(request); | |
| 24 DCHECK(info); | |
| 25 if (info->IsMainFrame()) | |
| 26 return next_handler; | |
| 27 | |
| 28 const auto& it = frame_limits_.find(info->GetGlobalFrameRoutingId()); | |
| 29 if (it != frame_limits_.end()) { | |
| 30 next_handler.reset(new ContentSizeResourceHandler(std::move(next_handler), | |
| 31 request, this, it)); | |
| 32 } | |
| 33 return next_handler; | |
| 34 } | |
| 35 | |
| 36 void ContentSizeResourceHandlerManager::NotifyRead( | |
| 37 ResourceRequestInfoImpl* request_info, | |
| 38 int bytes_read) { | |
| 39 (*pending_updates_)[request_info->GetGlobalFrameRoutingId()] += bytes_read; | |
| 40 } | |
| 41 | |
| 42 void ContentSizeResourceHandlerManager::NotifyUIThreadReadUpdates() { | |
| 43 BrowserThread::PostTask( | |
| 44 BrowserThread::UI, FROM_HERE, | |
| 45 base::Bind(&LoaderIOThreadNotifier::UpdateGlobalDataAccounting, | |
| 46 base::Passed(std::move(pending_updates_)))); | |
| 47 pending_updates_.reset(new std::map<GlobalFrameRoutingId, int>); | |
| 48 } | |
| 49 | |
| 50 void ContentSizeResourceHandlerManager::FrameHasSizeLimit( | |
| 51 const GlobalFrameRoutingId& id, int limit) { | |
| 52 frame_limits_[id] = limit; | |
|
mmenke
2016/08/02 18:02:11
How useful is this? This seems best-effort-y, giv
Charlie Harrison
2016/08/02 18:35:36
It's pretty useful. I don't exact numbers but I co
| |
| 53 } | |
| 54 | |
| 55 void ContentSizeResourceHandlerManager::FramesWentOverSizeLimits( | |
| 56 std::unique_ptr<std::set<GlobalFrameRoutingId>> ids) { | |
| 57 for (const auto& it : *ids) { | |
| 58 frame_limits_[it] = 0; | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 void ContentSizeResourceHandlerManager::FrameDeleted( | |
| 63 const GlobalFrameRoutingId& id) { | |
| 64 frame_limits_.erase(id); | |
| 65 } | |
| 66 | |
| 67 ContentSizeResourceHandler::ContentSizeResourceHandler( | |
| 68 std::unique_ptr<ResourceHandler> next_handler, | |
| 69 net::URLRequest* request, | |
| 70 ContentSizeResourceHandlerManager* manager, | |
| 71 std::map<GlobalFrameRoutingId, int>::iterator frame_limit_iterator) | |
| 72 : LayeredResourceHandler(request, std::move(next_handler)), | |
| 73 manager_(manager), | |
| 74 frame_limit_iterator_(frame_limit_iterator) {} | |
| 75 | |
| 76 ContentSizeResourceHandler::~ContentSizeResourceHandler() {} | |
| 77 | |
| 78 bool ContentSizeResourceHandler::OnWillStart(const GURL& url, bool* defer) { | |
| 79 if (frame_limit_iterator_->second <= 0) | |
| 80 return false; | |
| 81 return next_handler_->OnWillStart(url, defer); | |
| 82 } | |
| 83 | |
| 84 bool ContentSizeResourceHandler::OnReadCompleted(int bytes_read, bool* defer) { | |
| 85 frame_limit_iterator_->second -= bytes_read; | |
| 86 manager_->NotifyRead(GetRequestInfo(), bytes_read); | |
| 87 if (frame_limit_iterator_->second <= 0) | |
| 88 return false; | |
| 89 return next_handler_->OnReadCompleted(bytes_read, defer); | |
| 90 } | |
| 91 | |
| 92 void ContentSizeResourceHandler::OnResponseCompleted( | |
| 93 const net::URLRequestStatus& status, | |
| 94 const std::string& security_info, | |
| 95 bool* defer) { | |
| 96 manager_->NotifyUIThreadReadUpdates(); | |
| 97 next_handler_->OnResponseCompleted(status, security_info, defer); | |
| 98 } | |
| 99 | |
| 100 } // namespace content | |
| OLD | NEW |