From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Shelley Vohr <shelley.vohr@gmail.com>
Date: Mon, 25 Oct 2021 21:45:57 +0200
Subject: fix: adapt exclusive_access for electron needs

This patch is necessary in order to properly enable
navigator.keyboard.{(un)?lock}() functionality. We don't have a concept
of PermissionManager nor of a Profile, so this would not affect usage of
the API.

We also need to ensure that NotifyExclusiveTabAccessLost is called
on all platforms in FullscreenController::ExitFullscreenModeInternal()
and not just macOS, since Electron's native window impls report state
change fairly instantly as well, and so pressing escape won't work on
Linux or Windows to un-fullscreen in some circumstances without this
change.

diff --git a/chrome/browser/ui/exclusive_access/fullscreen_controller.cc b/chrome/browser/ui/exclusive_access/fullscreen_controller.cc
index c38ce5558472c785837f1d4d2542f2edf2ffab58..219f562f51b45d2c961246e232ef4daf9d1f5c19 100644
--- a/chrome/browser/ui/exclusive_access/fullscreen_controller.cc
+++ b/chrome/browser/ui/exclusive_access/fullscreen_controller.cc
@@ -46,7 +46,7 @@
 #include "components/prefs/pref_service.h"
 #endif
 
-#if !BUILDFLAG(IS_ANDROID)
+#if 0
 #include "chrome/browser/ui/blocked_content/popunder_preventer.h"
 #endif  // !BUILDFLAG(IS_ANDROID)
 
@@ -271,7 +271,7 @@ void FullscreenController::EnterFullscreenModeForTab(
     return;
   }
 
-#if !BUILDFLAG(IS_ANDROID)
+#if 0
   if (!popunder_preventer_) {
     popunder_preventer_ = std::make_unique<PopunderPreventer>(web_contents);
   } else {
@@ -391,12 +391,14 @@ void FullscreenController::ExitFullscreenModeForTab(WebContents* web_contents) {
 void FullscreenController::FullscreenTabOpeningPopup(
     content::WebContents* opener,
     content::WebContents* popup) {
+#if 0
   if (!popunder_preventer_) {
     return;
   }
 
   DCHECK_EQ(exclusive_access_tab(), opener);
   popunder_preventer_->AddPotentialPopunder(popup);
+#endif
 }
 #endif  // !BUILDFLAG(IS_ANDROID)
 
@@ -487,8 +489,7 @@ void FullscreenController::FullscreenTransitionCompleted() {
 #endif  // DCHECK_IS_ON()
   tab_fullscreen_target_display_id_ = display::kInvalidDisplayId;
   started_fullscreen_transition_ = false;
-
-#if !BUILDFLAG(IS_ANDROID)
+#if 0
   if (!IsTabFullscreen()) {
     // Activate any popup windows created while content fullscreen, after exit.
     popunder_preventer_.reset();
@@ -623,18 +624,17 @@ void FullscreenController::EnterFullscreenModeInternal(
   // Do not enter fullscreen mode if disallowed by pref. This prevents the user
   // from manually entering fullscreen mode and also disables kiosk mode on
   // desktop platforms.
-  if (!exclusive_access_manager()
-           ->context()
-           ->GetProfile()
-           ->GetPrefs()
-           ->GetBoolean(prefs::kFullscreenAllowed)) {
+  auto* profile = exclusive_access_manager()->context()->GetProfile();
+  if (!profile || !profile->GetPrefs()->GetBoolean(prefs::kFullscreenAllowed))
     return;
-  }
 #endif
   started_fullscreen_transition_ = true;
   toggled_into_fullscreen_ = true;
+#if 0
   bool entering_tab_fullscreen = option == TAB && !tab_fullscreen_;
+#endif
   url::Origin origin;
+#if 0
   if (option == TAB) {
     origin = GetRequestingOrigin();
     tab_fullscreen_ = true;
@@ -671,6 +671,7 @@ void FullscreenController::EnterFullscreenModeInternal(
       origin = url::Origin::Create(extension_url_.value());
     }
   }
+#endif
 
   fullscreen_start_time_ = base::TimeTicks::Now();
   if (option == BROWSER) {
@@ -692,6 +693,7 @@ void FullscreenController::ExitFullscreenModeInternal() {
     return;
   }
 
+#if 0
   // `fullscreen_start_time_` is null if a fullscreen tab moves to a new window.
   if (fullscreen_start_time_ && exclusive_access_tab()) {
     ukm::SourceId source_id =
@@ -703,18 +705,19 @@ void FullscreenController::ExitFullscreenModeInternal() {
         .Record(ukm::UkmRecorder::Get());
     fullscreen_start_time_.reset();
   }
+#endif
 
   toggled_into_fullscreen_ = false;
   started_fullscreen_transition_ = true;
-#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID)
-  // Mac windows report a state change instantly, and so we must also clear
+
+  // Electron native windows report a state change instantly, and so we must also clear
   // state_prior_to_tab_fullscreen_ to match them else other logic using
   // state_prior_to_tab_fullscreen_ will be incorrect.
   // On Android the state of fullscreen is keep in the Java Fullscreen
   // Controller. The change is instant so we notify about access lost to
   // keep the state coherent.
   NotifyTabExclusiveAccessLost();
-#endif
+
   exclusive_access_manager()->context()->ExitFullscreen();
   extension_url_.reset();
   exclusive_access_manager()->UpdateBubble(base::NullCallback());
@@ -778,8 +781,12 @@ url::Origin FullscreenController::GetEmbeddingOrigin() const {
 void FullscreenController::RecordMetricsOnFullscreenApiRequested(
     content::RenderFrameHost* requesting_frame) {
   history::HistoryService* service =
+  #if 0
       HistoryServiceFactory::GetForProfileWithoutCreating(
           exclusive_access_manager()->context()->GetProfile());
+  #else
+      nullptr;
+  #endif
   if (service) {
     // Check if the origin has been visited more than a day ago and whether it's
     // on an allowlist, then record those bits of information in a metric.
diff --git a/chrome/browser/ui/exclusive_access/fullscreen_controller.h b/chrome/browser/ui/exclusive_access/fullscreen_controller.h
index 0fa707f6dfcfb26ed15488b099cc03efe548785b..0ef6faf1e2385b091df36b0c0efb89167b17ac34 100644
--- a/chrome/browser/ui/exclusive_access/fullscreen_controller.h
+++ b/chrome/browser/ui/exclusive_access/fullscreen_controller.h
@@ -254,7 +254,7 @@ class FullscreenController : public ExclusiveAccessControllerBase {
   // Used in testing to set the state to tab fullscreen.
   bool is_tab_fullscreen_for_testing_ = false;
 
-#if !BUILDFLAG(IS_ANDROID)
+#if 0
   // Tracks related popups that lost activation or were shown without activation
   // during content fullscreen sessions. This also activates the popups when
   // fullscreen exits, to prevent sites from creating persistent popunders.
