File: rlz_tracker.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 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 (283 lines) | stat: -rw-r--r-- 11,371 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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_RLZ_RLZ_TRACKER_H_
#define COMPONENTS_RLZ_RLZ_TRACKER_H_

#include <map>
#include <memory>
#include <string>

#include "base/memory/ref_counted.h"
#include "base/no_destructor.h"
#include "base/sequence_checker.h"
#include "base/thread_annotations.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "rlz/lib/rlz_lib.h"

namespace base {
class SequencedTaskRunner;
}

namespace rlz {

class RLZTrackerDelegate;

// RLZ is a library which is used to measure distribution scenarios.
// Its job is to record certain lifetime events in the registry and to send
// them encoded as a compact string at most twice. The sent data does
// not contain information that can be used to identify a user or to infer
// browsing habits. The API in this file is a wrapper around the open source
// RLZ library which can be found at http://code.google.com/p/rlz.
//
// For partner or bundled installs, the RLZ might send more information
// according to the terms disclosed in the EULA.

class RLZTracker {
 public:
  RLZTracker(const RLZTracker&) = delete;
  RLZTracker& operator=(const RLZTracker&) = delete;

  // Sets the RLZTrackerDelegate that should be used by the global RLZTracker
  // instance. Must be called before calling any other method of RLZTracker.
  static void SetRlzDelegate(std::unique_ptr<RLZTrackerDelegate> delegate);

  // Clear the RLZTrackerDelegate for testing only.
  static void ClearRlzDelegateForTesting();

  // Initializes the RLZ library services for use in chrome. Schedules a delayed
  // task that performs the ping and registers some events when 'first-run' is
  // true.
  //
  // When |send_ping_immediately| is true, a financial ping should be sent
  // immediately after a first search is recorded, without waiting for |delay|.
  // However, we only want this behaviour on first run.
  //
  // If the chrome brand is organic (no partners) then the pings don't occur.
  static bool InitRlzDelayed(bool first_run,
                             bool send_ping_immediately,
                             base::TimeDelta delay,
                             bool is_google_default_search,
                             bool is_google_homepage,
                             bool is_google_in_startpages);

  // Records an RLZ event. Some events can be access point independent.
  // Returns false if the event could not be recorded. Requires write access
  // to the HKCU registry hive on windows.
  static bool RecordProductEvent(rlz_lib::Product product,
                                 rlz_lib::AccessPoint point,
                                 rlz_lib::Event event_id);

  // For the point parameter of RecordProductEvent.
  static rlz_lib::AccessPoint ChromeOmnibox();
#if !BUILDFLAG(IS_IOS)
  static rlz_lib::AccessPoint ChromeHomePage();
  static rlz_lib::AccessPoint ChromeAppList();
#endif  // !BUILDFLAG(IS_IOS)

  // Gets the HTTP header value that can be added to requests from the
  // specific access point.  The string returned is of the form:
  //
  //    "X-Rlz-String: <access-point-rlz>\r\n"
  //
  static std::string GetAccessPointHttpHeader(rlz_lib::AccessPoint point);

  // Gets the RLZ value of the access point.
  // Returns false if the rlz string could not be obtained. In some cases
  // an empty string can be returned which is not an error.
  static bool GetAccessPointRlz(rlz_lib::AccessPoint point,
                                std::u16string* rlz);

  // Invoked during shutdown to clean up any state created by RLZTracker.
  static void CleanupRlz();

#if BUILDFLAG(IS_CHROMEOS)
  // Clears all product state. Should be called when turning RLZ off. On other
  // platforms, this is done by product uninstaller.
  static void ClearRlzState();
#endif

  // This method is public for use by the Singleton class.
  static RLZTracker* GetInstance();

  // Enables zero delay for InitRlzDelayed. For testing only.
  static void EnableZeroDelayForTesting();

#if !BUILDFLAG(IS_IOS)
  // Records that the app list search has been used.
  static void RecordAppListSearch();

  // Returns true if there is a non-organic brand code and we have never
  // recorded that user has performed a Google search from their Google homepage
  // yet.
  static bool ShouldRecordChromeHomePageSearch();

  // Records that the user has their homepage set to Google search and performs
  // a Google search from there. This event should be recorded at most once.
  static void RecordChromeHomePageSearch();

  // Manually sets if the search has been performed for testing only.
  static void SetRlzChromeHomePageSearchRecordedForTesting(bool recorded);
#endif  // !BUILDFLAG(IS_IOS)

  // The following methods are made protected so that they can be used for
  // testing purposes. Production code should never need to call these.
 protected:
  RLZTracker();
  virtual ~RLZTracker();

  // Performs initialization of RLZ tracker that is purposefully delayed so
  // that it does not interfere with chrome startup time.
  virtual void DelayedInit();

  // Used by test code to override the default RLZTracker instance returned
  // by GetInstance().
  void set_tracker(RLZTracker* tracker) { tracker_ = tracker; }

  // Sends the financial ping to the RLZ servers and invalidates the RLZ string
  // cache since the response from the RLZ server may have changed then.
  // Protected so that its accessible from tests.
  void PingNowImpl();

 private:
  friend class base::NoDestructor<RLZTracker>;
  friend class base::RefCountedThreadSafe<RLZTracker>;

  // Implementation called from SetRlzDelegate() static method.
  void SetDelegate(std::unique_ptr<RLZTrackerDelegate> delegate);

