[lens-overlay] Fix crash when side panel Webui opened in tab

Technically, WebUIs can be opened in a tab, and therefore it is not safe
to assume the only entrance to the side panel WebUI is
RegisterEntryAndShow. This CL removes that assumption and ensures there
are no other crashes when interacting with the UI.

Fixed: 417119042
Change-Id: Ic0cca345f7e086da1c21063290bb473aab90ead6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6542069
Commit-Queue: Duncan Mercer <mercerd@google.com>
Reviewed-by: Juan Mojica <juanmojica@google.com>
Cr-Commit-Position: refs/heads/main@{#1459692}
diff --git a/chrome/browser/ui/lens/lens_overlay_controller.cc b/chrome/browser/ui/lens/lens_overlay_controller.cc
index 62badf9..451d2c6 100644
--- a/chrome/browser/ui/lens/lens_overlay_controller.cc
+++ b/chrome/browser/ui/lens/lens_overlay_controller.cc
@@ -1117,6 +1117,9 @@
 }
 
 void LensOverlayController::ClearTextSelection() {
+  if(!IsOverlayShowing()) {
+    return;
+  }
   if (initialization_data_->selected_text_.has_value()) {
     initialization_data_->selected_text_.reset();
     page_->ClearTextSelection();
@@ -1124,6 +1127,9 @@
 }
 
 void LensOverlayController::ClearRegionSelection() {
+  if(!IsOverlayShowing()) {
+    return;
+  }
   GetLensSearchboxController()->SetSearchboxThumbnail("");
   lens_selection_type_ = lens::UNKNOWN_SELECTION_TYPE;
   initialization_data_->selected_region_.reset();
diff --git a/chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.cc b/chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.cc
index 67033e34..b9078ee 100644
--- a/chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.cc
+++ b/chrome/browser/ui/lens/lens_overlay_side_panel_coordinator.cc
@@ -511,7 +511,13 @@
 void LensOverlaySidePanelCoordinator::BindSidePanel(
     mojo::PendingReceiver<lens::mojom::LensSidePanelPageHandler> receiver,
     mojo::PendingRemote<lens::mojom::LensSidePanelPage> page) {
-  CHECK(state_ == State::kOpeningSidePanel);
+  // Ideally, this should be a CHECK, but if the user just navigates to the
+  // WebUI link directly, then this will be called without
+  // RegisterEntryAndShow() being called. If that is the case, ignore this call.
+  // More info at crbug.com/417119042.
+  if (state_ != State::kOpeningSidePanel) {
+    return;
+  }
 
   side_panel_receiver_.Bind(std::move(receiver));
   side_panel_page_.Bind(std::move(page));
diff --git a/chrome/browser/ui/lens/lens_searchbox_controller.cc b/chrome/browser/ui/lens/lens_searchbox_controller.cc
index b59c6ee27..d07f7d56 100644
--- a/chrome/browser/ui/lens/lens_searchbox_controller.cc
+++ b/chrome/browser/ui/lens/lens_searchbox_controller.cc
@@ -134,14 +134,10 @@
 }
 
 void LensSearchboxController::OnTextModified() {
-  // TOOD(crbug.com/404941800): Verify this doesn't break if the overlay is
-  // off.
   lens_search_controller_->lens_overlay_controller()->ClearTextSelection();
 }
 
 void LensSearchboxController::OnThumbnailRemoved() {
-  // TOOD(crbug.com/404941800): Verify this doesn't break if the overlay is
-  // off.
   lens_search_controller_->lens_overlay_controller()->ClearRegionSelection();
 }