File: almanac_fetcher.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 (87 lines) | stat: -rw-r--r-- 3,043 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 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_DISCOVERY_SERVICE_ALMANAC_FETCHER_H_
#define CHROME_BROWSER_APPS_APP_DISCOVERY_SERVICE_ALMANAC_FETCHER_H_

#include "base/memory/raw_ptr.h"
#include "chrome/browser/apps/almanac_api_client/proto_file_manager.h"
#include "chrome/browser/apps/app_discovery_service/almanac_api/launcher_app.pb.h"
#include "chrome/browser/apps/app_discovery_service/app_discovery_util.h"
#include "chrome/browser/apps/app_discovery_service/app_fetcher_manager.h"

class Profile;

namespace user_prefs {
class PrefRegistrySyncable;
}  // namespace user_prefs

namespace apps {
class AlmanacIconCache;

// This class processes data received from Almanac.
class AlmanacFetcher : public AppFetcher {
 public:
  AlmanacFetcher(Profile* profile,
                 std::unique_ptr<AlmanacIconCache> icon_cache);
  AlmanacFetcher(const AlmanacFetcher&) = delete;
  AlmanacFetcher& operator=(const AlmanacFetcher&) = delete;
  ~AlmanacFetcher() override;

  void GetApps(ResultCallback callback) override;

  base::CallbackListSubscription RegisterForAppUpdates(
      RepeatingResultCallback callback) override;

  void GetIcon(const std::string& icon_id,
               int32_t size_hint_in_dip,
               GetIconCallback callback) override;

  // Registers prefs used for calling the Almanac.
  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);

  // Allows tests to skip the check of whether the user has an official Google
  // API key so that we can trigger an Almanac query.
  static void SetSkipApiKeyCheckForTesting(bool skip_api_key_check);

  // Methods exposed for testing the Almanac server.

  // Returns the time the server was last called.
  base::Time GetLastAppsUpdateTime() const;

  // Sets the time when the server was last called.
  void SetLastAppsUpdateTime(base::Time value);

 private:
  // Downloads apps from the server, updates the disk and the in-memory app
  // caches, and notifies the class subscribers.
  void DownloadApps();

  // Writes the response to disk if the call to the server succeeded or reads
  // the cached data otherwise.
  void OnServerResponse(std::optional<proto::LauncherAppResponse> response);

  // Updates the app caches and relevant profile preferences on a successful
  // response.
  void OnFileWritten(proto::LauncherAppResponse response, bool write_complete);

  // Parses all app data on update and notifies all subscribers with it.
  void OnAppsUpdate(std::optional<proto::LauncherAppResponse> response);

  raw_ptr<Profile> profile_;

  std::vector<Result> apps_;

  ResultCallbackList subscribers_;

  std::unique_ptr<ProtoFileManager<proto::LauncherAppResponse>>
      proto_file_manager_;
  std::unique_ptr<AlmanacIconCache> icon_cache_;

  base::WeakPtrFactory<AlmanacFetcher> weak_factory_{this};
};

}  // namespace apps

#endif  // CHROME_BROWSER_APPS_APP_DISCOVERY_SERVICE_ALMANAC_FETCHER_H_