File: navigation_entry_remover.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 (242 lines) | stat: -rw-r--r-- 9,285 bytes parent folder | download | duplicates (6)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2018 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/browsing_data/navigation_entry_remover.h"

#include <functional>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sessions/tab_restore_service_factory.h"
#include "chrome/common/buildflags.h"
#include "components/sessions/core/serialized_navigation_entry.h"
#include "components/sessions/core/tab_restore_service.h"
#include "components/sessions/core/tab_restore_service_observer.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"

#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#else
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#endif

#if BUILDFLAG(ENABLE_SESSION_SERVICE)
#include "chrome/browser/sessions/session_service.h"
#include "chrome/browser/sessions/session_service_factory.h"
#endif  // BUILDFLAG(ENABLE_SESSION_SERVICE)

namespace {

bool ShouldDeleteUrl(base::Time begin,
                     base::Time end,
                     const std::optional<std::set<GURL>>& restrict_urls,
                     const GURL& url,
                     base::Time time_stamp) {
  return begin <= time_stamp && (time_stamp < end || end.is_null()) &&
         (!restrict_urls.has_value() ||
          restrict_urls->find(url) != restrict_urls->end());
}

bool ShouldDeleteNavigationEntry(
    base::Time begin,
    base::Time end,
    const std::optional<std::set<GURL>>& restrict_urls,
    content::NavigationEntry* entry) {
  return ShouldDeleteUrl(begin, end, restrict_urls, entry->GetURL(),
                         entry->GetTimestamp());
}

bool ShouldDeleteSerializedNavigationEntry(
    base::Time begin,
    base::Time end,
    const std::optional<std::set<GURL>>& restrict_urls,
    const sessions::SerializedNavigationEntry& entry) {
  return ShouldDeleteUrl(begin, end, restrict_urls, entry.virtual_url(),
                         entry.timestamp());
}

bool UrlMatcherForNavigationEntry(const base::flat_set<GURL>& urls,
                                  content::NavigationEntry* entry) {
  return urls.find(entry->GetURL()) != urls.end();
}

bool UrlMatcherForSerializedNavigationEntry(
    const base::flat_set<GURL>& urls,
    const sessions::SerializedNavigationEntry& entry) {
  return urls.find(entry.virtual_url()) != urls.end();
}

base::flat_set<GURL> CreateUrlSet(const history::URLRows& deleted_rows) {
  return base::MakeFlatSet<GURL>(deleted_rows, {}, &history::URLRow::url);
}

void DeleteNavigationEntries(
    content::WebContents* web_contents,
    const content::NavigationController::DeletionPredicate& predicate) {
  content::NavigationController* controller = &web_contents->GetController();
  controller->DiscardNonCommittedEntries();
  // We discarded pending and transient entries but there could still be
  // no last_committed_entry, which would prevent deletion.
  if (controller->CanPruneAllButLastCommitted())
    controller->DeleteNavigationEntries(predicate);
}

void DeleteTabNavigationEntries(
    Profile* profile,
    const history::DeletionTimeRange& time_range,
    const std::optional<std::set<GURL>>& restrict_urls,
    const base::flat_set<GURL>& url_set) {
  auto predicate = time_range.IsValid()
                       ? base::BindRepeating(
                             &ShouldDeleteNavigationEntry, time_range.begin(),
                             time_range.end(), std::cref(restrict_urls))
                       : base::BindRepeating(&UrlMatcherForNavigationEntry,
                                             std::cref(url_set));

#if BUILDFLAG(IS_ANDROID)
  auto session_predicate =
      time_range.IsValid()
          ? base::BindRepeating(&ShouldDeleteSerializedNavigationEntry,
                                time_range.begin(), time_range.end(),
                                std::cref(restrict_urls))
          : base::BindRepeating(&UrlMatcherForSerializedNavigationEntry,
                                std::cref(url_set));

  for (const TabModel* tab_model : TabModelList::models()) {
    if (tab_model->GetProfile() == profile) {
      for (int i = 0; i < tab_model->GetTabCount(); i++) {
        TabAndroid* tab = tab_model->GetTabAt(i);
        tab->DeleteFrozenNavigationEntries(session_predicate);
        content::WebContents* web_contents = tab->web_contents();
        if (web_contents)
          DeleteNavigationEntries(web_contents, predicate);
      }
    }
  }
#else
  for (Browser* browser : *BrowserList::GetInstance()) {
    TabStripModel* tab_strip = browser->tab_strip_model();
    if (browser->profile() == profile) {
      for (int i = 0; i < tab_strip->count(); i++)
        DeleteNavigationEntries(tab_strip->GetWebContentsAt(i), predicate);
    }
  }
#endif
}

void PerformTabRestoreDeletion(
    sessions::TabRestoreService* service,
    const sessions::TabRestoreService::DeletionPredicate& predicate) {
  service->DeleteNavigationEntries(predicate);
  service->DeleteLastSession();
}

// This class waits until TabRestoreService is loaded, then deletes
// navigation entries using |predicate| and deletes itself afterwards.
class TabRestoreDeletionHelper : public sessions::TabRestoreServiceObserver {
 public:
  TabRestoreDeletionHelper(
      sessions::TabRestoreService* service,
      const sessions::TabRestoreService::DeletionPredicate& predicate)
      : service_(service), deletion_predicate_(predicate) {
    DCHECK(!service->IsLoaded());
    service->AddObserver(this);
    service->LoadTabsFromLastSession();
  }

