File: remote_apps_model.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 (116 lines) | stat: -rw-r--r-- 3,688 bytes parent folder | download | duplicates (7)
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
// Copyright 2020 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/ash/remote_apps/remote_apps_model.h"

namespace ash {

RemoteAppsModel::AppInfo::AppInfo(const std::string& id,
                                  const std::string& name,
                                  const GURL& icon_url,
                                  std::string folder_id,
                                  bool add_to_front)
    : id(id),
      name(name),
      icon_url(icon_url),
      folder_id(folder_id),
      icon(gfx::ImageSkia()),
      add_to_front(add_to_front) {}

RemoteAppsModel::AppInfo::AppInfo(const AppInfo& other) = default;

RemoteAppsModel::AppInfo::~AppInfo() = default;

RemoteAppsModel::FolderInfo::FolderInfo(const std::string& id,
                                        const std::string& name,
                                        bool add_to_front)
    : id(id), name(name), add_to_front(add_to_front) {}

RemoteAppsModel::FolderInfo::FolderInfo(const FolderInfo& other) = default;

RemoteAppsModel::FolderInfo::~FolderInfo() = default;

RemoteAppsModel::RemoteAppsModel()
    : id_generator_(std::make_unique<GuidIdGenerator>()) {}

RemoteAppsModel::~RemoteAppsModel() = default;

RemoteAppsModel::AppInfo& RemoteAppsModel::AddApp(const std::string& name,
                                                  const GURL& icon_url,
                                                  const std::string& folder_id,
                                                  bool add_to_front) {
  std::string id = id_generator_->GenerateId();
  app_map_.insert({id, AppInfo(id, name, icon_url, folder_id, add_to_front)});

  if (!folder_id.empty()) {
    DCHECK(folder_map_.find(folder_id) != folder_map_.end());
    FolderInfo& folder_info = folder_map_.at(folder_id);
    folder_info.items.insert(id);
  }

  return app_map_.at(id);
}

bool RemoteAppsModel::HasApp(const std::string& id) const {
  return app_map_.find(id) != app_map_.end();
}

RemoteAppsModel::AppInfo& RemoteAppsModel::GetAppInfo(const std::string& id) {
  DCHECK(app_map_.find(id) != app_map_.end());
  return app_map_.at(id);
}

const std::map<std::string, RemoteAppsModel::AppInfo>&
RemoteAppsModel::GetAllAppInfo() const {
  return app_map_;
}

RemoteAppsModel::FolderInfo& RemoteAppsModel::AddFolder(
    const std::string& folder_name,
    bool add_to_front) {
  std::string folder_id = id_generator_->GenerateId();
  auto it = folder_map_.insert(
      folder_map_.begin(),
      {folder_id, FolderInfo(folder_id, folder_name, add_to_front)});
  return it->second;
}

bool RemoteAppsModel::HasFolder(const std::string& folder_id) const {
  return folder_map_.find(folder_id) != folder_map_.end();
}

RemoteAppsModel::FolderInfo& RemoteAppsModel::GetFolderInfo(
    const std::string& folder_id) {
  DCHECK(folder_map_.find(folder_id) != folder_map_.end());
  return folder_map_.at(folder_id);
}

void RemoteAppsModel::DeleteApp(const std::string& id) {
  DCHECK(HasApp(id));
  auto it = app_map_.find(id);
  const std::string& folder_id = it->second.folder_id;

  if (!folder_id.empty()) {
    auto folder_it = folder_map_.find(folder_id);
    folder_it->second.items.erase(id);
  }

  app_map_.erase(it);
}

void RemoteAppsModel::DeleteFolder(const std::string& folder_id) {
  DCHECK(HasFolder(folder_id));
  auto it = folder_map_.find(folder_id);
  const std::set<std::string>& app_set = it->second.items;

  for (const auto& id : app_set) {
    DCHECK(app_map_.find(id) != app_map_.end());
    AppInfo& info = app_map_.at(id);
    info.folder_id.clear();
  }

  folder_map_.erase(it);
}

}  // namespace ash