File: theme_local_data_batch_uploader.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 (113 lines) | stat: -rw-r--r-- 4,271 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
// Copyright 2025 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/themes/theme_local_data_batch_uploader.h"

#include <variant>

#include "base/feature_list.h"
#include "base/functional/callback.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/themes/theme_service_utils.h"
#include "chrome/browser/themes/theme_syncable_service.h"
#include "chrome/browser/ui/webui/cr_components/theme_color_picker/customize_chrome_colors.h"
#include "chrome/common/pref_names.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/data_type.h"
#include "components/sync/base/features.h"
#include "components/sync/protocol/theme_specifics.pb.h"
#include "components/sync/service/local_data_description.h"
#include "ui/base/l10n/l10n_util.h"

namespace {
std::string BuildTitle(const sync_pb::ThemeSpecifics& specifics) {
  if (specifics.use_custom_theme()) {
    return specifics.custom_theme_name();
  }
  if (specifics.has_ntp_background() &&
      !specifics.ntp_background().attribution_line_1().empty()) {
    return specifics.ntp_background().attribution_line_1();
  }
  if (specifics.has_grayscale_theme_enabled()) {
    return l10n_util::GetStringUTF8(IDS_NTP_CUSTOMIZE_GREY_DEFAULT_LABEL);
  }
  if (specifics.has_user_color_theme()) {
    auto it = std::ranges::find_if(
        kDynamicCustomizeChromeColors,
        [&](const DynamicColorInfo& dynamic_color) {
          return dynamic_color.color == specifics.user_color_theme().color() &&
                 dynamic_color.variant ==
                     ProtoEnumToBrowserColorVariant(
                         specifics.user_color_theme().browser_color_variant());
        });
    if (it != kDynamicCustomizeChromeColors.end()) {
      return l10n_util::GetStringFUTF8(IDS_NTP_COLORS_BATCH_UPLOAD_DESCRIPTION,
                                       l10n_util::GetStringUTF16(it->label_id));
    }
  }
  return l10n_util::GetStringUTF8(IDS_NTP_CUSTOMIZE_COLOR_PICKER_LABEL);
}
}  // namespace

// static
const char ThemeLocalDataBatchUploader::kThemesLocalDataItemModelId[] =
    "current-theme";

ThemeLocalDataBatchUploader::ThemeLocalDataBatchUploader(
    ThemeLocalDataBatchUploaderDelegate* delegate)
    : delegate_(delegate) {
  CHECK(delegate_);
}

void ThemeLocalDataBatchUploader::GetLocalDataDescription(
    base::OnceCallback<void(syncer::LocalDataDescription)> callback) {
  syncer::LocalDataDescription desc;
  desc.type = syncer::THEMES;
  if (!base::FeatureList::IsEnabled(syncer::kThemesBatchUpload)) {
    std::move(callback).Run(desc);
    return;
  }
  // Avoid offering batch upload for local default theme.
  std::optional<sync_pb::ThemeSpecifics> specifics =
      GetNonDefaultSavedLocalTheme();
  base::UmaHistogramBoolean("Theme.BatchUpload.HasLocalTheme",
                            specifics.has_value());
  if (specifics.has_value()) {
    syncer::LocalDataItemModel item;
    item.id = kThemesLocalDataItemModelId;
    item.title = BuildTitle(*specifics);
    desc.local_data_models.push_back(std::move(item));
  }
  std::move(callback).Run(std::move(desc));
}

void ThemeLocalDataBatchUploader::TriggerLocalDataMigration() {
  CHECK(base::FeatureList::IsEnabled(syncer::kThemesBatchUpload));
  // Avoid migrating local default theme.
  if (GetNonDefaultSavedLocalTheme()) {
    delegate_->ApplySavedLocalThemeIfExistsAndClear();
    base::UmaHistogramBoolean("Theme.BatchUpload.LocalThemeMigrationTriggered",
                              true);
  }
}

void ThemeLocalDataBatchUploader::TriggerLocalDataMigrationForItems(
    std::vector<syncer::LocalDataItemModel::DataId> items) {
  if (items.empty()) {
    return;
  }
  CHECK_EQ(items.size(), 1U);
  CHECK_EQ(std::get<std::string>(items[0]), kThemesLocalDataItemModelId);
  TriggerLocalDataMigration();
}

std::optional<sync_pb::ThemeSpecifics>
ThemeLocalDataBatchUploader::GetNonDefaultSavedLocalTheme() const {
  std::optional<sync_pb::ThemeSpecifics> specifics =
      delegate_->GetSavedLocalTheme();
  return (specifics && ThemeSyncableService::HasNonDefaultTheme(*specifics))
             ? specifics
             : std::nullopt;
}