File: app_icon_writer.h

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-- 3,646 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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_APPS_APP_SERVICE_APP_ICON_APP_ICON_WRITER_H_
#define CHROME_BROWSER_APPS_APP_SERVICE_APP_ICON_APP_ICON_WRITER_H_

#include <map>
#include <set>
#include <vector>

#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/services/app_service/public/cpp/icon_types.h"
#include "ui/base/resource/resource_scale_factor.h"

class Profile;

namespace apps {

class CompressedIconGetter;

// AppIconWriter writes app icons and shortcut icons to the icon image files in
// the local disk.
class AppIconWriter {
 public:
  explicit AppIconWriter(Profile* profile);
  AppIconWriter(const AppIconWriter&) = delete;
  AppIconWriter& operator=(const AppIconWriter&) = delete;
  ~AppIconWriter();

  // Calls `compressed_icon_getter`'s GetCompressedIconData to get the
  // compressed icon data for `id`, and saves the icon data to the local disk.
  // Calls `callback` with true if saving and loading the data was successful.
  void InstallIcon(CompressedIconGetter* compressed_icon_getter,
                   const std::string& id,
                   int32_t size_in_dip,
                   base::OnceCallback<void(bool)> callback);

 private:
  // Key contains the arguments of InstallIcon. It implements operator<, so that
  // it can be the "K" in a "map<K, V>".
  class Key {
   public:
    Key(const std::string& id, int32_t size_in_dip);

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

    ~Key();

    Key(Key&&) = default;
    Key& operator=(Key&&) = default;

    bool operator<(const Key& other) const;

    std::string id_;
    int32_t size_in_dip_;
  };

  // Contains the scale factors and the callback for the compressed app icon
  // data requests.
  struct PendingResult {
    PendingResult();

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

    ~PendingResult();

    PendingResult(PendingResult&&);
    PendingResult& operator=(PendingResult&&);

    // The requested scale factors for the icon requests with `id` and
    // `size_in_dip`.
    std::set<ui::ResourceScaleFactor> scale_factors;

    // The finished icon requested for scale factors. E.g. the icon data for the
    // scale factor(k100Percent) has been fetched, and saved in the icon file,
    // and the icon data for the scale factor(k200Percent) has not been fetched.
    // Then we have:
    // scale_factors = {k100Percent, k200Percent}
    // complete_scale_factors = {k100Percent}
    std::set<ui::ResourceScaleFactor> complete_scale_factors;

    // The callbacks for the icon requests with `id` and `size_in_dip`.
    std::vector<base::OnceCallback<void(bool)>> callbacks;
  };

  // Saves the compressed icon data in `iv` to the local disk.
  void OnIconLoad(const std::string& id,
                  int32_t size_in_dip,
                  ui::ResourceScaleFactor scale_factor,
                  IconValuePtr iv);

  void OnWriteIconFile(const std::string& id,
                       int32_t size_in_dip,
                       ui::ResourceScaleFactor scale_factor,
                       bool ret);

  const raw_ptr<Profile> profile_;

  // The map from the app id to PendingResult, which contains pending app icon
  // requests.
  std::map<Key, PendingResult> pending_results_;

  base::WeakPtrFactory<AppIconWriter> weak_ptr_factory_{this};
};

}  // namespace apps

#endif  // CHROME_BROWSER_APPS_APP_SERVICE_APP_ICON_APP_ICON_WRITER_H_