summaryrefslogtreecommitdiffstats
path: root/chrome/browser/renderer_host/resource_queue.cc
blob: dca6bd2b7a289672a353646716db2209aa879004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// 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() {
  DCHECK(shutdown_);
}

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();
    }
  }
}