File: pdf_iframe_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 (175 lines) | stat: -rw-r--r-- 6,433 bytes parent folder | download | duplicates (5)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// 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/plugins/pdf_iframe_navigation_throttle.h"

#include <string>

#include "base/feature_list.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/escape.h"
#include "base/task/sequenced_task_runner.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/pdf_util.h"
#include "components/pdf/common/constants.h"
#include "components/pdf/common/pdf_util.h"
#include "content/public/browser/download_utils.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "net/http/http_response_headers.h"

#if BUILDFLAG(ENABLE_PLUGINS)
#include "chrome/browser/plugins/chrome_plugin_service_filter.h"
#include "content/public/browser/plugin_service.h"
#include "content/public/common/webplugininfo.h"
#endif

namespace {

// Used to scope the posted navigation task to the lifetime of |web_contents|.
class PdfWebContentsLifetimeHelper
    : public content::WebContentsUserData<PdfWebContentsLifetimeHelper> {
 public:
  explicit PdfWebContentsLifetimeHelper(content::WebContents* web_contents)
      : content::WebContentsUserData<PdfWebContentsLifetimeHelper>(
            *web_contents) {}

  base::WeakPtr<PdfWebContentsLifetimeHelper> GetWeakPtr() {
    return weak_factory_.GetWeakPtr();
  }

  void NavigateIFrameToPlaceholder(const content::OpenURLParams& url_params) {
    GetWebContents().OpenURL(url_params, /*navigation_handle_callback=*/{});
  }

 private:
  friend class content::WebContentsUserData<PdfWebContentsLifetimeHelper>;

  base::WeakPtrFactory<PdfWebContentsLifetimeHelper> weak_factory_{this};

  WEB_CONTENTS_USER_DATA_KEY_DECL();
};

WEB_CONTENTS_USER_DATA_KEY_IMPL(PdfWebContentsLifetimeHelper);

#if BUILDFLAG(ENABLE_PLUGINS)
// Returns true if the PDF plugin for |navigation_handle| is enabled. Optionally
// also sets |is_stale| to true if the plugin list needs a reload.
bool IsPDFPluginEnabled(content::NavigationHandle* navigation_handle,
                        bool* is_stale) {
  content::WebPluginInfo plugin_info;
  return content::PluginService::GetInstance()->GetPluginInfo(
      navigation_handle->GetWebContents()->GetBrowserContext(),
      navigation_handle->GetURL(), pdf::kPDFMimeType,
      false /* allow_wildcard */, is_stale, &plugin_info,
      nullptr /* actual_mime_type */);
}
#endif

}  // namespace

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

PDFIFrameNavigationThrottle::~PDFIFrameNavigationThrottle() = default;

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

// static
void PDFIFrameNavigationThrottle::MaybeCreateAndAdd(
    content::NavigationThrottleRegistry& registry) {
  if (registry.GetNavigationHandle().IsInMainFrame())
    return;

  registry.AddThrottle(std::make_unique<PDFIFrameNavigationThrottle>(registry));
}

content::NavigationThrottle::ThrottleCheckResult
PDFIFrameNavigationThrottle::WillProcessResponse() {
  const net::HttpResponseHeaders* response_headers =
      navigation_handle()->GetResponseHeaders();
  if (!response_headers)
    return content::NavigationThrottle::PROCEED;

  std::string mime_type;
  response_headers->GetMimeType(&mime_type);
  if (mime_type != pdf::kPDFMimeType) {
    return content::NavigationThrottle::PROCEED;
  }

  // We MUST download responses marked as attachments rather than showing
  // a placeholder.
  if (content::download_utils::MustDownload(
          navigation_handle()->GetWebContents()->GetBrowserContext(),
          navigation_handle()->GetURL(), response_headers, mime_type)) {
    return content::NavigationThrottle::PROCEED;
  }

#if BUILDFLAG(ENABLE_PLUGINS)
  bool is_stale = false;
  bool pdf_plugin_enabled = IsPDFPluginEnabled(navigation_handle(), &is_stale);

  if (is_stale) {
    // On browser start, the plugin list may not be ready yet.
    content::PluginService::GetInstance()->GetPlugins(
        base::BindOnce(&PDFIFrameNavigationThrottle::OnPluginsLoaded,
                       weak_factory_.GetWeakPtr()));
    return content::NavigationThrottle::DEFER;
  }

  // If the plugin was found, proceed on the navigation. Otherwise fall through
  // to the placeholder case.
  if (pdf_plugin_enabled)
    return content::NavigationThrottle::PROCEED;
#endif

  LoadPlaceholderHTML();
  return content::NavigationThrottle::CANCEL_AND_IGNORE;
}

#if BUILDFLAG(ENABLE_PLUGINS)
void PDFIFrameNavigationThrottle::OnPluginsLoaded(
    const std::vector<content::WebPluginInfo>& plugins) {
  if (IsPDFPluginEnabled(navigation_handle(), nullptr /* is_stale */)) {
    Resume();
  } else {
    LoadPlaceholderHTML();
    CancelDeferredNavigation(content::NavigationThrottle::CANCEL_AND_IGNORE);
  }
}
#endif

void PDFIFrameNavigationThrottle::LoadPlaceholderHTML() {
  // Prepare the params to navigate to the placeholder.
  std::string html = GetPDFPlaceholderHTML(navigation_handle()->GetURL());
  GURL data_url("data:text/html," + base::EscapePath(html));
  content::OpenURLParams params =
      content::OpenURLParams::FromNavigationHandle(navigation_handle());
  params.url = data_url;
  params.transition = ui::PAGE_TRANSITION_AUTO_SUBFRAME;

  // Post a task to navigate to the placeholder HTML. We don't navigate
  // synchronously here, as starting a navigation within a navigation is
  // an antipattern. Use a helper object scoped to the WebContents lifetime to
  // scope the navigation task to the WebContents lifetime.
  content::WebContents* web_contents = navigation_handle()->GetWebContents();
  if (!web_contents)
    return;

  ReportPDFLoadStatus(PDFLoadStatus::kLoadedIframePdfWithNoPdfViewer);

  PdfWebContentsLifetimeHelper::CreateForWebContents(web_contents);
  PdfWebContentsLifetimeHelper* helper =
      PdfWebContentsLifetimeHelper::FromWebContents(web_contents);
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(&PdfWebContentsLifetimeHelper::NavigateIFrameToPlaceholder,
                     helper->GetWeakPtr(), std::move(params)));
}