File: sync_app_list_helper.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 (180 lines) | stat: -rw-r--r-- 6,835 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
// Copyright 2013 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/sync/test/integration/sync_app_list_helper.h"

#include <map>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service.h"
#include "chrome/browser/ash/app_list/app_list_syncable_service_factory.h"
#include "chrome/browser/ash/app_list/chrome_app_list_item.h"
#include "chrome/browser/ash/app_list/chrome_app_list_model_updater.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
#include "chrome/browser/sync/test/integration/sync_test.h"
#include "chrome/common/extensions/sync_helper.h"
#include "extensions/browser/app_sorting.h"
#include "extensions/browser/extension_system.h"

using app_list::AppListSyncableService;
using app_list::AppListSyncableServiceFactory;

SyncAppListHelper* SyncAppListHelper::GetInstance() {
  SyncAppListHelper* instance = base::Singleton<SyncAppListHelper>::get();
  instance->SetupIfNecessary(sync_datatype_helper::test());
  return instance;
}

SyncAppListHelper::SyncAppListHelper() = default;

SyncAppListHelper::~SyncAppListHelper() = default;

void SyncAppListHelper::SetupIfNecessary(SyncTest* test) {
  if (setup_completed_) {
    DCHECK_EQ(test, test_);
    return;
  }
  test_ = test;
  for (Profile* profile : test_->GetAllProfiles()) {
    extensions::ExtensionSystem::Get(profile)->InitForRegularProfile(
        true /* extensions_enabled */);
  }

  setup_completed_ = true;
}

bool SyncAppListHelper::AppListMatch(Profile* profile1, Profile* profile2) {
  AppListSyncableService* service1 =
      AppListSyncableServiceFactory::GetForProfile(profile1);
  AppListSyncableService* service2 =
      AppListSyncableServiceFactory::GetForProfile(profile2);
  // Note: sync item entries may not exist in verifier, but item lists should
  // match.
  if (service1->GetModelUpdater()->ItemCount() !=
      service2->GetModelUpdater()->ItemCount()) {
    LOG(ERROR) << "Model item count: "
               << service1->GetModelUpdater()->ItemCount()
               << " != " << service2->GetModelUpdater()->ItemCount();
    return false;
  }
  bool res = true;
  for (size_t i = 0; i < service1->GetModelUpdater()->ItemCount(); ++i) {
    ChromeAppListItem* item1 = service1->GetModelUpdater()->ItemAtForTest(i);
    size_t index2;
    if (!service2->GetModelUpdater()->FindItemIndexForTest(item1->id(),
                                                           &index2)) {
      LOG(ERROR) << " Item(" << i << ") in profile1: " << item1->ToDebugString()
                 << " Not in profile2.";
      res = false;
      continue;
    }

    ChromeAppListItem* item2 =
        service2->GetModelUpdater()->ItemAtForTest(index2);
    if (item1->CompareForTest(item2)) {
      continue;
    }

    LOG(ERROR) << "Item(" << i << ") in profile1: " << item1->ToDebugString()
               << " != "
               << "Item(" << i << ") in profile2: " << item2->ToDebugString();
    res = false;
  }
  return res;
}

bool SyncAppListHelper::AllProfilesHaveSameAppList(size_t* size_out) {
  const std::vector<raw_ptr<Profile, VectorExperimental>>& profiles =
      test_->GetAllProfiles();
  for (Profile* profile : profiles) {
    if (profile != profiles.front() &&
        !AppListMatch(profiles.front(), profile)) {
      DVLOG(1) << "Profile1: "
               << AppListSyncableServiceFactory::GetForProfile(profile);
      PrintAppList(profile);
      DVLOG(1) << "Profile2: "
               << AppListSyncableServiceFactory::GetForProfile(
                      profiles.front());
      PrintAppList(profiles.front());
      return false;
    }
  }
  if (size_out) {
    *size_out = AppListSyncableServiceFactory::GetForProfile(profiles.front())
                    ->GetModelUpdater()
                    ->ItemCount();
  }
  return true;
}

void SyncAppListHelper::MoveAppToFolder(Profile* profile,
                                        const std::string& id,
                                        const std::string& folder_id) {
  AppListSyncableService* service =
      AppListSyncableServiceFactory::GetForProfile(profile);
  service->GetModelUpdater()->SetItemFolderId(id, folder_id);
}

void SyncAppListHelper::MoveAppFromFolder(Profile* profile,
                                          const std::string& id,
                                          const std::string& folder_id) {
  AppListSyncableService* service =
      AppListSyncableServiceFactory::GetForProfile(profile);
  ChromeAppListItem* folder =
      service->GetModelUpdater()->FindFolderItem(folder_id);
  if (!folder) {
    LOG(ERROR) << "Folder not found: " << folder_id;
    return;
  }
  service->GetModelUpdater()->SetItemFolderId(id, "");
}

void SyncAppListHelper::PrintAppList(Profile* profile) {
  AppListSyncableService* service =
      AppListSyncableServiceFactory::GetForProfile(profile);
  // Build a map from each folder item's id to a list of its child items.
  std::map<const std::string, std::vector<ChromeAppListItem*>> children;
  for (size_t i = 0; i < service->GetModelUpdater()->ItemCount(); ++i) {
    ChromeAppListItem* item = service->GetModelUpdater()->ItemAtForTest(i);
    if (!item->folder_id().empty()) {
      children[item->folder_id()].push_back(item);
    }
  }
  for (size_t i = 0; i < service->GetModelUpdater()->ItemCount(); ++i) {
    ChromeAppListItem* item = service->GetModelUpdater()->ItemAtForTest(i);
    // Skip if it's not a top level item.
    if (!item->folder_id().empty()) {
      continue;
    }
    std::string label = base::StringPrintf("Item(%d): ", static_cast<int>(i));
    PrintItem(profile, item, label);
    // Print children if it has any.
    if (children.count(item->id())) {
      DCHECK(item->is_folder());
      std::vector<ChromeAppListItem*>& child_items =
          children[item->folder_id()];
      for (size_t j = 0; j < child_items.size(); ++j) {
        ChromeAppListItem* child_item = child_items[j];
        std::string child_label =
            base::StringPrintf(" Folder Item(%d): ", static_cast<int>(j));
        PrintItem(profile, child_item, child_label);
      }
    }
  }
}

void SyncAppListHelper::PrintItem(Profile* profile,
                                  ChromeAppListItem* item,
                                  const std::string& label) {
  extensions::AppSorting* s =
      extensions::ExtensionSystem::Get(profile)->app_sorting();
  std::string id = item->id();
  DVLOG(1) << label << item->ToDebugString()
           << " Page: " << s->GetPageOrdinal(id).ToDebugString().substr(0, 8)
           << " Item: "
           << s->GetAppLaunchOrdinal(id).ToDebugString().substr(0, 8);
}