File: chrome_bookmark_client.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (268 lines) | stat: -rw-r--r-- 10,480 bytes parent folder | download | duplicates (2)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2014 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/bookmarks/chrome_bookmark_client.h"

#include "base/metrics/histogram_functions.h"
#include "base/metrics/user_metrics.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "chrome/browser/commerce/shopping_service_factory.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/bookmarks/browser/bookmark_model.h"
#include "components/bookmarks/browser/bookmark_node.h"
#include "components/bookmarks/browser/bookmark_storage.h"
#include "components/bookmarks/common/bookmark_pref_names.h"
#include "components/bookmarks/managed/managed_bookmark_service.h"
#include "components/bookmarks/managed/managed_bookmark_util.h"
#include "components/commerce/core/commerce_feature_list.h"
#include "components/commerce/core/price_tracking_utils.h"
#include "components/commerce/core/shopping_service.h"
#include "components/favicon/core/favicon_util.h"
#include "components/favicon_base/favicon_types.h"
#include "components/history/core/browser/history_service.h"
#include "components/history/core/browser/url_database.h"
#include "components/offline_pages/buildflags/buildflags.h"
#include "components/power_bookmarks/core/suggested_save_location_provider.h"
#include "components/signin/public/base/persistent_repeating_timer.h"
#include "components/sync/base/features.h"
#include "components/sync_bookmarks/bookmark_model_view.h"
#include "components/sync_bookmarks/bookmark_sync_service.h"
#include "components/undo/bookmark_undo_service.h"

#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
#include "chrome/browser/offline_pages/offline_page_bookmark_observer.h"
#endif

#if !BUILDFLAG(IS_ANDROID)
#include "chrome/browser/bookmarks/bookmark_merged_surface_service.h"
#include "chrome/browser/bookmarks/bookmark_merged_surface_service_factory.h"
#endif  // !BUILDFLAG(IS_ANDROID)

namespace {

class ShoppingCollectionProvider
    : public power_bookmarks::SuggestedSaveLocationProvider {
 public:
  ShoppingCollectionProvider(bookmarks::BookmarkModel* model, Profile* profile)
      : model_(model), profile_(profile) {}
  ShoppingCollectionProvider(const ShoppingCollectionProvider&) = delete;
  ShoppingCollectionProvider& operator=(const ShoppingCollectionProvider&) =
      delete;
  ~ShoppingCollectionProvider() override = default;

  const bookmarks::BookmarkNode* GetSuggestion(const GURL& url) override {
    commerce::ShoppingService* service =
        commerce::ShoppingServiceFactory::GetForBrowserContext(profile_);
    if (!service || !service->GetAvailableProductInfoForUrl(url).has_value()) {
      return nullptr;
    }
    return commerce::GetShoppingCollectionBookmarkFolder(model_.get(), true);
  }

  base::TimeDelta GetBackoffTime() override {
    // TODO(b:291326480): Make this configurable.
    return base::Hours(2);
  }

  std::string GetFeatureNameForMetrics() override {
    return "ShoppingCollection";
  }

  void OnSuggestionRejected() override {
    base::RecordAction(base::UserMetricsAction(
        "Commerce.PriceTracking.ShoppingCollection.RejectedSuggestion"));
  }

 private:
  raw_ptr<bookmarks::BookmarkModel> model_;
  raw_ptr<Profile> profile_;
};

}  // namespace

ChromeBookmarkClient::ChromeBookmarkClient(
    Profile* profile,
    bookmarks::ManagedBookmarkService* managed_bookmark_service,
    sync_bookmarks::BookmarkSyncService*
        local_or_syncable_bookmark_sync_service,
    sync_bookmarks::BookmarkSyncService* account_bookmark_sync_service,
    BookmarkUndoService* bookmark_undo_service)
    : profile_(profile),
      managed_bookmark_service_(managed_bookmark_service),
      local_or_syncable_bookmark_sync_service_(
          local_or_syncable_bookmark_sync_service),
      account_bookmark_sync_service_(account_bookmark_sync_service),
      bookmark_undo_service_(bookmark_undo_service) {
  CHECK(profile_);
  base::UmaHistogramBoolean(
      "Bookmarks.BookmarkBar.Shown",
      profile_->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar));
}

ChromeBookmarkClient::~ChromeBookmarkClient() {
  if (shopping_save_location_provider_) {
    RemoveSuggestedSaveLocationProvider(shopping_save_location_provider_.get());
  }
}

void ChromeBookmarkClient::Init(bookmarks::BookmarkModel* model) {
  BookmarkClientBase::Init(model);
  if (managed_bookmark_service_)
    managed_bookmark_service_->BookmarkModelCreated(model);
  model_ = model;

  shopping_save_location_provider_ =
      std::make_unique<ShoppingCollectionProvider>(model, profile_);

  AddSuggestedSaveLocationProvider(shopping_save_location_provider_.get());

#if BUILDFLAG(ENABLE_OFFLINE_PAGES)
  offline_page_observer_ =
      std::make_unique<offline_pages::OfflinePageBookmarkObserver>(profile_);
  model_observation_ = std::make_unique<base::ScopedObservation<
      bookmarks::BookmarkModel, bookmarks::BaseBookmarkModelObserver>>(
      offline_page_observer_.get());
  model_observation_->Observe(model);
#endif

#if !BUILDFLAG(IS_ANDROID)
  // Ensure `BookmarkMergedSurfaceService` is created all the time when
  // `BookmarkModel` is created and before the `BookmarkModel` completes loading
  // to catch if `ids_reassigned`. Posting a task is required as
  // `BookmarkMergedSurfaceServiceFactory` will invoke the factory for the
  // `BookmarkModel`.
  base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
      FROM_HERE,
      base::BindOnce(
          [](base::WeakPtr<Profile> profile) {
            if (profile) {
              BookmarkMergedSurfaceServiceFactory::GetForProfile(profile.get());
            }
          },
          profile_->GetWeakPtr()));
