diff options
author | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 20:04:35 +0000 |
---|---|---|
committer | phajdan.jr@chromium.org <phajdan.jr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-12-10 20:04:35 +0000 |
commit | 92b24c11f42b31158cca43921eac16f33c98839c (patch) | |
tree | 81304558304a29fb589327af857db90294f17fc8 /chrome/browser/renderer_host/resource_queue.cc | |
parent | 8eadc12f4690e648e11b5f84733da84f2f8825a8 (diff) | |
download | chromium_src-92b24c11f42b31158cca43921eac16f33c98839c.zip chromium_src-92b24c11f42b31158cca43921eac16f33c98839c.tar.gz chromium_src-92b24c11f42b31158cca43921eac16f33c98839c.tar.bz2 |
Implement ResourceQueue, an object that makes it easy to delay starting
requests in ResourceDispatcherHost until specified conditions are met.
Make UserScriptListener use ResourceQueue.
This is the first step toward waiting for the privacy blacklists to load.
TEST=Covered by unit_tests.
BUG=21541
Review URL: http://codereview.chromium.org/460108
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@34271 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/renderer_host/resource_queue.cc')
-rw-r--r-- | chrome/browser/renderer_host/resource_queue.cc | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/chrome/browser/renderer_host/resource_queue.cc b/chrome/browser/renderer_host/resource_queue.cc new file mode 100644 index 0000000..409f6b9 --- /dev/null +++ b/chrome/browser/renderer_host/resource_queue.cc @@ -0,0 +1,92 @@ +// Copyright (c) 2009 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. + +#include "chrome/browser/renderer_host/resource_queue.h" + +#include "base/stl_util-inl.h" +#include "chrome/browser/chrome_thread.h" +#include "chrome/browser/renderer_host/global_request_id.h" +#include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h" + +ResourceQueueDelegate::~ResourceQueueDelegate() { +} + +ResourceQueue::ResourceQueue() : shutdown_(false) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); +} + +ResourceQueue::~ResourceQueue() { + // TODO(phajdan.jr): Add DCHECK(shutdown_) here when unit tests stop abusing + // ResourceDispatcherHost by not shutting it down in tests. +} + +void ResourceQueue::Initialize(const DelegateSet& delegates) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI)); + DCHECK(delegates_.empty()); + delegates_ = delegates; +} + +void ResourceQueue::Shutdown() { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + + shutdown_ = true; + for (DelegateSet::iterator i = delegates_.begin(); + i != delegates_.end(); ++i) { + (*i)->WillShutdownResourceQueue(); + } +} + +void ResourceQueue::AddRequest( + URLRequest* request, + const ResourceDispatcherHostRequestInfo& request_info) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + DCHECK(!shutdown_); + + GlobalRequestID request_id(request_info.child_id(), + request_info.request_id()); + + DCHECK(!ContainsKey(requests_, request_id)); + requests_[request_id] = request; + + DelegateSet interested_delegates; + + for (DelegateSet::iterator i = delegates_.begin(); + i != delegates_.end(); ++i) { + if ((*i)->ShouldDelayRequest(request, request_info, request_id)) + interested_delegates.insert(*i); + } + + if (interested_delegates.empty()) { + request->Start(); + return; + } + + DCHECK(!ContainsKey(interested_delegates_, request_id)); + interested_delegates_[request_id] = interested_delegates; +} + +void ResourceQueue::RemoveRequest(const GlobalRequestID& request_id) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + requests_.erase(request_id); +} + +void ResourceQueue::StartDelayedRequest(ResourceQueueDelegate* delegate, + const GlobalRequestID& request_id) { + DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); + DCHECK(!shutdown_); + + DCHECK(ContainsKey(interested_delegates_, request_id)); + DCHECK(ContainsKey(interested_delegates_[request_id], delegate)); + interested_delegates_[request_id].erase(delegate); + if (interested_delegates_[request_id].empty()) { + interested_delegates_.erase(request_id); + + if (ContainsKey(requests_, request_id)) { + URLRequest* request = requests_[request_id]; + // The request shouldn't have started (SUCCESS is the initial state). + DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status()); + request->Start(); + } + } +} |