rlz_tracker.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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.
  4. #ifndef COMPONENTS_RLZ_RLZ_TRACKER_H_
  5. #define COMPONENTS_RLZ_RLZ_TRACKER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/memory/ref_counted.h"
  10. #include "base/memory/singleton.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/thread_annotations.h"
  13. #include "base/time/time.h"
  14. #include "build/build_config.h"
  15. #include "build/chromeos_buildflags.h"
  16. #include "rlz/lib/rlz_lib.h"
  17. namespace base {
  18. class SequencedTaskRunner;
  19. }
  20. namespace rlz {
  21. class RLZTrackerDelegate;
  22. // RLZ is a library which is used to measure distribution scenarios.
  23. // Its job is to record certain lifetime events in the registry and to send
  24. // them encoded as a compact string at most twice. The sent data does
  25. // not contain information that can be used to identify a user or to infer
  26. // browsing habits. The API in this file is a wrapper around the open source
  27. // RLZ library which can be found at http://code.google.com/p/rlz.
  28. //
  29. // For partner or bundled installs, the RLZ might send more information
  30. // according to the terms disclosed in the EULA.
  31. class RLZTracker {
  32. public:
  33. RLZTracker(const RLZTracker&) = delete;
  34. RLZTracker& operator=(const RLZTracker&) = delete;
  35. // Sets the RLZTrackerDelegate that should be used by the global RLZTracker
  36. // instance. Must be called before calling any other method of RLZTracker.
  37. static void SetRlzDelegate(std::unique_ptr<RLZTrackerDelegate> delegate);
  38. // Initializes the RLZ library services for use in chrome. Schedules a delayed
  39. // task that performs the ping and registers some events when 'first-run' is
  40. // true.
  41. //
  42. // When |send_ping_immediately| is true, a financial ping should be sent
  43. // immediately after a first search is recorded, without waiting for |delay|.
  44. // However, we only want this behaviour on first run.
  45. //
  46. // If the chrome brand is organic (no partners) then the pings don't occur.
  47. static bool InitRlzDelayed(bool first_run,
  48. bool send_ping_immediately,
  49. base::TimeDelta delay,
  50. bool is_google_default_search,
  51. bool is_google_homepage,
  52. bool is_google_in_startpages);
  53. // Records an RLZ event. Some events can be access point independent.
  54. // Returns false it the event could not be recorded. Requires write access
  55. // to the HKCU registry hive on windows.
  56. static bool RecordProductEvent(rlz_lib::Product product,
  57. rlz_lib::AccessPoint point,
  58. rlz_lib::Event event_id);
  59. // For the point parameter of RecordProductEvent.
  60. static rlz_lib::AccessPoint ChromeOmnibox();
  61. #if !BUILDFLAG(IS_IOS)
  62. static rlz_lib::AccessPoint ChromeHomePage();
  63. static rlz_lib::AccessPoint ChromeAppList();
  64. #endif
  65. // Gets the HTTP header value that can be added to requests from the
  66. // specific access point. The string returned is of the form:
  67. //
  68. // "X-Rlz-String: <access-point-rlz>\r\n"
  69. //
  70. static std::string GetAccessPointHttpHeader(rlz_lib::AccessPoint point);
  71. // Gets the RLZ value of the access point.
  72. // Returns false if the rlz string could not be obtained. In some cases
  73. // an empty string can be returned which is not an error.
  74. static bool GetAccessPointRlz(rlz_lib::AccessPoint point,
  75. std::u16string* rlz);
  76. // Invoked during shutdown to clean up any state created by RLZTracker.
  77. static void CleanupRlz();
  78. #if BUILDFLAG(IS_CHROMEOS_ASH)
  79. // Clears all product state. Should be called when turning RLZ off. On other
  80. // platforms, this is done by product uninstaller.
  81. static void ClearRlzState();
  82. #endif
  83. // This method is public for use by the Singleton class.
  84. static RLZTracker* GetInstance();
  85. // Enables zero delay for InitRlzDelayed. For testing only.
  86. static void EnableZeroDelayForTesting();
  87. #if !BUILDFLAG(IS_IOS)
  88. // Records that the app list search has been used.
  89. static void RecordAppListSearch();
  90. #endif
  91. // The following methods are made protected so that they can be used for
  92. // testing purposes. Production code should never need to call these.
  93. protected:
  94. RLZTracker();
  95. virtual ~RLZTracker();
  96. // Performs initialization of RLZ tracker that is purposefully delayed so
  97. // that it does not interfere with chrome startup time.
  98. virtual void DelayedInit();
  99. // Used by test code to override the default RLZTracker instance returned
  100. // by GetInstance().
  101. void set_tracker(RLZTracker* tracker) { tracker_ = tracker; }
  102. // Sends the financial ping to the RLZ servers and invalidates the RLZ string
  103. // cache since the response from the RLZ server may have changed then.
  104. // Protected so that its accessible from tests.
  105. void PingNowImpl();
  106. private:
  107. friend struct base::DefaultSingletonTraits<RLZTracker>;
  108. friend class base::RefCountedThreadSafe<RLZTracker>;
  109. // Implementation called from SetRlzDelegate() static method.
  110. void SetDelegate(std::unique_ptr<RLZTrackerDelegate> delegate);
  111. // Implementation called from InitRlzDelayed() static method.
  112. bool Init(bool first_run,
  113. bool send_ping_immediately,
  114. base::TimeDelta delay,
  115. bool is_google_default_search,
  116. bool is_google_homepage,
  117. bool is_google_in_startpages);
  118. // Implementation called from CleanupRlz static method.
  119. void Cleanup();
  120. // Implementation called from RecordProductEvent() static method.
  121. bool RecordProductEventImpl(rlz_lib::Product product,
  122. rlz_lib::AccessPoint point,
  123. rlz_lib::Event event_id);
  124. // Records FIRST_SEARCH event. Passed as bound callback to RLZTrackerDelegate.
  125. void RecordFirstSearch(rlz_lib::AccessPoint point);
  126. // Implementation called from GetAccessPointRlz() static method.
  127. bool GetAccessPointRlzImpl(rlz_lib::AccessPoint point, std::u16string* rlz);
  128. // Schedules the delayed initialization. This method is virtual to allow
  129. // tests to override how the scheduling is done.
  130. virtual void ScheduleDelayedInit(base::TimeDelta delay);
  131. // Schedules a call to rlz_lib::RecordProductEvent(). This method is virtual
  132. // to allow tests to override how the scheduling is done.
  133. virtual bool ScheduleRecordProductEvent(rlz_lib::Product product,
  134. rlz_lib::AccessPoint point,
  135. rlz_lib::Event event_id);
  136. // Schedules a call to rlz_lib::RecordFirstSearch(). This method is virtual
  137. // to allow tests to override how the scheduling is done.
  138. virtual bool ScheduleRecordFirstSearch(rlz_lib::AccessPoint point);
  139. // Schedules a call to rlz_lib::SendFinancialPing(). This method is virtual
  140. // to allow tests to override how the scheduling is done.
  141. virtual void ScheduleFinancialPing();
  142. // Schedules a call to GetAccessPointRlz() on the I/O thread if the current
  143. // thread is not already the I/O thread, otherwise does nothing. Returns
  144. // true if the call was scheduled, and false otherwise. This method is
  145. // virtual to allow tests to override how the scheduling is done.
  146. virtual bool ScheduleGetAccessPointRlz(rlz_lib::AccessPoint point);
  147. // Sends the financial ping to the RLZ servers. This method is virtual to
  148. // allow tests to override.
  149. virtual bool SendFinancialPing(const std::string& brand,
  150. const std::u16string& lang,
  151. const std::u16string& referral);
  152. #if BUILDFLAG(IS_CHROMEOS_ASH)
  153. // Implementation called from ClearRlzState static method.
  154. void ClearRlzStateImpl();
  155. // Schedules a call to ClearRlzStateImpl(). This method is virtual
  156. // to allow tests to override how the scheduling is done.
  157. virtual bool ScheduleClearRlzState();
  158. #endif
  159. // Returns a pointer to the bool corresponding to whether |point| has been
  160. // used but not reported.
  161. bool* GetAccessPointRecord(rlz_lib::AccessPoint point);
  162. // Tracker used for testing purposes only. If this value is non-NULL, it
  163. // will be returned from GetInstance() instead of the regular singleton.
  164. static RLZTracker* tracker_;
  165. // Delegate abstracting embedder specific knowledge. Must not be null.
  166. std::unique_ptr<RLZTrackerDelegate> delegate_;
  167. // Configuation data for RLZ tracker. Set by call to Init().
  168. bool first_run_;
  169. bool send_ping_immediately_;
  170. bool is_google_default_search_;
  171. bool is_google_homepage_;
  172. bool is_google_in_startpages_;
  173. // Keeps track if the RLZ tracker has already performed its delayed
  174. // initialization.
  175. bool already_ran_;
  176. // Keeps a cache of RLZ access point strings, since they rarely change.
  177. // The cache must be protected by a lock since it may be accessed from
  178. // the UI thread for reading and the IO thread for reading and/or writing.
  179. base::Lock cache_lock_;
  180. std::map<rlz_lib::AccessPoint, std::u16string> rlz_cache_
  181. GUARDED_BY(cache_lock_);
  182. // Keeps track of whether the omnibox, home page or app list have been used.
  183. bool omnibox_used_;
  184. bool homepage_used_;
  185. bool app_list_used_;
  186. // Main and (optionally) reactivation brand codes, assigned on UI thread.
  187. std::string brand_;
  188. std::string reactivation_brand_;
  189. // Minimum delay before sending financial ping after initialization.
  190. base::TimeDelta min_init_delay_;
  191. class WrapperURLLoaderFactory;
  192. std::unique_ptr<WrapperURLLoaderFactory> custom_url_loader_factory_;
  193. // Runner for RLZ background tasks. The checker is used to verify operations
  194. // occur in the correct sequence, especially in tests.
  195. scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
  196. SEQUENCE_CHECKER(sequence_checker_);
  197. };
  198. } // namespace rlz
  199. #endif // COMPONENTS_RLZ_RLZ_TRACKER_H_