File: gfx_utils.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 (269 lines) | stat: -rw-r--r-- 9,957 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
// Copyright 2016 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/extensions/gfx_utils.h"

#include <vector>

#include "base/containers/flat_map.h"
#include "base/lazy_instance.h"
#include "chrome/browser/ash/app_list/arc/arc_app_list_prefs.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/common/extensions/extension_constants.h"
#include "chrome/common/pref_names.h"
#include "chrome/grit/chromeos_app_icon_resources.h"
#include "components/prefs/pref_service.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/common/constants.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/image/image_skia_operations.h"

namespace extensions {

namespace {

// The badge map between |arc_package_name| and |extension_id|. Note the mapping
// from |extension_id| to |arc_package_name| is unique, but the mapping from
// |arc_package_name| to |extension_id| is not.
const struct {
  const char* arc_package_name;
  const char* extension_id;
} kDualBadgeMap[] = {
    // Google Keep
    {"com.google.android.keep", "hmjkmjkepdijhoojdojkdfohbdgmmhki"},
    {"com.google.android.keep", "dondgdlndnpianbklfnehgdhkickdjck"},
    // GMail
    {"com.google.android.gm", extension_misc::kGmailAppId},
    {"com.google.android.gm", "bjdhhokmhgelphffoafoejjmlfblpdha"},
    // Google Drive
    {"com.google.android.apps.docs", extension_misc::kGoogleDriveAppId},
    {"com.google.android.apps.docs", "mdhnphfgagkpdhndljccoackjjhghlif"},
    // Google Maps
    {"com.google.android.apps.maps", "lneaknkopdijkpnocmklfnjbeapigfbh"},
    // Calculator
    {"com.google.android.calculator", "joodangkbfjnajiiifokapkpmhfnpleo"},
    // Google Calender
    {"com.google.android.calendar", "ejjicmeblgpmajnghnpcppodonldlgfn"},
    {"com.google.android.calendar", "fpgfohogebplgnamlafljlcidjedbdeb"},
    // Google Docs
    {"com.google.android.apps.docs.editors.docs",
     extension_misc::kGoogleDocsAppId},
    {"com.google.android.apps.docs.editors.docs",
     "npnjdccdffhdndcbeappiamcehbhjibf"},
    // Google Slides
    {"com.google.android.apps.docs.editors.slides",
     extension_misc::kGoogleSlidesAppId},
    {"com.google.android.apps.docs.editors.slides",
     "hdmobeajeoanbanmdlabnbnlopepchip"},
    // Google Sheets
    {"com.google.android.apps.docs.editors.sheets",
     extension_misc::kGoogleSheetsAppId},
    {"com.google.android.apps.docs.editors.sheets",
     "nifkmgcdokhkjghdlgflonppnefddien"},
    // YouTube
    {"com.google.android.youtube", extension_misc::kYoutubeAppId},
    {"com.google.android.youtube", "pbdihpaifchmclcmkfdgffnnpfbobefh"},
    // Google Play Books
    {"com.google.android.apps.books", extension_misc::kGooglePlayBooksAppId},
    // Google+
    {"com.google.android.apps.plus", "dlppkpafhbajpcmmoheippocdidnckmm"},
    {"com.google.android.apps.plus", "fgjnkhlabjcaajddbaenilcmpcidahll"},
    // Google Play Movies & TV
    {"com.google.android.videos", extension_misc::kGooglePlayMoviesAppId},
    {"com.google.android.videos", "amfoiggnkefambnaaphodjdmdooiinna"},
    // Google Play Music
    {"com.google.android.music", extension_misc::kGooglePlayMusicAppId},
    {"com.google.android.music", "onbhgdmifjebcabplolilidlpgeknifi"},
    // Google Now
    {"com.google.android.launcher", "mnfadmojomeniojkkikjpgjaegolkbpb"},
    // Google Photos
    {"com.google.android.apps.photos", "hcglmfcclpfgljeaiahehebeoaiicbko"},
    {"com.google.android.apps.photos", "efjnaogkjbogokcnohkmnjdojkikgobo"},
    {"com.google.android.apps.photos", "ifpkhncdnjfipfjlhfidljjffdgklanh"},
    // Google Classroom
    {"com.google.android.apps.classroom", "mfhehppjhmmnlfbbopchdfldgimhfhfk"},
    // Google Hangouts
    {"com.google.android.talk", "knipolnnllmklapflnccelgolnpehhpl"},
    {"com.google.android.talk", "cgmlfbhkckbedohgdepgbkflommbfkep"},
    {"com.google.android.talk", "gldgpnmcpaogjlojhhpebkbbanacoglc"},
    // Google Play Music
    {"com.google.android.music", "fahmaaghhglfmonjliepjlchgpgfmobi"},
    // Google News
    {"com.google.android.apps.genie.geniewidget",
     "dllkocilcinkggkchnjgegijklcililc"},
    // Used in unit tests.
    {"fake.package.name1", "emfkafnhnpcmabnnkckkchdilgeoekbo"},
};

// This class maintains the maps between the extension id and its equivalent
// ARC package name.
class AppDualBadgeMap {
 public:
  using ArcAppToExtensionsMap =
      base::flat_map<std::string, std::vector<std::string>>;
  using ExtensionToArcAppMap = base::flat_map<std::string, std::string>;

  AppDualBadgeMap() {
    for (auto dual_badge : kDualBadgeMap) {
      arc_app_to_extensions_map_[dual_badge.arc_package_name].push_back(
          dual_badge.extension_id);
      extension_to_arc_app_map_[dual_badge.extension_id] =
          dual_badge.arc_package_name;
    }
  }

