Chromium Code Reviews| Index: content/browser/loader/content_size_resource_handler.h |
| diff --git a/content/browser/loader/content_size_resource_handler.h b/content/browser/loader/content_size_resource_handler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dcbe0cc12ba88d1019821c4e409d771baf2b1814 |
| --- /dev/null |
| +++ b/content/browser/loader/content_size_resource_handler.h |
| @@ -0,0 +1,91 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_BROWSER_LOADER_CONTENT_SIZE_RESOURCE_HANDLER_H_ |
| +#define CONTENT_BROWSER_LOADER_CONTENT_SIZE_RESOURCE_HANDLER_H_ |
| + |
| +#include <map> |
| + |
| +#include "base/macros.h" |
| +#include "content/browser/loader/global_routing_id.h" |
| +#include "content/browser/loader/layered_resource_handler.h" |
| +#include "net/url_request/url_request_status.h" |
| +#include "url/gurl.h" |
| + |
| +namespace net { |
| +class URLRequest; |
| +} |
| + |
| +namespace content { |
| + |
| +class ContentSizeResourceHandler; |
| +class ResourceRequestInfoImpl; |
| + |
| +// This class manages all in flight ContentSizeResourceHandlers. It keeps an IO |
| +// thread map of GlobalFrameRoutingId -> bytes left, and keeps it as up to date |
| +// as possible. This map is not exact, as it does not take into account child |
| +// frames using up data from parent frames. To make the data accounting exact, |
| +// the class periodically posts to the UI thread all of the current frame |
| +// updates, and the UI thread posts back all of the frames that should be |
| +// blocked. |
| +class ContentSizeResourceHandlerManager { |
|
mmenke
2016/08/05 17:16:27
I'm not a fan of having two top-level classes in a
Charlie Harrison
2016/08/08 14:04:38
SGTM moved to a separate file.
|
| + public: |
| + ContentSizeResourceHandlerManager(); |
| + ~ContentSizeResourceHandlerManager(); |
| + |
| + std::unique_ptr<ResourceHandler> MaybeAddHandler( |
| + std::unique_ptr<ResourceHandler> next_handler, |
| + net::URLRequest* request); |
| + |
| + // Post a task to the UI thread of all the byte accounting updates so far. |
| + // This is used for an asynchronous exact measurement of the policy. |
| + void NotifyUIThreadReadUpdates(); |
| + void NotifyRead(ResourceRequestInfoImpl* request_info, int bytes_read); |
| + |
| + // These methods are posted from the UI thread. |
| + void FrameHasSizeLimit(const GlobalFrameRoutingId& id, int limit); |
| + void FramesWentOverSizeLimits( |
| + std::unique_ptr<std::set<GlobalFrameRoutingId>> ids); |
| + void FrameDeleted(const GlobalFrameRoutingId& id); |
| + |
| + private: |
| + // As frames update their data accounting, use this map to keep track of bytes |
| + // downloaded, which gets sent to the UI thread to sync the source of truth |
| + // data. |
| + std::unique_ptr<std::map<GlobalFrameRoutingId, int>> pending_updates_; |
| + |
| + // Approximate data left per frame routing id. Resources for child frames do |
| + // not update parent frames here, so this is an upper bound. This is a simple |
| + // way to make easy case more accurate. If the UI thread notifies this class |
| + // of a blocked id, it gets set to 0 here. |
| + std::map<GlobalFrameRoutingId, int> frame_limits_; |
| +}; |
| + |
| +class ContentSizeResourceHandler : public LayeredResourceHandler { |
| + public: |
| + ContentSizeResourceHandler( |
| + std::unique_ptr<ResourceHandler> next_handler, |
| + net::URLRequest* request, |
| + ContentSizeResourceHandlerManager* manager, |
| + std::map<GlobalFrameRoutingId, int>::iterator frame_limit_iterator); |
| + ~ContentSizeResourceHandler() override; |
| + |
| + private: |
| + // ResourceHandler: |
| + bool OnWillStart(const GURL&, bool* defer) override; |
| + bool OnReadCompleted(int bytes_read, bool* defer) override; |
| + void OnResponseCompleted(const net::URLRequestStatus& status, |
| + const std::string& security_info, |
| + bool* defer) override; |
| + |
| + ContentSizeResourceHandlerManager* manager_; |
| + |
| + std::map<GlobalFrameRoutingId, int>::iterator frame_limit_iterator_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ContentSizeResourceHandler); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_BROWSER_LOADER_CONTENT_SIZE_RESOURCE_HANDLER_H_ |