File: app_list_model.cc

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,122,156 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 (419 lines) | stat: -rw-r--r-- 14,435 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/app_list/model/app_list_model.h"

#include <string>
#include <utility>

#include "ash/app_list/model/app_list_folder_item.h"
#include "ash/app_list/model/app_list_item.h"
#include "ash/app_list/model/app_list_model_observer.h"
#include "ash/public/cpp/app_list/app_list_model_delegate.h"
#include "base/logging.h"

namespace ash {

AppListModel::AppListModel(AppListModelDelegate* app_list_model_delegate)
    : delegate_(app_list_model_delegate),
      top_level_item_list_(
          std::make_unique<AppListItemList>(app_list_model_delegate)) {
  item_list_scoped_observations_.AddObservation(top_level_item_list_.get());
}

AppListModel::~AppListModel() {
  item_list_scoped_observations_.RemoveAllObservations();
}

void AppListModel::AddObserver(AppListModelObserver* observer) {
  observers_.AddObserver(observer);
}

void AppListModel::RemoveObserver(AppListModelObserver* observer) {
  observers_.RemoveObserver(observer);
}

void AppListModel::SetStatus(AppListModelStatus status) {
  if (status_ == status)
    return;

  status_ = status;
  for (auto& observer : observers_)
    observer.OnAppListModelStatusChanged();
}

AppListItem* AppListModel::FindItem(const std::string& id) {
  AppListItem* item = top_level_item_list_->FindItem(id);
  if (item)
    return item;
  for (size_t i = 0; i < top_level_item_list_->item_count(); ++i) {
    AppListItem* child_item =
        top_level_item_list_->item_at(i)->FindChildItem(id);
    if (child_item)
      return child_item;
  }
  return nullptr;
}

AppListFolderItem* AppListModel::FindFolderItem(const std::string& id) {
  AppListItem* item = top_level_item_list_->FindItem(id);
  if (item && item->GetItemType() == AppListFolderItem::kItemType)
    return static_cast<AppListFolderItem*>(item);
  DCHECK(!item);
  return nullptr;
}

AppListFolderItem* AppListModel::CreateFolderItem(
    const std::string& folder_id) {
  DCHECK(!top_level_item_list()->FindItem(folder_id));
  std::unique_ptr<AppListFolderItem> new_folder =
      std::make_unique<AppListFolderItem>(folder_id, delegate_);
  new_folder->set_position(
      top_level_item_list_->CreatePositionBefore(syncer::StringOrdinal()));
  AppListItem* new_folder_item = AddItemToRootListAndNotify(
      std::move(new_folder), ReparentItemReason::kAdd);
  return static_cast<AppListFolderItem*>(new_folder_item);
}

AppListItem* AppListModel::AddItem(std::unique_ptr<AppListItem> item) {
  DCHECK(!item->IsInFolder());
  DCHECK(!top_level_item_list()->FindItem(item->id()));
  return AddItemToRootListAndNotify(std::move(item), ReparentItemReason::kAdd);
}

void AppListModel::SetItemMetadata(const std::string& id,
                                   std::unique_ptr<AppListItemMetadata> data) {
  AppListItem* item = FindItem(id);
  if (!item)
    return;

  // TODO(https://crbug.com/1252433): refactor this function because the current
  // implementation is bug prone.

  // data may not contain valid position or icon. Preserve it in this case.
  if (!data->position.IsValid())
    data->position = item->position();

  // Update the item's position and name based on the metadata.
  if (!data->position.Equals(item->position()))
    SetItemPosition(item, data->position);

  if (data->name != item->name()) {
    SetItemName(item, data->name);
  }

  if (data->accessible_name != item->accessible_name()) {
    SetItemAccessibleName(item, data->accessible_name);
  }

  if (data->progress > item->progress() ||
      data->app_status != item->app_status()) {
    item->SetProgress(data->progress);
    item->SetAppStatus(data->app_status);
    DVLOG(2) << "AppListModel::SetProgress: " << item->ToDebugString();
    for (auto& observer : observers_) {
      observer.OnAppListItemUpdated(item);
    }
  }

  if (data->icon.isNull()) {
    // Folder icons are generated on ash side so the icon of the metadata passed
    // from chrome side is null. Do not alter `item` default icon in this case.
    data->icon = item->GetDefaultIcon();
    data->icon_color = item->GetDefaultIconColor();
  }

  if (data->folder_id != item->folder_id())
    MoveItemToFolder(item, data->folder_id);

  item->SetMetadata(std::move(data));
}

AppListItem* AppListModel::AddItemToFolder(std::unique_ptr<AppListItem> item,
                                           const std::string& folder_id) {
  if (folder_id.empty())
    return AddItem(std::move(item));
  DVLOG(2) << "AddItemToFolder: " << item->id() << ": " << folder_id;
  CHECK_NE(folder_id, item->folder_id());
  DCHECK_NE(AppListFolderItem::kItemType, item->GetItemType());
  AppListFolderItem* dest_folder = FindFolderItem(folder_id);
  if (!dest_folder)
    dest_folder = CreateFolderItem(folder_id);
  DCHECK(!dest_folder->item_list()->FindItem(item->id()))
      << "Already in folder: " << dest_folder->id();
  return AddItemToFolderListAndNotify(dest_folder, std::move(item),
                                      ReparentItemReason::kAdd);
}

const std::string AppListModel::MergeItems(const std::string& target_item_id,
                                           const std::string& source_item_id) {
  DVLOG(2) << "MergeItems: " << source_item_id << " -> " << target_item_id;

  if (target_item_id == source_item_id) {
    LOG(WARNING) << "MergeItems tried to drop item onto itself ("
                 << source_item_id << " -> " << target_item_id << ").";
    return "";
  }

  // Find the target item.
  AppListItem* target_item = top_level_item_list_->FindItem(target_item_id);
  if (!target_item) {
    LOG(ERROR) << "MergeItems: Target no longer exists.";
    return "";
  }

  AppListItem* source_item = FindItem(source_item_id);
  if (!source_item) {
    LOG(ERROR) << "MergeItems: Source no longer exists.";
    return "";
  }

  // If the target item is a folder, just add the source item to it.
  if (target_item->GetItemType() == AppListFolderItem::kItemType) {
    AppListFolderItem* target_folder =
        static_cast<AppListFolderItem*>(target_item);
    if (target_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) {
      LOG(WARNING) << "MergeItems called with OEM folder as target";
      return "";
    }
    delegate_->RequestMoveItemToFolder(source_item_id, target_item_id);
    return target_folder->id();
  }

  return delegate_->RequestFolderCreation(target_item_id, source_item_id);
}

void AppListModel::MoveItemToFolder(AppListItem* item,
                                    const std::string& folder_id) {
  DVLOG(2) << "MoveItemToFolder: " << folder_id << " <- "
           << item->ToDebugString();
  if (item->folder_id() == folder_id)
    return;

  if (!item->IsInFolder()) {
    AppListFolderItem* dest_folder = FindFolderItem(folder_id);
    if (!dest_folder)
      dest_folder = CreateFolderItem(folder_id);
    // Handle the case that `item` is a top list item.
    std::unique_ptr<AppListItem> item_ptr = RemoveFromTopList(item);
    AddItemToFolderListAndNotify(dest_folder, std::move(item_ptr),
                                 ReparentItemReason::kUpdate);
    return;
  }

  ReparentOrDeleteItemInFolder(item, folder_id);
}

bool AppListModel::MoveItemToRootAt(AppListItem* item,
                                    syncer::StringOrdinal position) {
  DVLOG(2) << "MoveItemToRootAt: "
           << "[" << position.ToDebugString() << "]"
           << " <- " << item->ToDebugString();
  if (item->folder_id().empty())
    return false;
  AppListFolderItem* src_folder = FindFolderItem(item->folder_id());
  if (src_folder &&
      src_folder->folder_type() == AppListFolderItem::FOLDER_TYPE_OEM) {
    LOG(WARNING) << "MoveItemToFolderAt called with OEM folder as source";
    return false;
  }
  delegate_->RequestMoveItemToRoot(
      item->id(), top_level_item_list_->CreatePositionBefore(position));
  return true;
}

void AppListModel::SetItemPosition(AppListItem* item,
                                   const syncer::StringOrdinal& new_position) {
  if (!item->IsInFolder()) {
    SetRootItemPosition(item, new_position);
    return;
  }

  // The code below handles the case that `item` has a parent folder.

  AppListFolderItem* folder = FindFolderItem(item->folder_id());
  DCHECK(folder);
  folder->item_list()->SetItemPosition(item, new_position);
  for (auto& observer : observers_)
    observer.OnAppListItemUpdated(item);
}

void AppListModel::SetItemName(AppListItem* item, const std::string& name) {
  item->SetName(name);
  DVLOG(2) << "AppListModel::SetItemName: " << item->ToDebugString();
  for (auto& observer : observers_)
    observer.OnAppListItemUpdated(item);
}

void AppListModel::SetItemAccessibleName(AppListItem* item,
                                         const std::string& name) {
  item->SetAccessibleName(name);
  for (auto& observer : observers_) {
    observer.OnAppListItemUpdated(item);
  }
}

void AppListModel::DeleteItem(const std::string& id) {
  AppListItem* item = FindItem(id);
  if (!item)
    return;

  const std::string copied_folder_id = item->folder_id();
  if (!item->IsInFolder()) {
    DCHECK_EQ(0u, item->ChildItemCount())
        << "Invalid call to DeleteItem for item with children: " << id;
    for (auto& observer : observers_)
      observer.OnAppListItemWillBeDeleted(item);
    if (item->GetItemType() == AppListFolderItem::kItemType) {
      item_list_scoped_observations_.RemoveObservation(
          static_cast<AppListFolderItem*>(item)->item_list());
    }
    top_level_item_list_->DeleteItem(id);
    return;
  }

  // Destroy `item`.
  ReparentOrDeleteItemInFolder(item,
                               /*destination_folder_id=*/std::nullopt);
}

// Private methods

void AppListModel::OnListItemMoved(size_t from_index,
                                   size_t to_index,
                                   AppListItem* item) {
  for (auto& observer : observers_)
    observer.OnAppListItemUpdated(item);
}

AppListItem* AppListModel::AddItemToRootListAndNotify(
    std::unique_ptr<AppListItem> item_ptr,
    ReparentItemReason reason) {
  DCHECK(!item_ptr->IsInFolder());
  if (reason == ReparentItemReason::kAdd &&
      item_ptr->GetItemType() == AppListFolderItem::kItemType) {
    item_list_scoped_observations_.AddObservation(
        static_cast<AppListFolderItem*>(item_ptr.get())->item_list());
  }
  AppListItem* item = top_level_item_list_->AddItem(std::move(item_ptr));
  NotifyItemParentChange(item, reason);
  return item;
}

AppListItem* AppListModel::AddItemToFolderListAndNotify(
    AppListFolderItem* folder,
    std::unique_ptr<AppListItem> item_ptr,
    ReparentItemReason reason) {
  CHECK_NE(folder->id(), item_ptr->folder_id());

  // Calling `AppListItemList::AddItem()` could trigger
  // `AppListModel::SetItemMetadata()` so set the folder id before addition.
  item_ptr->set_folder_id(folder->id());

  AppListItem* item = folder->item_list()->AddItem(std::move(item_ptr));
  NotifyItemParentChange(item, reason);
  return item;
}

void AppListModel::NotifyItemParentChange(AppListItem* item,
                                          ReparentItemReason reason) {
  for (auto& observer : observers_) {
    switch (reason) {
      case ReparentItemReason::kAdd:
        observer.OnAppListItemAdded(item);
        break;
      case ReparentItemReason::kUpdate:
        observer.OnAppListItemUpdated(item);
        break;
    }
  }
}

std::unique_ptr<AppListItem> AppListModel::RemoveFromTopList(
    AppListItem* item) {
  DCHECK(!item->IsInFolder());

  if (item->GetItemType() == AppListFolderItem::kItemType) {
    item_list_scoped_observations_.RemoveObservation(
        static_cast<AppListFolderItem*>(item)->item_list());
  }

  return top_level_item_list_->RemoveItem(item->id());
}

void AppListModel::ReparentOrDeleteItemInFolder(
    AppListItem* item,
    std::optional<std::string> destination_folder_id) {
  AppListFolderItem* folder = FindFolderItem(item->folder_id());
  DCHECK(folder) << "Folder not found for item: " << item->ToDebugString();

  const std::string item_parent_id = item->folder_id();
  std::unique_ptr<AppListItem> removed_item =
      RemoveItemFromFolder(folder, item);
  if (destination_folder_id.has_value()) {
    // When an item is removed from a folder, it can be moved to the top
    // list or a folder.
    if (destination_folder_id->empty()) {
      AddItemToRootListAndNotify(std::move(removed_item),
                                 ReparentItemReason::kUpdate);
    } else {
      // Create a folder if the destination folder doesn't exist.
      AppListFolderItem* destination_folder =
          FindFolderItem(*destination_folder_id);
      if (!destination_folder)
        destination_folder = CreateFolderItem(*destination_folder_id);

      AddItemToFolderListAndNotify(destination_folder, std::move(removed_item),
                                   ReparentItemReason::kUpdate);
    }
  } else {
    // Destroy `removed_item` and notify observers.
    for (auto& observer : observers_)
      observer.OnAppListItemWillBeDeleted(item);
    removed_item.reset();  // Deletes item.
  }

  // Delete the folder if the folder becomes empty after child removal.
  DeleteFolderIfEmpty(item_parent_id);
}

std::unique_ptr<AppListItem> AppListModel::RemoveItemFromFolder(
    AppListFolderItem* folder,
    AppListItem* item) {
  CHECK_EQ(item->folder_id(), folder->id());
  std::unique_ptr<AppListItem> removed_item =
      folder->item_list()->RemoveItem(item->id());
  removed_item->set_folder_id("");
  return removed_item;
}

void AppListModel::DeleteFolderIfEmpty(const std::string& folder_id) {
  const AppListFolderItem* folder = FindFolderItem(folder_id);
  if (!folder || folder->item_list()->item_count())
    return;

  DVLOG(2) << "Deleting empty folder: " << folder->ToDebugString();
  std::string copy_id = folder->id();
  DeleteItem(copy_id);
}

void AppListModel::SetRootItemPosition(
    AppListItem* item,
    const syncer::StringOrdinal& new_position) {
  DCHECK(!item->IsInFolder());
  DCHECK(FindItem(item->id()));

  const bool index_change =
      top_level_item_list_->SetItemPosition(item, new_position);

  // If `index_change` is true, `OnListItemMoved()` is called and model
  // observers are signaled. Nothing to do so return early.
  if (index_change)
    return;

  for (auto& observer : observers_)
    observer.OnAppListItemUpdated(item);
}

}  // namespace ash