#endif  // !BUILDFLAG(IS_ANDROID)
}

base::CancelableTaskTracker::TaskId
ChromeBookmarkClient::GetFaviconImageForPageURL(
    const GURL& page_url,
    favicon_base::FaviconImageCallback callback,
    base::CancelableTaskTracker* tracker) {
  return favicon::GetFaviconImageForPageURL(
      FaviconServiceFactory::GetForProfile(profile_,
                                           ServiceAccessType::EXPLICIT_ACCESS),
      page_url, favicon_base::IconType::kFavicon, std::move(callback), tracker);
}

bool ChromeBookmarkClient::SupportsTypedCountForUrls() {
  return true;
}

void ChromeBookmarkClient::GetTypedCountForUrls(
    UrlTypedCountMap* url_typed_count_map) {
  history::HistoryService* history_service =
      HistoryServiceFactory::GetForProfileIfExists(
          profile_, ServiceAccessType::EXPLICIT_ACCESS);
  history::URLDatabase* url_db =
      history_service ? history_service->InMemoryDatabase() : nullptr;
  if (!url_db)
    return;

  for (auto& url_typed_count_pair : *url_typed_count_map) {
    // The in-memory URLDatabase might not cache all URLRows, but it
    // guarantees to contain those with `typed_count` > 0. Thus, if we cannot
    // fetch the URLRow, it is safe to assume that its `typed_count` is 0.
    history::URLRow url_row;
    const GURL* url = url_typed_count_pair.first;
    if (url && url_db->GetRowForURL(*url, &url_row))
      url_typed_count_pair.second = url_row.typed_count();
  }
}

bookmarks::LoadManagedNodeCallback
ChromeBookmarkClient::GetLoadManagedNodeCallback() {
  if (!managed_bookmark_service_)
    return bookmarks::LoadManagedNodeCallback();

  return managed_bookmark_service_->GetLoadManagedNodeCallback();
}

bool ChromeBookmarkClient::IsSyncFeatureEnabledIncludingBookmarks() {
  return local_or_syncable_bookmark_sync_service_->IsTrackingMetadata();
}

bool ChromeBookmarkClient::CanSetPermanentNodeTitle(
    const bookmarks::BookmarkNode* permanent_node) {
  return !managed_bookmark_service_ ||
         managed_bookmark_service_->CanSetPermanentNodeTitle(permanent_node);
}

bool ChromeBookmarkClient::IsNodeManaged(const bookmarks::BookmarkNode* node) {
  return managed_bookmark_service_ &&
         managed_bookmark_service_->IsNodeManaged(node);
}

std::string ChromeBookmarkClient::EncodeLocalOrSyncableBookmarkSyncMetadata() {
  return local_or_syncable_bookmark_sync_service_->EncodeBookmarkSyncMetadata();
}

std::string ChromeBookmarkClient::EncodeAccountBookmarkSyncMetadata() {
  if (!account_bookmark_sync_service_) {
    return std::string();
  }

  return account_bookmark_sync_service_->EncodeBookmarkSyncMetadata();
}

void ChromeBookmarkClient::DecodeLocalOrSyncableBookmarkSyncMetadata(
    const std::string& metadata_str,
    const base::RepeatingClosure& schedule_save_closure) {
  local_or_syncable_bookmark_sync_service_->DecodeBookmarkSyncMetadata(
      metadata_str, schedule_save_closure,
      std::make_unique<
          sync_bookmarks::BookmarkModelViewUsingLocalOrSyncableNodes>(model_));
}

ChromeBookmarkClient::DecodeAccountBookmarkSyncMetadataResult
ChromeBookmarkClient::DecodeAccountBookmarkSyncMetadata(
    const std::string& metadata_str,
    const base::RepeatingClosure& schedule_save_closure) {
  if (!account_bookmark_sync_service_) {
    return DecodeAccountBookmarkSyncMetadataResult::
        kMustRemoveAccountPermanentFolders;
  }

  account_bookmark_sync_service_->DecodeBookmarkSyncMetadata(
      metadata_str, schedule_save_closure,
      std::make_unique<sync_bookmarks::BookmarkModelViewUsingAccountNodes>(
          model_));
  return account_bookmark_sync_service_->IsTrackingMetadata()
             ? DecodeAccountBookmarkSyncMetadataResult::kSuccess
             : DecodeAccountBookmarkSyncMetadataResult::
                   kMustRemoveAccountPermanentFolders;
}

void ChromeBookmarkClient::OnBookmarkNodeRemovedUndoable(
    const bookmarks::BookmarkNode* parent,
    size_t index,
    std::unique_ptr<bookmarks::BookmarkNode> node) {
  bookmark_undo_service_->AddUndoEntryForRemovedNode(parent, index,
                                                     std::move(node));
}

void ChromeBookmarkClient::SchedulePersistentTimerForDailyMetrics(
    base::RepeatingClosure metrics_callback) {
  if (PrefService* prefs = profile_->GetPrefs()) {
    // Periodically records the metrics for local and/or account bookmarks
    // availability in permanent nodes.
    repeating_timer_ = std::make_unique<signin::PersistentRepeatingTimer>(
        prefs, bookmarks::prefs::kBookmarkStorageComputationLastUpdatePref,
        base::Hours(24), metrics_callback);
    repeating_timer_->Start();
  }
}