File: pdf_infobar_controller.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 (215 lines) | stat: -rw-r--r-- 7,642 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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2025 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/ui/pdf/infobar/pdf_infobar_controller.h"

#include <optional>

#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/cstring_view.h"
#include "base/task/thread_pool.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/shell_integration.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/pdf/infobar/pdf_infobar_delegate.h"
#include "chrome/browser/ui/pdf/infobar/pdf_infobar_prefs.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/common/buildflags.h"
#include "chrome/common/pref_names.h"
#include "components/infobars/content/content_infobar_manager.h"
#include "components/pdf/common/constants.h"
#include "components/prefs/pref_service.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_WIN)
#include "chrome/install_static/install_util.h"
#endif  // BUILDFLAG(IS_WIN)

namespace pdf::infobar {
namespace {

// Returns true if `navigation_handle` is committed, not an error page, and
// contains a PDF.
bool IsLoadedPdf(content::NavigationHandle* navigation_handle) {
  const bool has_committed = navigation_handle->HasCommitted();
  const bool is_error_page = navigation_handle->IsErrorPage();
  auto* web_contents = navigation_handle->GetWebContents();
  const bool is_pdf =
      web_contents && web_contents->GetContentsMimeType() == pdf::kPDFMimeType;
  return has_committed && !is_error_page && is_pdf;
}

// Returns true if `browser` supports being set as default and is a normal,
// non-incognito, non-guest browser.
bool IsAppropriateForInfoBar(BrowserWindowInterface* browser) {
#if BUILDFLAG(IS_WIN)
  // On Windows, some install modes don't support being set as default.
  if (!install_static::SupportsSetAsDefaultBrowser()) {
    return false;
  }
#endif  // BUILDFLAG(IS_WIN)
  if (browser->GetType() != BrowserWindowInterface::TYPE_NORMAL) {
    return false;
  }
  const auto* profile = browser->GetProfile();
  if (profile->IsIncognitoProfile() || profile->IsGuestSession()) {
    return false;
  }
  return true;
}

}  // namespace

std::optional<bool> PdfInfoBarController::higher_priority_infobar_shown_;

PdfInfoBarController::PdfInfoBarController(BrowserWindowInterface* browser)
    : browser_(browser) {
  CHECK(base::FeatureList::IsEnabled(features::kPdfInfoBar));
  if (!IsAppropriateForInfoBar(browser_)) {
    return;
  }

  browser_subscriptions_.push_back(
      browser_->RegisterBrowserDidClose(base::BindRepeating(
          &PdfInfoBarController::OnBrowserClosed, base::Unretained(this))));

  // This is the entry point to the PDF infobar if it shows when a PDF loads.
  if (features::kPdfInfoBarTrigger.Get() ==
      features::PdfInfoBarTrigger::kPdfLoad) {
    // Register to find out when the active tab changes.
    browser_subscriptions_.push_back(browser_->RegisterActiveTabDidChange(
        base::BindRepeating(&PdfInfoBarController::OnActiveTabChanged,
                            base::Unretained(this))));
  }
}

PdfInfoBarController::~PdfInfoBarController() = default;

// static
void PdfInfoBarController::MaybeShowInfoBarAtStartup(
    base::WeakPtr<BrowserWindowInterface> startup_browser,
    bool higher_priority_infobar_shown) {
  higher_priority_infobar_shown_ = higher_priority_infobar_shown;
  if (!startup_browser) {
    return;
  }
  if (!IsAppropriateForInfoBar(startup_browser.get())) {
    return;
  }
  // This is the entry point to the PDF infobar if it shows at startup.
  if (features::kPdfInfoBarTrigger.Get() ==
      features::PdfInfoBarTrigger::kStartup) {
    startup_browser->GetFeatures().pdf_infobar_controller()->MaybeShowInfoBar();
  }
}

void PdfInfoBarController::OnActiveTabChanged(BrowserWindowInterface* browser) {
  // Observe web contents to see if it loads a PDF (see
  // `DidFinishNavigation()`).
  Observe(browser->GetActiveTabInterface()->GetContents());
}

void PdfInfoBarController::OnBrowserClosed(BrowserWindowInterface* browser) {
  if (infobar_) {
    infobar_manager_->RemoveInfoBar(infobar_);
  }
}

void PdfInfoBarController::DidFinishNavigation(
    content::NavigationHandle* navigation_handle) {
  CHECK(features::kPdfInfoBarTrigger.Get() ==
        features::PdfInfoBarTrigger::kPdfLoad);
  if (IsLoadedPdf(navigation_handle)) {
    MaybeShowInfoBar();
  }
}

void PdfInfoBarController::OnInfoBarRemoved(infobars::InfoBar* infobar,
                                            bool animate) {
  if (infobar_ != infobar) {
    return;
  }
  infobar_ = nullptr;
  infobar_manager_->RemoveObserver(this);
  infobar_manager_ = nullptr;
}

void PdfInfoBarController::MaybeShowInfoBar() {
#if BUILDFLAG(IS_MAC)
  auto is_default_pdf_viewer_callback = base::BindOnce(
      &shell_integration::IsDefaultHandlerForUTType, "com.adobe.pdf");
#elif BUILDFLAG(IS_WIN)
  auto is_default_pdf_viewer_callback = base::BindOnce(
      &shell_integration::IsDefaultHandlerForFileExtension, ".pdf");
#else
#error PdfInfoBarController should only be created on Windows or MacOS
#endif
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()}, std::move(is_default_pdf_viewer_callback),
      base::BindOnce(&PdfInfoBarController::MaybeShowInfoBarCallback,
                     weak_factory_.GetWeakPtr()));
}

void PdfInfoBarController::MaybeShowInfoBarCallback(
    shell_integration::DefaultWebClientState default_state) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(base::FeatureList::IsEnabled(features::kPdfInfoBar));

  // If Chrome is already the default PDF viewer, do nothing.
  if (default_state == shell_integration::DefaultWebClientState::IS_DEFAULT ||
      default_state ==
          shell_integration::DefaultWebClientState::OTHER_MODE_IS_DEFAULT) {
    return;
  }
  // Don't offer to be the default PDF viewer if Chrome is set not to view PDFs.
  if (IsPdfViewerDisabled(browser_->GetProfile())) {
    return;
  }
  // Don't offer to be the default PDF viewer if default apps are controlled by
  // a policy.
  if (IsDefaultBrowserPolicyControlled()) {
    return;
  }
  // Don't show the infobar if it's already showing or was recently shown.
  if (infobar_) {
    return;
  }
  if (InfoBarShownRecentlyOrMaxTimes()) {
    return;
  }
  // Don't show the infobar if a higher priority infobar has been shown or might
  // be about to show, to avoid asking too many similar questions in a session.
  if (!higher_priority_infobar_shown_.has_value() ||
      higher_priority_infobar_shown_.value()) {
    return;
  }
  content::WebContents* web_contents =
      browser_->GetTabStripModel()->GetActiveWebContents();
  if (!web_contents) {
    return;
  }

  // Show the PDF infobar.
  infobar_manager_ =
      infobars::ContentInfoBarManager::FromWebContents(web_contents);
  infobar_manager_->AddObserver(this);
  infobar_ = PdfInfoBarDelegate::Create(infobar_manager_);
  SetInfoBarShownRecently();
}

// static
void PdfInfoBarController::SetHigherPriorityInfoBarShownForTesting(
    bool higher_priority_infobar_shown) {
  higher_priority_infobar_shown_ = higher_priority_infobar_shown;
}

}  // namespace pdf::infobar