OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef CHROME_INSTALLER_UTIL_EXPERIMENT_STORAGE_H_ | |
6 #define CHROME_INSTALLER_UTIL_EXPERIMENT_STORAGE_H_ | |
7 | |
8 #include <memory> | |
9 | |
10 #include "base/macros.h" | |
11 #include "base/memory/ref_counted.h" | |
grt (UTC plus 2)
2017/06/02 11:30:46
unused
nikunjb
2017/06/02 22:49:48
Done.
| |
12 #include "base/strings/string16.h" | |
13 #include "base/win/scoped_handle.h" | |
14 | |
15 namespace installer { | |
16 | |
17 class Experiment; | |
18 struct ExperimentMetrics; | |
19 | |
20 // Manages the storage of experiment state on the machine. | |
21 // | |
22 // Participation is a per-install property evaluated one time to determine | |
23 // whether or not the install as a whole participates in the study. It is | |
24 // stored in the install's ClientState key. | |
25 // | |
26 // ExperimentMetrics are stored in a per-install experiment_label, while the | |
27 // fine-grained Experiment data are stored in a per-user key in Chrome's | |
28 // ClientState or ClientStateMedium key. For system-level installs, metrics are | |
29 // reported for only a single user on the machine, so only that one user will | |
30 // participate in the experiment. | |
31 // | |
32 // Owing to this "metrics are per-install" property in the face of potentially | |
33 // multiple users running the same install, ExperimentMetrics are considered the | |
34 // source of truth for an install's participation in the experiment. For any | |
35 // given state expressed in the experiment_label holding these metrics, there | |
36 // should be matching Experiment data for precisely one user. | |
37 // | |
38 // As state is written across multiple locations in the registry, a global | |
39 // mutex is used for all reads and writes to ensure consistent state. | |
40 class ExperimentStorage { | |
41 public: | |
42 enum class Participation { | |
43 // No participation state was found for the install. | |
44 kNotEvaluated, | |
45 | |
46 // The client is not participating in the study. | |
47 kNotParticipating, | |
48 | |
49 // The client is participating in the study. | |
50 kIsParticipating, | |
51 }; | |
52 | |
53 // Grants the holder exclusive access to the data in the registry. Consumers | |
54 // are expected to not hold an instance across any blocking operations. | |
55 class Lock { | |
56 public: | |
57 ~Lock(); | |
58 | |
59 // Reads the participation state for the install. Returns false in case of | |
60 // error. | |
61 bool ReadParticipation(Participation* participation); | |
62 | |
63 // Writes the participation state for the install. Returns false if the | |
64 // write failed. | |
65 bool WriteParticipation(Participation participation); | |
66 | |
67 // Loads the experiment metrics and data from the registry. Returns false if | |
68 // the state in the registry corresponds to a different user or could not be | |
69 // read. | |
70 bool LoadExperiment(Experiment* experiment); | |
71 | |
72 // Stores the experiment metrics and data in |experiment| into the registry. | |
73 bool StoreExperiment(const Experiment& experiment); | |
74 | |
75 // Loads per-install experiment metrics into |metrics|. Returns true if a | |
76 // value was read or if none was found, in which case |metrics| is set to | |
77 // the uninitialized state. Returns false in case of any error reading or | |
78 // parsing the metrics. | |
79 bool LoadMetrics(ExperimentMetrics* metrics); | |
80 | |
81 // Stores |metrics| in the per-install experiment_label. | |
82 bool StoreMetrics(const ExperimentMetrics& metrics); | |
83 | |
84 private: | |
85 friend ExperimentStorage; | |
86 | |
87 explicit Lock(ExperimentStorage* storage); | |
88 | |
89 ExperimentStorage* storage_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(Lock); | |
92 }; | |
93 | |
94 ExperimentStorage(); | |
95 ~ExperimentStorage(); | |
96 | |
97 // Returns exclusive access to the experiment storage. | |
98 std::unique_ptr<Lock> AcquireLock(); | |
99 | |
100 private: | |
101 FRIEND_TEST_ALL_PREFIXES(ExperimentStorageTest, TestEncodeDecodeMetrics); | |
102 FRIEND_TEST_ALL_PREFIXES(ExperimentStorageTest, TestEncodeDecodeForMin); | |
103 FRIEND_TEST_ALL_PREFIXES(ExperimentStorageTest, TestEncodeDecodeForMax); | |
104 FRIEND_TEST_ALL_PREFIXES(ExperimentStorageTest, TestLoadStoreMetrics); | |
105 FRIEND_TEST_ALL_PREFIXES(ExperimentStorageTest, TestLoadStoreExperiment); | |
106 | |
107 // Reads |len| bits ending at |low_bit| of a 64 bit unsigned int into an int. | |
108 static int ReadUint64Bits(uint64_t source, int low_bit, int len); | |
109 | |
110 // Sets the last |len| bits of |value| into |source| at bit position | |
111 // ending at low_bit. | |
112 static void SetUint64Bits(int value, int len, int low_bit, uint64_t* source); | |
113 | |
114 // Decodes |encoded_metrics| into |metrics|, return true on success. Returns | |
115 // false if the encoding is malformed. | |
116 static bool DecodeMetrics(base::StringPiece16 encoded_metrics, | |
117 ExperimentMetrics* metrics); | |
118 | |
119 // Returns the encoded form of |metrics|. | |
120 static base::string16 EncodeMetrics(const ExperimentMetrics& metrics); | |
121 | |
122 // Loads the per-install experiment metrics into |metrics|. Returns true if a | |
123 // value was read or if none was found, in which case |metrics| is set to the | |
124 // uninitialized state. Returns false in case of any error reading or parsing | |
125 // the metrics. | |
126 bool LoadMetricsUnsafe(ExperimentMetrics* metrics); | |
127 | |
128 // StoreMetrics without acquiring the mutex. | |
129 bool StoreMetricsUnsafe(const ExperimentMetrics& metrics); | |
130 | |
131 // Loads the experiment state for the current user's Retention key into | |
132 // |experiment|. Returns true if all values are read. Returns false otherwise, | |
133 // in which case |experiment| is left in an undefined state. | |
134 bool LoadStateUnsafe(Experiment* experiment); | |
135 | |
136 // Stores |experiment| in the current user's Retention key. | |
137 bool StoreStateUnsafe(const Experiment& experiment); | |
138 | |
139 // A global mutex with a distinct name for the current installation. | |
140 base::win::ScopedHandle mutex_; | |
141 | |
142 DISALLOW_COPY_AND_ASSIGN(ExperimentStorage); | |
143 }; | |
144 | |
145 } // namespace installer | |
146 | |
147 #endif // CHROME_INSTALLER_UTIL_EXPERIMENT_STORAGE_H_ | |
OLD | NEW |