File: device_name_store.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 (128 lines) | stat: -rw-r--r-- 4,520 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
117
118
119
120
121
122
123
124
125
126
127
128
// 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.

#ifndef CHROME_BROWSER_ASH_DEVICE_NAME_DEVICE_NAME_STORE_H_
#define CHROME_BROWSER_ASH_DEVICE_NAME_DEVICE_NAME_STORE_H_

#include <string>

#include "base/gtest_prod_util.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"

class PrefRegistrySimple;
class PrefService;

namespace policy {
class DeviceNamePolicyHandler;
}  // namespace policy

namespace ash {

// DeviceNameStore is a device-persistent model of the device name which
// clients use to display the device name to the user and broadcast the device
// name to local networks (e.g., the DHCP hostname). Clients also have the
// ability to set the device name (e.g., OS Settings).
//
// An initial device name is created on first boot and is set to the default
// name 'ChromeOS'.
//
// DeviceNameStore is responsible for updating the system state to reflect
// changes to the device name, e.g., setting the hostname via Shill.
//
// Must only be used on the UI thread.
class DeviceNameStore {
 public:
  // Types of results for setting the device name. Numerical values from this
  // enum must stay in sync with the JS enum in device_name_util.js.
  enum class SetDeviceNameResult {
    // Device name was updated successfully.
    kSuccess = 0,

    // Device name change is prohibited by policy. An administrator can choose
    // the device name directly and/or prevent managed users from changing it.
    kProhibitedByPolicy = 1,

    // Non-managed users who are not device owners cannot update the name.
    kNotDeviceOwner = 2,

    // Name must be >0 characters and <=15 characters long and contain only
    // letters, numbers or hyphens. Examples of invalid names include "Chrome
    // OS" (uses a space), "ChromeOS!" (uses an exclamation point),
    // "0123456789012345" (too long), "" (empty string).
    kInvalidName = 3,
  };

  // Types of states for the current device name. Numerical values from this
  // enum must stay in sync with the JS enum in device_name_util.js.
  enum class DeviceNameState {
    // Device name can be modified.
    kCanBeModified = 0,

    // Device name change is prohibited by policy. An administrator can choose
    // the device name directly and/or prevent managed users from changing it.
    kCannotBeModifiedBecauseOfPolicy = 1,

    // Non-managed users who are not device owners cannot modify the name.
    kCannotBeModifiedBecauseNotDeviceOwner = 2,
  };

  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;

    // Called when the device name and/or state changes. Use
    // GetDeviceNameMetadata() to get the new device name and state.
    virtual void OnDeviceNameMetadataChanged() = 0;
  };

  // Contains the device name and whether device name change is possible.
  struct DeviceNameMetadata {
    std::string device_name;
    DeviceNameState device_name_state;
  };

  // Returns a pointer to the singleton instance for the current process.
  // Should only be called after Initialize().
  static DeviceNameStore* GetInstance();

  // Register the pref used to store the device name in the local state.
  static void RegisterLocalStatePrefs(PrefRegistrySimple* registry);

  // Checks the local state for a device name to store if it exists; otherwise,
  // creates a new device name and persists it. Must be called before any other
  // non-static method on DeviceNameStore.
  // |prefs| is the PrefService used to persist and read the device name value.
  static void Initialize(PrefService* prefs,
                         policy::DeviceNamePolicyHandler* handler);

  // Shutdown() should be called to destroy the instance once its clients no
  // longer need it.
  static void Shutdown();

  virtual DeviceNameMetadata GetDeviceNameMetadata() const = 0;

  // Attempts to update the device name and returns the result of the update.
  virtual SetDeviceNameResult SetDeviceName(
      const std::string& new_device_name) = 0;

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

 protected:
  DeviceNameStore();
  virtual ~DeviceNameStore();

  void NotifyDeviceNameMetadataChanged();

 private:
  DeviceNameStore(const DeviceNameStore&) = delete;
  DeviceNameStore& operator=(const DeviceNameStore&) = delete;

  base::ObserverList<Observer> observer_list_;
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_DEVICE_NAME_DEVICE_NAME_STORE_H_