  // Implementation called from ClearRlzDelegateForTesting() static method.
  void ClearDelegateForTesting();

  // Implementation called from InitRlzDelayed() static method.
  bool Init(bool first_run,
            bool send_ping_immediately,
            base::TimeDelta delay,
            bool is_google_default_search,
            bool is_google_homepage,
            bool is_google_in_startpages);

  // Implementation called from CleanupRlz static method.
  void Cleanup();

  // Implementation called from RecordProductEvent() static method.
  bool RecordProductEventImpl(rlz_lib::Product product,
                              rlz_lib::AccessPoint point,
                              rlz_lib::Event event_id);

  // Records FIRST_SEARCH event. Passed as bound callback to RLZTrackerDelegate.
  void RecordFirstSearch(rlz_lib::AccessPoint point);

  // Implementation called from GetAccessPointRlz() static method.
  bool GetAccessPointRlzImpl(rlz_lib::AccessPoint point, std::u16string* rlz);

  // Schedules the delayed initialization. This method is virtual to allow
  // tests to override how the scheduling is done.
  virtual void ScheduleDelayedInit(base::TimeDelta delay);

  // Schedules a call to rlz_lib::RecordProductEvent(). This method is virtual
  // to allow tests to override how the scheduling is done.
  virtual bool ScheduleRecordProductEvent(rlz_lib::Product product,
                                          rlz_lib::AccessPoint point,
                                          rlz_lib::Event event_id);

  // Schedules a call to rlz_lib::RecordFirstSearch(). This method is virtual
  // to allow tests to override how the scheduling is done.
  virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point);

  // Schedules a call to rlz_lib::SendFinancialPing(). This method is virtual
  // to allow tests to override how the scheduling is done.
  virtual void ScheduleFinancialPing();

  // Schedules a call to GetAccessPointRlz() on the I/O thread if the current
  // thread is not already the I/O thread, otherwise does nothing. Returns
  // true if the call was scheduled, and false otherwise. This method is
  // virtual to allow tests to override how the scheduling is done.
  virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point);

  // Sends the financial ping to the RLZ servers. This method is virtual to
  // allow tests to override.
  virtual bool SendFinancialPing(const std::string& brand,
                                 const std::u16string& lang,
                                 const std::u16string& referral);

#if BUILDFLAG(IS_CHROMEOS)
  // Implementation called from ClearRlzState static method.
  void ClearRlzStateImpl();

  // Schedules a call to ClearRlzStateImpl(). This method is virtual
  // to allow tests to override how the scheduling is done.
  virtual bool ScheduleClearRlzState();
#endif

  // Returns a pointer to the bool corresponding to whether |point| has been
  // used but not reported.
  bool* GetAccessPointRecord(rlz_lib::AccessPoint point);

#if !BUILDFLAG(IS_IOS)
  // Implementation called from SetRlzChromeHomePageSearchRecordedForTesting()
  // static method.
  void SetChromeHomePageSearchRecordedForTesting(bool recorded);
#endif  // !BUILDFLAG(IS_IOS)

  // Tracker used for testing purposes only. If this value is non-NULL, it
  // will be returned from GetInstance() instead of the regular singleton.
  static RLZTracker* tracker_;

  // Delegate abstracting embedder specific knowledge. Must not be null.
  std::unique_ptr<RLZTrackerDelegate> delegate_;

  // Configuation data for RLZ tracker. Set by call to Init().
  bool first_run_;
  bool send_ping_immediately_;
  bool is_google_default_search_;
  bool is_google_homepage_;
  bool is_google_in_startpages_;

  // Keeps track if the RLZ tracker has already performed its delayed
  // initialization.
  bool already_ran_;

  // Keeps a cache of RLZ access point strings, since they rarely change.
  // The cache must be protected by a lock since it may be accessed from
  // the UI thread for reading and the IO thread for reading and/or writing.
  base::Lock cache_lock_;
  std::map<rlz_lib::AccessPoint, std::u16string> rlz_cache_
      GUARDED_BY(cache_lock_);

  // Keeps track of whether the omnibox, home page or app list have been used.
  bool omnibox_used_;
  bool homepage_used_;
  bool app_list_used_;

#if !BUILDFLAG(IS_IOS)
  // Sets to true when we have attempted to record that user has performed a
  // Google search from their Google homepage. This will be set to true
  // regardless whether the event is recorded successfully, so that new
  // |ChromeRLZTrackerWebContentsObserver| only observes web contents if still
  // needed. On the contrast, |homepage_used_| is only set to true if the event
  // is not recorded successfully and needs another attempt.
  bool chrome_homepage_search_recorded_ = false;
#endif  // !BUILDFLAG(IS_IOS)

  // Main and (optionally) reactivation brand codes, assigned on UI thread.
  std::string brand_;
  std::string reactivation_brand_;

  // Minimum delay before sending financial ping after initialization.
  base::TimeDelta min_init_delay_;

  class WrapperURLLoaderFactory;
  std::unique_ptr<WrapperURLLoaderFactory> custom_url_loader_factory_;

  // Runner for RLZ background tasks.  The checker is used to verify operations
  // occur in the correct sequence, especially in tests.
  scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
  SEQUENCE_CHECKER(sequence_checker_);
};

}  // namespace rlz

#endif  // COMPONENTS_RLZ_RLZ_TRACKER_H_