summaryrefslogtreecommitdiffstats
path: root/content/browser/download/download_request_handle.cc
blob: e5d9b60d1cac3937d3252300518fe33f7ec4bf80 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2011 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 "content/browser/download/download_request_handle.h"

#include "base/stringprintf.h"
#include "content/browser/browser_context.h"
#include "content/browser/browser_thread.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/renderer_host/resource_dispatcher_host.h"
#include "content/browser/tab_contents/tab_contents.h"

// IO Thread indirections to resource dispatcher host.
// Provided as targets for PostTask from within this object
// only.
static void ResourceDispatcherHostPauseRequest(
    ResourceDispatcherHost* rdh,
    int process_unique_id,
    int request_id,
    bool pause) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  rdh->PauseRequest(process_unique_id, request_id, pause);
}

static void ResourceDispatcherHostCancelRequest(
    ResourceDispatcherHost* rdh,
    int process_unique_id,
    int request_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
  rdh->CancelRequest(process_unique_id, request_id, false);
}

DownloadRequestHandle::DownloadRequestHandle()
    : rdh_(NULL),
      child_id_(-1),
      render_view_id_(-1),
      request_id_(-1) {
}

DownloadRequestHandle::DownloadRequestHandle(ResourceDispatcherHost* rdh,
                                             int child_id,
                                             int render_view_id,
                                             int request_id)
    : rdh_(rdh),
      child_id_(child_id),
      render_view_id_(render_view_id),
      request_id_(request_id) {
  // ResourceDispatcherHost should not be null for non-default instances
  // of DownloadRequestHandle.
  DCHECK(rdh);
}

TabContents* DownloadRequestHandle::GetTabContents() const {
  RenderViewHost* render_view_host =
      RenderViewHost::FromID(child_id_, render_view_id_);
  if (!render_view_host)
    return NULL;

  return render_view_host->delegate()->GetAsTabContents();
}

DownloadManager* DownloadRequestHandle::GetDownloadManager() const {
  TabContents* contents = GetTabContents();
  if (!contents)
    return NULL;

  content::BrowserContext* browser_context = contents->browser_context();
  if (!browser_context)
    return NULL;

  return browser_context->GetDownloadManager();
}

void DownloadRequestHandle::PauseRequest() const {
  // The post is safe because ResourceDispatcherHost is guaranteed
  // to outlive the IO thread.
  if (rdh_) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        NewRunnableFunction(&ResourceDispatcherHostPauseRequest,
                            rdh_, child_id_, request_id_, true));
  }
}

void DownloadRequestHandle::ResumeRequest() const {
  // The post is safe because ResourceDispatcherHost is guaranteed
  // to outlive the IO thread.
  if (rdh_) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        NewRunnableFunction(&ResourceDispatcherHostPauseRequest,
                            rdh_, child_id_, request_id_, false));
  }
}

void DownloadRequestHandle::CancelRequest() const {
  // The post is safe because ResourceDispatcherHost is guaranteed
  // to outlive the IO thread.
  if (rdh_) {
    BrowserThread::PostTask(
        BrowserThread::IO, FROM_HERE,
        NewRunnableFunction(&ResourceDispatcherHostCancelRequest,
                            rdh_, child_id_, request_id_));
  }
}

std::string DownloadRequestHandle::DebugString() const {
  return base::StringPrintf("{"
                            " child_id = %d"
                            " render_view_id = %d"
                            " request_id = %d"
                            "}",
                            child_id_,
                            render_view_id_,
                            request_id_);
}