[go: nahoru, domu]

Prevent CrashReporterURLObserver to force web state realization

This CL updates the code to prevent CrashReporterURLObserver from
inadvertently for the realization of a WebState.

(cherry picked from commit f97c97d6913f32d7ac34850f89a1846fa43791dd)

Fixed: 326471657
Change-Id: I4a9ad88fccc97ef2efaf3a049a131c95bbcb5516
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5324366
Auto-Submit: Louis Romero <lpromero@google.com>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: Sylvain Defresne <sdefresne@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1265843}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5362677
Owners-Override: Krishna Govind <govind@chromium.org>
Reviewed-by: Krishna Govind <govind@chromium.org>
Commit-Queue: Krishna Govind <govind@chromium.org>
Reviewed-by: Justin Cohen <justincohen@chromium.org>
Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com>
Commit-Queue: Justin Cohen <justincohen@chromium.org>
Cr-Commit-Position: refs/branch-heads/6312@{#524}
Cr-Branched-From: 6711dcdae48edaf98cbc6964f90fac85b7d9986e-refs/heads/main@{#1262506}
diff --git a/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.h b/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.h
index c6969c8..f21a2e74 100644
--- a/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.h
+++ b/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.h
@@ -47,7 +47,7 @@
   // Records the given URL associated to the given id to the list of URLs to
   // send to the crash server. If `pending` is true, the URL is one that is
   // expected to start loading, but hasn't actually been seen yet.
-  void RecordURL(const GURL& url, web::WebState* web_state, bool pending);
+  void RecordURL(const GURL& url, const web::WebState* web_state, bool pending);
 
   // Observes `webState` by this instance of the CrashReporterURLObserver.
   void ObservePreloadWebState(web::WebState* web_state);
@@ -74,17 +74,17 @@
   // A unique string identifying `web_state_list` as a group of WebStates.
   std::string GroupForWebStateList(WebStateList* web_state_list);
   // Convenient method to report the URL displayed by `web_state`.
-  void RecordURLForWebState(web::WebState* web_state);
+  void RecordURLForWebState(const web::WebState* web_state);
   // Remove the reporting of URLs of every WebStates in `group`.
   void RemoveGroup(const std::string& group);
   // Map associating each crash key with the group it is currently reporting
   // URLs for.
   NSMutableDictionary<NSString*, NSNumber*>* crash_key_by_group_;
   // Map associating each WebState to its group.
-  std::map<web::WebState*, std::string> web_state_to_group_;
+  std::map<const web::WebState*, std::string> web_state_to_group_;
   // Map associating each group to the WebState currently reported in crash
   // reports.
-  std::map<std::string, web::WebState*> current_web_states_;
+  std::map<std::string, const web::WebState*> current_web_states_;
   // List of keys to use for recording URLs. This list is sorted such that a new
   // tab must use the first key in this list to record its URLs.
   NSMutableArray<NSNumber*>* crash_keys_;
diff --git a/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.mm b/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.mm
index e9019b80..37052dd 100644
--- a/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.mm
+++ b/ios/chrome/browser/crash_report/model/crash_reporter_url_observer.mm
@@ -138,7 +138,7 @@
 #pragma mark - Record URLs
 
 void CrashReporterURLObserver::RecordURL(const GURL& url,
-                                         web::WebState* web_state,
+                                         const web::WebState* web_state,
                                          bool pending) {
   DCHECK(!web_state->GetBrowserState()->IsOffTheRecord());
   std::string group = web_state_to_group_[web_state];
@@ -175,9 +175,13 @@
   }
 }
 
-void CrashReporterURLObserver::RecordURLForWebState(web::WebState* web_state) {
-  web::NavigationItem* pending_item =
-      web_state->GetNavigationManager()->GetPendingItem();
+void CrashReporterURLObserver::RecordURLForWebState(
+    const web::WebState* web_state) {
+  // web_state is const, so GetNavigationManager won't force its realization
+  // (which is intended).
+  const web::NavigationManager* manager = web_state->GetNavigationManager();
+  const web::NavigationItem* pending_item =
+      manager ? manager->GetPendingItem() : nullptr;
   const GURL& url =
       pending_item ? pending_item->GetURL() : web_state->GetLastCommittedURL();
   RecordURL(url, web_state, pending_item != nullptr);