  AppDualBadgeMap(const AppDualBadgeMap&) = delete;
  AppDualBadgeMap& operator=(const AppDualBadgeMap&) = delete;

  std::vector<std::string> GetExtensionIdsForArcPackageName(
      std::string arc_package_name) {
    const auto iter = arc_app_to_extensions_map_.find(arc_package_name);
    if (iter == arc_app_to_extensions_map_.end())
      return std::vector<std::string>();
    return iter->second;
  }

  std::string GetArcPackageNameFromExtensionId(std::string extension_id) {
    const auto iter = extension_to_arc_app_map_.find(extension_id);
    if (iter == extension_to_arc_app_map_.end())
      return std::string();
    return iter->second;
  }

 private:
  ArcAppToExtensionsMap arc_app_to_extensions_map_;
  ExtensionToArcAppMap extension_to_arc_app_map_;
};

base::LazyInstance<AppDualBadgeMap>::DestructorAtExit g_dual_badge_map =
    LAZY_INSTANCE_INITIALIZER;

}  // namespace

namespace util {

bool HasEquivalentInstalledArcApp(content::BrowserContext* context,
                                  const std::string& extension_id) {
  std::unordered_set<std::string> arc_apps;
  return GetEquivalentInstalledArcApps(context, extension_id, &arc_apps);
}

bool GetEquivalentInstalledArcApps(content::BrowserContext* context,
                                   const std::string& extension_id,
                                   std::unordered_set<std::string>* arc_apps) {
  const std::string arc_package_name =
      g_dual_badge_map.Get().GetArcPackageNameFromExtensionId(extension_id);
  if (arc_package_name.empty())
    return false;

  const ArcAppListPrefs* const prefs = ArcAppListPrefs::Get(context);
  if (!prefs)
    return false;

  // TODO(hidehiko): The icon is per launcher, so we should have more precise
  // check here.
  DCHECK(arc_apps);
  prefs->GetAppsForPackage(arc_package_name).swap(*arc_apps);
  return !arc_apps->empty();
}

const std::vector<std::string> GetEquivalentInstalledAppIds(
    const std::string& arc_package_name) {
  return g_dual_badge_map.Get().GetExtensionIdsForArcPackageName(
      arc_package_name);
}

const std::vector<std::string> GetEquivalentInstalledExtensions(
    content::BrowserContext* context,
    const std::string& arc_package_name) {
  const ExtensionRegistry* registry = ExtensionRegistry::Get(context);
  if (!registry)
    return std::vector<std::string>();

  std::vector<std::string> extension_ids =
      g_dual_badge_map.Get().GetExtensionIdsForArcPackageName(arc_package_name);
  if (extension_ids.empty())
    return std::vector<std::string>();

  std::erase_if(extension_ids, [registry](const std::string& extension_id) {
    return !registry->GetInstalledExtension(extension_id);
  });
  return extension_ids;
}

bool ShouldApplyChromeBadge(content::BrowserContext* context,
                            const std::string& extension_id) {
  DCHECK(context);

  Profile* profile = Profile::FromBrowserContext(context);
  // Only apply Chrome badge for the primary profile.
  if (!ash::ProfileHelper::IsPrimaryProfile(profile) ||
      !multi_user_util::IsProfileFromActiveUser(profile)) {
    return false;
  }

  const ExtensionRegistry* registry = ExtensionRegistry::Get(context);
  if (!registry || !registry->GetInstalledExtension(extension_id))
    return false;

  if (!HasEquivalentInstalledArcApp(context, extension_id))
    return false;

  return true;
}

bool ShouldApplyChromeBadgeToWebApp(content::BrowserContext* context,
                                    const std::string& web_app_id) {
  DCHECK(context);

  Profile* profile = Profile::FromBrowserContext(context);
  // Only apply Chrome badge for the primary profile.
  if (!ash::ProfileHelper::IsPrimaryProfile(profile) ||
      !multi_user_util::IsProfileFromActiveUser(profile)) {
    return false;
  }

  if (!HasEquivalentInstalledArcApp(context, web_app_id))
    return false;

  return true;
}

void ApplyBadge(gfx::ImageSkia* icon_out, ChromeAppIcon::Badge badge_type) {
  DCHECK(icon_out);
  DCHECK_NE(ChromeAppIcon::Badge::kNone, badge_type);

  int badge_res = 0;
  switch (badge_type) {
    case ChromeAppIcon::Badge::kChrome:
      badge_res = IDR_CHROME_ICON_BADGE;
      break;
    case ChromeAppIcon::Badge::kBlocked:
      badge_res = IDR_BLOCK_ICON_BADGE;
      break;
    case ChromeAppIcon::Badge::kPaused:
      badge_res = IDR_HOURGLASS_ICON_BADGE;
      break;
    default:
      NOTREACHED();
  }

  const gfx::ImageSkia* badge_image =
      ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(badge_res);
  DCHECK(badge_image);

  gfx::ImageSkia resized_badge_image = *badge_image;
  if (badge_image->size() != icon_out->size()) {
    resized_badge_image = gfx::ImageSkiaOperations::CreateResizedImage(
        *badge_image, skia::ImageOperations::RESIZE_BEST, icon_out->size());
  }
  *icon_out = gfx::ImageSkiaOperations::CreateSuperimposedImage(
      *icon_out, resized_badge_image);
}

}  // namespace util
}  // namespace extensions