1 // Copyright (c) 2012 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.
5 #ifndef CHROME_BROWSER_RLZ_RLZ_H_
6 #define CHROME_BROWSER_RLZ_RLZ_H_
8 #include "build/build_config.h"
10 #if defined(ENABLE_RLZ)
15 #include "base/basictypes.h"
16 #include "base/memory/singleton.h"
17 #include "base/strings/string16.h"
18 #include "base/threading/sequenced_worker_pool.h"
19 #include "base/time/time.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "rlz/lib/rlz_lib.h"
26 class URLRequestContextGetter
;
29 // RLZ is a library which is used to measure distribution scenarios.
30 // Its job is to record certain lifetime events in the registry and to send
31 // them encoded as a compact string at most twice. The sent data does
32 // not contain information that can be used to identify a user or to infer
33 // browsing habits. The API in this file is a wrapper around the open source
34 // RLZ library which can be found at http://code.google.com/p/rlz.
36 // For partner or bundled installs, the RLZ might send more information
37 // according to the terms disclosed in the EULA.
39 class RLZTracker
: public content::NotificationObserver
{
41 // Initializes the RLZ library services for use in chrome. Schedules a delayed
42 // task that performs the ping and registers some events when 'first-run' is
45 // When |send_ping_immediately| is true, a financial ping should be sent
46 // immediately after a first search is recorded, without waiting for |delay|.
47 // However, we only want this behaviour on first run.
49 // If the chrome brand is organic (no partners) then the pings don't occur.
50 static bool InitRlzFromProfileDelayed(Profile
* profile
,
52 bool send_ping_immediately
,
53 base::TimeDelta delay
);
55 // Records an RLZ event. Some events can be access point independent.
56 // Returns false it the event could not be recorded. Requires write access
57 // to the HKCU registry hive on windows.
58 static bool RecordProductEvent(rlz_lib::Product product
,
59 rlz_lib::AccessPoint point
,
60 rlz_lib::Event event_id
);
62 // For the point parameter of RecordProductEvent.
63 static rlz_lib::AccessPoint
ChromeOmnibox();
65 static rlz_lib::AccessPoint
ChromeHomePage();
66 static rlz_lib::AccessPoint
ChromeAppList();
69 // Gets the HTTP header value that can be added to requests from the
70 // specific access point. The string returned is of the form:
72 // "X-Rlz-String: <access-point-rlz>\r\n"
74 static std::string
GetAccessPointHttpHeader(rlz_lib::AccessPoint point
);
76 // Gets the RLZ value of the access point.
77 // Returns false if the rlz string could not be obtained. In some cases
78 // an empty string can be returned which is not an error.
79 static bool GetAccessPointRlz(rlz_lib::AccessPoint point
,
82 // Invoked during shutdown to clean up any state created by RLZTracker.
83 static void CleanupRlz();
85 #if defined(OS_CHROMEOS)
86 // Clears all product state. Should be called when turning RLZ off. On other
87 // platforms, this is done by product uninstaller.
88 static void ClearRlzState();
91 // This method is public for use by the Singleton class.
92 static RLZTracker
* GetInstance();
94 // Enables zero delay for InitRlzFromProfileDelayed. For testing only.
95 static void EnableZeroDelayForTesting();
98 // Records that the app list search has been used.
99 static void RecordAppListSearch();
102 // The following methods are made protected so that they can be used for
103 // testing purposes. Production code should never need to call these.
106 virtual ~RLZTracker();
108 // Called by InitRlzFromProfileDelayed with values taken from |profile|.
109 static bool InitRlzDelayed(bool first_run
,
110 bool send_ping_immediately
,
111 base::TimeDelta delay
,
112 bool is_google_default_search
,
113 bool is_google_homepage
,
114 bool is_google_in_startpages
);
116 // Performs initialization of RLZ tracker that is purposefully delayed so
117 // that it does not interfere with chrome startup time.
118 virtual void DelayedInit();
120 // content::NotificationObserver implementation:
121 virtual void Observe(int type
,
122 const content::NotificationSource
& source
,
123 const content::NotificationDetails
& details
) OVERRIDE
;
125 // Used by test code to override the default RLZTracker instance returned
127 void set_tracker(RLZTracker
* tracker
) {
131 // Sends the financial ping to the RLZ servers and invalidates the RLZ string
132 // cache since the response from the RLZ server may have changed then.
133 // Protected so that its accessible from tests.
137 friend struct DefaultSingletonTraits
<RLZTracker
>;
138 friend class base::RefCountedThreadSafe
<RLZTracker
>;
140 // Implementation called from InitRlzDelayed() static method.
141 bool Init(bool first_run
,
142 bool send_ping_immediately
,
143 base::TimeDelta delay
,
144 bool google_default_search
,
145 bool google_default_homepage
,
146 bool is_google_in_startpages
);
148 // Implementation called from RecordProductEvent() static method.
149 bool RecordProductEventImpl(rlz_lib::Product product
,
150 rlz_lib::AccessPoint point
,
151 rlz_lib::Event event_id
);
153 // Records FIRST_SEARCH event. Called from Observe() on blocking task runner.
154 void RecordFirstSearch(rlz_lib::AccessPoint point
);
156 // Implementation called from GetAccessPointRlz() static method.
157 bool GetAccessPointRlzImpl(rlz_lib::AccessPoint point
, base::string16
* rlz
);
159 // Schedules the delayed initialization. This method is virtual to allow
160 // tests to override how the scheduling is done.
161 virtual void ScheduleDelayedInit(base::TimeDelta delay
);
163 // Schedules a call to rlz_lib::RecordProductEvent(). This method is virtual
164 // to allow tests to override how the scheduling is done.
165 virtual bool ScheduleRecordProductEvent(rlz_lib::Product product
,
166 rlz_lib::AccessPoint point
,
167 rlz_lib::Event event_id
);
169 // Schedules a call to rlz_lib::RecordFirstSearch(). This method is virtual
170 // to allow tests to override how the scheduling is done.
171 virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point
);
173 // Schedules a call to rlz_lib::SendFinancialPing(). This method is virtual
174 // to allow tests to override how the scheduling is done.
175 virtual void ScheduleFinancialPing();
177 // Schedules a call to GetAccessPointRlz() on the I/O thread if the current
178 // thread is not already the I/O thread, otherwise does nothing. Returns
179 // true if the call was scheduled, and false otherwise. This method is
180 // virtual to allow tests to override how the scheduling is done.
181 virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point
);
183 // Sends the financial ping to the RLZ servers. This method is virtual to
184 // allow tests to override.
185 virtual bool SendFinancialPing(const std::string
& brand
,
186 const base::string16
& lang
,
187 const base::string16
& referral
);
189 #if defined(OS_CHROMEOS)
190 // Implementation called from ClearRlzState static method.
191 void ClearRlzStateImpl();
193 // Schedules a call to ClearRlzStateImpl(). This method is virtual
194 // to allow tests to override how the scheduling is done.
195 virtual bool ScheduleClearRlzState();
198 // Returns a pointer to the bool corresponding to whether |point| has been
199 // used but not reported.
200 bool* GetAccessPointRecord(rlz_lib::AccessPoint point
);
202 // Tracker used for testing purposes only. If this value is non-NULL, it
203 // will be returned from GetInstance() instead of the regular singleton.
204 static RLZTracker
* tracker_
;
206 // Configuation data for RLZ tracker. Set by call to Init().
208 bool send_ping_immediately_
;
209 bool is_google_default_search_
;
210 bool is_google_homepage_
;
211 bool is_google_in_startpages_
;
213 // Unique sequence token so that tasks posted by RLZTracker are executed
214 // sequentially in the blocking pool.
215 base::SequencedWorkerPool::SequenceToken worker_pool_token_
;
217 // Keeps track if the RLZ tracker has already performed its delayed
221 // Keeps a cache of RLZ access point strings, since they rarely change.
222 // The cache must be protected by a lock since it may be accessed from
223 // the UI thread for reading and the IO thread for reading and/or writing.
224 base::Lock cache_lock_
;
225 std::map
<rlz_lib::AccessPoint
, base::string16
> rlz_cache_
;
227 // Keeps track of whether the omnibox, home page or app list have been used.
232 // Main and (optionally) reactivation brand codes, assigned on UI thread.
234 std::string reactivation_brand_
;
236 content::NotificationRegistrar registrar_
;
238 // Minimum delay before sending financial ping after initialization.
239 base::TimeDelta min_init_delay_
;
241 DISALLOW_COPY_AND_ASSIGN(RLZTracker
);
244 #endif // defined(ENABLE_RLZ)
246 #endif // CHROME_BROWSER_RLZ_RLZ_H_