File: intercept_oma_download_navigation_throttle.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (123 lines) | stat: -rw-r--r-- 4,858 bytes parent folder | download | duplicates (3)
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
118
119
120
121
122
123
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/download/android/intercept_oma_download_navigation_throttle.h"

#include "base/functional/bind.h"
#include "base/memory/ptr_util.h"
#include "base/strings/string_util.h"
#include "chrome/browser/download/android/download_controller_base.h"
#include "components/embedder_support/user_agent_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/mojom/loader/referrer.mojom.h"

using content::BrowserThread;

// static
void InterceptOMADownloadNavigationThrottle::CreateAndAdd(
    content::NavigationThrottleRegistry& registry) {
  registry.AddThrottle(
      base::WrapUnique(new InterceptOMADownloadNavigationThrottle(registry)));
}

InterceptOMADownloadNavigationThrottle::
    ~InterceptOMADownloadNavigationThrottle() = default;

content::NavigationThrottle::ThrottleCheckResult
InterceptOMADownloadNavigationThrottle::WillProcessResponse() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (!navigation_handle()->IsDownload()) {
    return content::NavigationThrottle::PROCEED;
  }

  if (!navigation_handle()->GetURL().SchemeIsHTTPOrHTTPS()) {
    return content::NavigationThrottle::PROCEED;
  }

  if (navigation_handle()->IsPost()) {
    return content::NavigationThrottle::PROCEED;
  }

  const net::HttpResponseHeaders* headers =
      navigation_handle()->GetResponseHeaders();
  if (!headers) {
    return content::NavigationThrottle::PROCEED;
  }

  std::string mime_type;
  if (!headers->GetMimeType(&mime_type)) {
    return content::NavigationThrottle::PROCEED;
  }

  if (!base::EqualsCaseInsensitiveASCII(mime_type, kOMADrmMessageMimeType) &&
      !base::EqualsCaseInsensitiveASCII(mime_type, kOMADrmContentMimeType) &&
      !base::EqualsCaseInsensitiveASCII(mime_type, kOMADrmRightsMimeType1) &&
      !base::EqualsCaseInsensitiveASCII(mime_type, kOMADrmRightsMimeType2)) {
    return content::NavigationThrottle::PROCEED;
  }

  // Make the throttle prerender-aware. Prerender must not have any observable
  // side effects so avoid calling InterceptDownload. The prerender should also
  // be cancelled in this case but that requires a reference to the
  // RenderFrameHost which isn't easily available for a download. Instead,
  // proceed in this throttle; it will be cancelled from
  // PrerenderNavigationThrottle eventually.
  // TODO(robertlin): Find a way to port PrerenderHost status checks to Java,
  // and add a test on Android to verify the cancellation of an OMA download.
  content::RenderFrameHost* parent_render_frame_host =
      navigation_handle()->GetParentFrame();
  if ((parent_render_frame_host &&
       content::RenderFrameHost::LifecycleState::kPrerendering ==
           parent_render_frame_host->GetLifecycleState()) ||
      navigation_handle()->IsInPrerenderedMainFrame()) {
    return content::NavigationThrottle::PROCEED;
  }

  InterceptDownload();
  return content::NavigationThrottle::CANCEL;
}

const char* InterceptOMADownloadNavigationThrottle::GetNameForLogging() {
  return "InterceptDownloadNavigationThrottle";
}

InterceptOMADownloadNavigationThrottle::InterceptOMADownloadNavigationThrottle(
    content::NavigationThrottleRegistry& registry)
    : content::NavigationThrottle(registry) {}

void InterceptOMADownloadNavigationThrottle::InterceptDownload() {
  GURL original_url;
  const std::vector<GURL>& url_chain = navigation_handle()->GetRedirectChain();
  if (!url_chain.empty()) {
    original_url = url_chain.front();
  }

  std::string mime_type;
  const net::HttpResponseHeaders* headers =
      navigation_handle()->GetResponseHeaders();
  headers->GetMimeType(&mime_type);
  std::string content_disposition =
      headers->GetNormalizedHeader("content-disposition")
          .value_or(std::string());
  content::WebContents* web_contents = navigation_handle()->GetWebContents();
  int process_id =
      web_contents
          ? web_contents->GetRenderViewHost()->GetProcess()->GetDeprecatedID()
          : 0;
  int routing_id =
      web_contents ? web_contents->GetRenderViewHost()->GetRoutingID() : 0;

  DownloadControllerBase::Get()->CreateAndroidDownload(
      base::BindRepeating(&GetWebContents, process_id, routing_id),
      DownloadInfo(navigation_handle()->GetURL(), original_url,
                   content_disposition, mime_type,
                   embedder_support::GetUserAgent(),
                   // TODO(qinmin): Get the cookie from cookie store.
                   std::string(), navigation_handle()->GetReferrer().url));
}