File: sync_appsync_optin_client.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (94 lines) | stat: -rw-r--r-- 4,027 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
// 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_ASH_SYNC_SYNC_APPSYNC_OPTIN_CLIENT_H_
#define CHROME_BROWSER_ASH_SYNC_SYNC_APPSYNC_OPTIN_CLIENT_H_

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "components/sync/service/sync_service_observer.h"

namespace syncer {
class SyncService;
}  // namespace syncer

namespace user_manager {
class UserManager;
}  // namespace user_manager

namespace ash {

/**
 * SyncAppsyncOptinClient listens for changes to a profile's opt-in to AppsSync,
 * and propagates those changes to a file in the user's daemon-store. This file
 * is used by crash_reporter/anomaly_detector in ChromeOS to check if a profile
 * has opted in to AppsSync without needing to communicate directly with
 * Chrome. The absence of the file is treated the same as a file indicating a
 * user has not opted in, so eif a write fails it falls back to the
 * default-private option, and will reattempt to write again in the future.
 */
class SyncAppsyncOptinClient : public syncer::SyncServiceObserver {
 public:
  // |sync_service| must not be null. |this| must be destroyed before
  // |sync_service| shutdown.
  // |user_manager| must not be null. |this| must be destroyed before
  // |user_manager| shutdown.
  explicit SyncAppsyncOptinClient(syncer::SyncService* sync_service,
                                  user_manager::UserManager* user_manager);
  // If the daemon-store location needs to be specified (e.g. for test)
  // this must be provided at instantiation time, since the constructor
  // will attempt to write out the opt-in file.
  explicit SyncAppsyncOptinClient(syncer::SyncService* sync_service,
                                  user_manager::UserManager* user_manager,
                                  const base::FilePath& daemon_store_location);
  explicit SyncAppsyncOptinClient(
      syncer::SyncService* sync_service,
      user_manager::UserManager* user_manager,
      const base::FilePath& daemon_store_location,
      const base::FilePath& old_daemon_store_location);
  SyncAppsyncOptinClient(const SyncAppsyncOptinClient& other) = delete;
  SyncAppsyncOptinClient& operator=(const SyncAppsyncOptinClient& other) =
      delete;
  ~SyncAppsyncOptinClient() override;

  // syncer::SyncServiceObserver
  void OnStateChanged(syncer::SyncService* sync_service) override;

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class AppsSyncOptinFileWrite {
    kAttempt = 0,
    kFailure = 1,
    kMaxValue = kFailure,
  };

 private:
  // Issues a write to the opt-in file, to reflect Profile state. The IO
  // operation is posted as a task to the ThreadPool, and thus could never be
  // run - or it may fail, but will be attempted again on state change or
  // client instantiation.
  void UpdateOptinFile(bool opted_in, const syncer::SyncService* sync_service);
  // Attmepts to remove any existing contents from defunct daemon-store
  // location. May silently fail (with debug log), but should be reattempted the
  // next time Client is instantiated so should eventually go through. Posted as
  // a task to the ThreadPool.
  void RemoveOldAppsyncDaemonDir(const syncer::SyncService* sync_service);
  // Looks up active profile and returns hash of username. String will be empty
  // if no profile can be found.
  std::string GetActiveProfileHash(const syncer::SyncService* sync_service);

  const raw_ptr<syncer::SyncService> sync_service_;
  const raw_ptr<user_manager::UserManager> user_manager_;

  bool is_apps_sync_enabled_;

  // Location of daemon-store - can be changed for testing.
  base::FilePath daemon_store_filepath_;
  // Only for use during migration from appsync-consent to appsync-optin
  // directory.
  base::FilePath old_daemon_store_filepath_;
};
}  // namespace ash

#endif  //  CHROME_BROWSER_ASH_SYNC_SYNC_APPSYNC_OPTIN_CLIENT_H_