  TabRestoreDeletionHelper(const TabRestoreDeletionHelper&) = delete;
  TabRestoreDeletionHelper& operator=(const TabRestoreDeletionHelper&) = delete;

  // sessions::TabRestoreServiceObserver:
  void TabRestoreServiceDestroyed(
      sessions::TabRestoreService* service) override {
    delete this;
  }

  void TabRestoreServiceLoaded(sessions::TabRestoreService* service) override {
    PerformTabRestoreDeletion(service, deletion_predicate_);
    delete this;
  }

 private:
  ~TabRestoreDeletionHelper() override { service_->RemoveObserver(this); }

  raw_ptr<sessions::TabRestoreService> service_;
  sessions::TabRestoreService::DeletionPredicate deletion_predicate_;
};

void DeleteTabRestoreEntries(Profile* profile,
                             const history::DeletionTimeRange& time_range,
                             const std::optional<std::set<GURL>>& restrict_urls,
                             const base::flat_set<GURL>& url_set) {
  sessions::TabRestoreService* tab_service =
      TabRestoreServiceFactory::GetForProfile(profile);
  if (!tab_service)
    return;

  auto predicate =
      time_range.IsValid()
          ? base::BindRepeating(&ShouldDeleteSerializedNavigationEntry,
                                time_range.begin(), time_range.end(),
                                restrict_urls)
          : base::BindRepeating(&UrlMatcherForSerializedNavigationEntry,
                                url_set);
  if (tab_service->IsLoaded()) {
    PerformTabRestoreDeletion(tab_service, predicate);
  } else {
    // The helper deletes itself when the tab entry deletion is finished.
    new TabRestoreDeletionHelper(tab_service, predicate);
  }
}

void DeleteLastSessionFromSessionService(Profile* profile) {
#if BUILDFLAG(ENABLE_SESSION_SERVICE)
  SessionService* session_service =
      SessionServiceFactory::GetForProfile(profile);
  if (session_service)
    session_service->DeleteLastSession();
#endif
}

}  // namespace

namespace browsing_data {

void RemoveNavigationEntries(Profile* profile,
                             const history::DeletionInfo& deletion_info) {
  DCHECK(!profile->IsOffTheRecord());
  DCHECK(!deletion_info.is_from_expiration());

  base::flat_set<GURL> url_set;
  if (!deletion_info.time_range().IsValid())
    url_set = CreateUrlSet(deletion_info.deleted_rows());

  DeleteTabNavigationEntries(profile, deletion_info.time_range(),
                             deletion_info.restrict_urls(), url_set);
  DeleteTabRestoreEntries(profile, deletion_info.time_range(),
                          deletion_info.restrict_urls(), url_set);

  // Removal of navigation entries may occur at any point during runtime and
  // session service data is cleared so that it can be later rebuilt without the
  // deleted entries.
  // However deletion of foreign visits specifically can occur during startup
  // and clearing session service data will delete the user's previous session
  // with no ability to rebuild/recover (see crbug.com/1424800). Foreign visits
  // can't be part of the local session so there is no risk of retaining the
  // session service data in this case.
  if (deletion_info.deletion_reason() !=
      history::DeletionInfo::Reason::kDeleteAllForeignVisits) {
    DeleteLastSessionFromSessionService(profile);
  }
}

}  // namespace browsing_data