Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(287)

Side by Side Diff: chrome/browser/renderer_host/resource_queue.cc

Issue 460108: Implement ResourceQueue, an object that makes it easy to delay starting (Closed)
Patch Set: nitfixing Created 11 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 "chrome/browser/renderer_host/resource_queue.h"
6
7 #include "base/stl_util-inl.h"
8 #include "chrome/browser/chrome_thread.h"
9 #include "chrome/browser/renderer_host/global_request_id.h"
10 #include "chrome/browser/renderer_host/resource_dispatcher_host_request_info.h"
11
12 ResourceQueueDelegate::~ResourceQueueDelegate() {
13 }
14
15 ResourceQueue::ResourceQueue() : shutdown_(false) {
16 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
17 }
18
19 ResourceQueue::~ResourceQueue() {
20 // TODO(phajdan.jr): Add DCHECK(shutdown_) here when unit tests stop abusing
21 // ResourceDispatcherHost by not shutting it down in tests.
22 }
23
24 void ResourceQueue::Initialize(const DelegateSet& delegates) {
25 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::UI));
26 DCHECK(delegates_.empty());
27 delegates_ = delegates;
28 }
29
30 void ResourceQueue::Shutdown() {
31 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
32
33 shutdown_ = true;
34 for (DelegateSet::iterator i = delegates_.begin();
35 i != delegates_.end(); ++i) {
36 (*i)->WillShutdownResourceQueue();
37 }
38 }
39
40 void ResourceQueue::AddRequest(
41 URLRequest* request,
42 const ResourceDispatcherHostRequestInfo& request_info) {
43 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
44 DCHECK(!shutdown_);
45
46 GlobalRequestID request_id(request_info.child_id(),
47 request_info.request_id());
48
49 DCHECK(!ContainsKey(requests_, request_id));
50 requests_[request_id] = request;
51
52 DelegateSet interested_delegates;
53
54 for (DelegateSet::iterator i = delegates_.begin();
55 i != delegates_.end(); ++i) {
56 if ((*i)->ShouldDelayRequest(request, request_info, request_id))
57 interested_delegates.insert(*i);
58 }
59
60 if (interested_delegates.empty()) {
61 request->Start();
62 return;
63 }
64
65 DCHECK(!ContainsKey(interested_delegates_, request_id));
66 interested_delegates_[request_id] = interested_delegates;
67 }
68
69 void ResourceQueue::RemoveRequest(const GlobalRequestID& request_id) {
70 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
71 requests_.erase(request_id);
72 }
73
74 void ResourceQueue::StartDelayedRequest(ResourceQueueDelegate* delegate,
75 const GlobalRequestID& request_id) {
76 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO));
77 DCHECK(!shutdown_);
78
79 DCHECK(ContainsKey(interested_delegates_, request_id));
80 DCHECK(ContainsKey(interested_delegates_[request_id], delegate));
81 interested_delegates_[request_id].erase(delegate);
82 if (interested_delegates_[request_id].empty()) {
83 interested_delegates_.erase(request_id);
84
85 if (ContainsKey(requests_, request_id)) {
86 URLRequest* request = requests_[request_id];
87 // The request shouldn't have started (SUCCESS is the initial state).
88 DCHECK_EQ(URLRequestStatus::SUCCESS, request->status().status());
89 request->Start();
90 }
91 }
92 }
OLDNEW
« no previous file with comments | « chrome/browser/renderer_host/resource_queue.h ('k') | chrome/browser/renderer_host/resource_queue_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698