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
|
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_CONTROLLER_H_
#define CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_CONTROLLER_H_
#include <memory>
#include "base/callback_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/enterprise/data_protection/data_protection_navigation_observer.h"
#include "components/enterprise/buildflags/buildflags.h"
#include "content/public/browser/web_contents_observer.h"
namespace tabs {
class TabInterface;
}
namespace enterprise_data_protection {
struct UrlSettings;
// Observes navigations in order to correctly set that tab's Data Protection
// settings based on the SafeBrowsing verdict for said navigation.
// This class is unconditionally created, but will do nothing if data protection
// is disabled.
class DataProtectionNavigationController
: public DataProtectionNavigationDelegate,
public content::WebContentsObserver {
public:
explicit DataProtectionNavigationController(
tabs::TabInterface* tab_interface);
~DataProtectionNavigationController() override;
// Callback is invoked by ApplyDataProtectionSettingsOrDelayIfEmpty.
void SetCallbackForTesting(base::OnceClosure closure);
private:
// TabInterface subscriber. Called when the associated tab enters the
// foreground.
void TabForegrounded(tabs::TabInterface* tab);
// TabInterface subscriber. Called when the associated tab is going to be
// discarded.
void WillDiscardContents(tabs::TabInterface* tab,
content::WebContents* old_contents,
content::WebContents* new_contents);
// Applies data protection settings if there are any to apply, otherwise
// delay clearing the data protection settings until the page loads.
//
// This is called from a finish navigation event to handle the case where the
// browser view is switching from a tab with data protections enabled to one
// without. At the end of the navigation, the existing page is still visible
// to the user since the UI has not yet refreshed. In this case the
// protections should remain in place. Once the document finishes loading,
// `ApplyDataProtectionSettings()` will be called. The observer passes
// `is_same_document` to this callback because, since there is no document
// onload event for that case, the original document is preserved, and
// the watermark is therefore cleared when the navigation finishes. See
// `DocumentOnLoadCompletedInPrimaryMainFrame()`.
void ApplyDataProtectionSettingsOrDelayIfEmpty(
base::WeakPtr<content::WebContents> expected_web_contents,
bool is_same_document,
const enterprise_data_protection::UrlSettings& settings);
// Applies data protection settings based on the verdict received by
// safe-browsing's realtime to `watermark_view_`.
void ApplyDataProtectionSettings(
base::WeakPtr<content::WebContents> expected_web_contents,
const enterprise_data_protection::UrlSettings& settings);
// content::WebContentsObserver
void DidStartNavigation(
content::NavigationHandle* navigation_handle) override;
void DocumentOnLoadCompletedInPrimaryMainFrame() override;
// DataProtectionNavigationDelegate
void Cleanup(int64_t navigation_id) override;
// Clear data protections once the page loads.
// TODO(b/330960313): These bools can be removed once FCP is used as the
// signal to set the data protections for the current tab.
bool clear_watermark_text_on_page_load_ = false;
#if BUILDFLAG(ENTERPRISE_SCREENSHOT_PROTECTION)
bool clear_screenshot_protection_on_page_load_ = false;
#endif
// Holds subscriptions for TabInterface callbacks.
std::vector<base::CallbackListSubscription> tab_subscriptions_;
// Maps navigation IDs to navigation observers. We take ownership of said
// navigation observers here because, with added support for
// same-document navigations, some verdicts arrive after the navigation
// finishes, and we need the navigation observer to persist after this
// happens.
DataProtectionNavigationObserver::NavigationObservers navigation_observers_;
raw_ptr<tabs::TabInterface> tab_interface_;
base::OnceClosure
on_delay_apply_data_protection_settings_if_empty_called_for_testing_;
mutable base::WeakPtrFactory<DataProtectionNavigationController>
weak_ptr_factory_{this};
};
} // namespace enterprise_data_protection
#endif // CHROME_BROWSER_ENTERPRISE_DATA_PROTECTION_DATA_PROTECTION_NAVIGATION_CONTROLLER_H_
|