OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/browser_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(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
17 } | |
18 | |
19 ResourceQueue::~ResourceQueue() { | |
20 DCHECK(shutdown_); | |
21 } | |
22 | |
23 void ResourceQueue::Initialize(const DelegateSet& delegates) { | |
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
25 DCHECK(delegates_.empty()); | |
26 delegates_ = delegates; | |
27 } | |
28 | |
29 void ResourceQueue::Shutdown() { | |
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
31 | |
32 shutdown_ = true; | |
33 for (DelegateSet::iterator i = delegates_.begin(); | |
34 i != delegates_.end(); ++i) { | |
35 (*i)->WillShutdownResourceQueue(); | |
36 } | |
37 } | |
38 | |
39 void ResourceQueue::AddRequest( | |
40 net::URLRequest* request, | |
41 const ResourceDispatcherHostRequestInfo& request_info) { | |
42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
43 DCHECK(!shutdown_); | |
44 | |
45 GlobalRequestID request_id(request_info.child_id(), | |
46 request_info.request_id()); | |
47 | |
48 DCHECK(!ContainsKey(requests_, request_id)) | |
49 << "child_id:" << request_info.child_id() | |
50 << ", request_id:" << request_info.request_id(); | |
51 requests_[request_id] = request; | |
52 | |
53 DelegateSet interested_delegates; | |
54 | |
55 for (DelegateSet::iterator i = delegates_.begin(); | |
56 i != delegates_.end(); ++i) { | |
57 if ((*i)->ShouldDelayRequest(request, request_info, request_id)) | |
58 interested_delegates.insert(*i); | |
59 } | |
60 | |
61 if (interested_delegates.empty()) { | |
62 request->Start(); | |
63 return; | |
64 } | |
65 | |
66 DCHECK(!ContainsKey(interested_delegates_, request_id)); | |
67 interested_delegates_[request_id] = interested_delegates; | |
68 } | |
69 | |
70 void ResourceQueue::RemoveRequest(const GlobalRequestID& request_id) { | |
71 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
72 requests_.erase(request_id); | |
73 } | |
74 | |
75 void ResourceQueue::StartDelayedRequest(ResourceQueueDelegate* delegate, | |
76 const GlobalRequestID& request_id) { | |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
78 DCHECK(!shutdown_); | |
79 | |
80 DCHECK(ContainsKey(interested_delegates_, request_id)); | |
81 DCHECK(ContainsKey(interested_delegates_[request_id], delegate)); | |
82 interested_delegates_[request_id].erase(delegate); | |
83 if (interested_delegates_[request_id].empty()) { | |
84 interested_delegates_.erase(request_id); | |
85 | |
86 if (ContainsKey(requests_, request_id)) { | |
87 net::URLRequest* request = requests_[request_id]; | |
88 // The request shouldn't have started (SUCCESS is the initial state). | |
89 DCHECK_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); | |
90 request->Start(); | |
91 } | |
92 } | |
93 } | |
OLD | NEW |