study_filtering.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. // Copyright 2014 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. #include "components/variations/study_filtering.h"
  5. #include <stddef.h>
  6. #include <stdint.h>
  7. #include <set>
  8. #include "base/containers/contains.h"
  9. #include "base/logging.h"
  10. #include "base/strings/string_util.h"
  11. #include "components/variations/variations_seed_processor.h"
  12. namespace variations {
  13. namespace {
  14. // Converts |date_time| in Study date format to base::Time.
  15. base::Time ConvertStudyDateToBaseTime(int64_t date_time) {
  16. return base::Time::UnixEpoch() + base::Seconds(date_time);
  17. }
  18. // Similar to base::Contains(), but specifically for ASCII strings and
  19. // case-insensitive comparison.
  20. template <typename Collection>
  21. bool ContainsStringIgnoreCaseASCII(const Collection& collection,
  22. const std::string& value) {
  23. return std::find_if(std::begin(collection), std::end(collection),
  24. [&value](const std::string& s) -> bool {
  25. return base::EqualsCaseInsensitiveASCII(s, value);
  26. }) != std::end(collection);
  27. }
  28. } // namespace
  29. namespace internal {
  30. bool CheckStudyChannel(const Study::Filter& filter, Study::Channel channel) {
  31. // An empty channel list matches all channels.
  32. if (filter.channel_size() == 0)
  33. return true;
  34. return base::Contains(filter.channel(), channel);
  35. }
  36. bool CheckStudyFormFactor(const Study::Filter& filter,
  37. Study::FormFactor form_factor) {
  38. // If both filters are empty, match all values.
  39. if (filter.form_factor_size() == 0 && filter.exclude_form_factor_size() == 0)
  40. return true;
  41. // Allow the |form_factor| if it's in the allowlist.
  42. // Note if both are specified, the excludelist is ignored. We do not expect
  43. // both to be present for Chrome due to server-side checks.
  44. if (filter.form_factor_size() > 0)
  45. return base::Contains(filter.form_factor(), form_factor);
  46. // Omit if there is a matching excludelist entry.
  47. return !base::Contains(filter.exclude_form_factor(), form_factor);
  48. }
  49. bool CheckStudyCpuArchitecture(const Study::Filter& filter,
  50. Study::CpuArchitecture cpu_architecture) {
  51. // If both filters are empty, match all values.
  52. if (filter.cpu_architecture_size() == 0 &&
  53. filter.exclude_cpu_architecture_size() == 0) {
  54. return true;
  55. }
  56. // Allow the |cpu_architecture| if it's in the allowlist.
  57. // Note if both are specified, the excludelist is ignored. We do not expect
  58. // both to be present for Chrome due to server-side checks.
  59. if (filter.cpu_architecture_size() > 0)
  60. return base::Contains(filter.cpu_architecture(), cpu_architecture);
  61. // Omit if there is a matching excludelist entry.
  62. return !base::Contains(filter.exclude_cpu_architecture(), cpu_architecture);
  63. }
  64. bool CheckStudyHardwareClass(const Study::Filter& filter,
  65. const std::string& hardware_class) {
  66. // If both filters are empty, match all values.
  67. if (filter.hardware_class_size() == 0 &&
  68. filter.exclude_hardware_class_size() == 0) {
  69. return true;
  70. }
  71. // Note: This logic changed in M66. Prior to M66, this used substring
  72. // comparison logic to match hardware classes. In M66, it was made consistent
  73. // with other filters.
  74. // Allow the |hardware_class| if it's in the allowlist.
  75. // Note if both are specified, the excludelist is ignored. We do not expect
  76. // both to be present for Chrome due to server-side checks.
  77. if (filter.hardware_class_size() > 0) {
  78. return ContainsStringIgnoreCaseASCII(filter.hardware_class(),
  79. hardware_class);
  80. }
  81. // Omit if there is a matching excludelist entry.
  82. return !ContainsStringIgnoreCaseASCII(filter.exclude_hardware_class(),
  83. hardware_class);
  84. }
  85. bool CheckStudyLocale(const Study::Filter& filter, const std::string& locale) {
  86. // If both filters are empty, match all values.
  87. if (filter.locale_size() == 0 && filter.exclude_locale_size() == 0)
  88. return true;
  89. // Allow the |locale| if it's in the allowlist.
  90. // Note if both are specified, the excludelist is ignored. We do not expect
  91. // both to be present for Chrome due to server-side checks.
  92. if (filter.locale_size() > 0)
  93. return base::Contains(filter.locale(), locale);
  94. // Omit if there is a matching excludelist entry.
  95. return !base::Contains(filter.exclude_locale(), locale);
  96. }
  97. bool CheckStudyCountry(const Study::Filter& filter,
  98. const std::string& country) {
  99. // If both filters are empty, match all values.
  100. if (filter.country_size() == 0 && filter.exclude_country_size() == 0)
  101. return true;
  102. // Allow the |country| if it's in the allowlist.
  103. // Note if both are specified, the excludelist is ignored. We do not expect
  104. // both to be present for Chrome due to server-side checks.
  105. if (filter.country_size() > 0)
  106. return base::Contains(filter.country(), country);
  107. // Omit if there is a matching excludelist entry.
  108. return !base::Contains(filter.exclude_country(), country);
  109. }
  110. bool CheckStudyPlatform(const Study::Filter& filter, Study::Platform platform) {
  111. for (int i = 0; i < filter.platform_size(); ++i) {
  112. if (filter.platform(i) == platform)
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool CheckStudyLowEndDevice(const Study::Filter& filter,
  118. bool is_low_end_device) {
  119. return !filter.has_is_low_end_device() ||
  120. filter.is_low_end_device() == is_low_end_device;
  121. }
  122. bool CheckStudyPolicyRestriction(const Study::Filter& filter,
  123. RestrictionPolicy policy_restriction) {
  124. switch (policy_restriction) {
  125. // If the policy is set to no restrictions let any study that is not
  126. // specifically designated for clients requesting critical studies only.
  127. case RestrictionPolicy::NO_RESTRICTIONS:
  128. return filter.policy_restriction() != Study::CRITICAL_ONLY;
  129. // If the policy is set to only allow critical studies than make sure they
  130. // have that restriction applied on their Filter.
  131. case RestrictionPolicy::CRITICAL_ONLY:
  132. return filter.policy_restriction() != Study::NONE;
  133. // If the policy is set to not allow any variations then return false
  134. // regardless of the actual Filter.
  135. case RestrictionPolicy::ALL:
  136. return false;
  137. }
  138. }
  139. bool CheckStudyStartDate(const Study::Filter& filter,
  140. const base::Time& date_time) {
  141. if (filter.has_start_date()) {
  142. const base::Time start_date =
  143. ConvertStudyDateToBaseTime(filter.start_date());
  144. return date_time >= start_date;
  145. }
  146. return true;
  147. }
  148. bool CheckStudyEndDate(const Study::Filter& filter,
  149. const base::Time& date_time) {
  150. if (filter.has_end_date()) {
  151. const base::Time end_date = ConvertStudyDateToBaseTime(filter.end_date());
  152. return end_date >= date_time;
  153. }
  154. return true;
  155. }
  156. bool CheckStudyVersion(const Study::Filter& filter,
  157. const base::Version& version) {
  158. if (filter.has_min_version()) {
  159. if (version.CompareToWildcardString(filter.min_version()) < 0)
  160. return false;
  161. }
  162. if (filter.has_max_version()) {
  163. if (version.CompareToWildcardString(filter.max_version()) > 0)
  164. return false;
  165. }
  166. return true;
  167. }
  168. bool CheckStudyOSVersion(const Study::Filter& filter,
  169. const base::Version& version) {
  170. if (filter.has_min_os_version()) {
  171. if (!version.IsValid() ||
  172. version.CompareToWildcardString(filter.min_os_version()) < 0) {
  173. return false;
  174. }
  175. }
  176. if (filter.has_max_os_version()) {
  177. if (!version.IsValid() ||
  178. version.CompareToWildcardString(filter.max_os_version()) > 0) {
  179. return false;
  180. }
  181. }
  182. return true;
  183. }
  184. bool CheckStudyEnterprise(const Study::Filter& filter,
  185. const ClientFilterableState& client_state) {
  186. return !filter.has_is_enterprise() ||
  187. filter.is_enterprise() == client_state.IsEnterprise();
  188. }
  189. const std::string& GetClientCountryForStudy(
  190. const Study& study,
  191. const ClientFilterableState& client_state) {
  192. switch (study.consistency()) {
  193. case Study::SESSION:
  194. return client_state.session_consistency_country;
  195. case Study::PERMANENT:
  196. // Use the saved country for permanent consistency studies. This allows
  197. // Chrome to use the same country for filtering permanent consistency
  198. // studies between Chrome upgrades. Since some studies have user-visible
  199. // effects, this helps to avoid annoying users with experimental group
  200. // churn while traveling.
  201. return client_state.permanent_consistency_country;
  202. }
  203. // Unless otherwise specified, use an empty country that won't pass any
  204. // filters that specifically include countries, but will pass any filters
  205. // that specifically exclude countries.
  206. return base::EmptyString();
  207. }
  208. bool IsStudyExpired(const Study& study, const base::Time& date_time) {
  209. if (study.has_expiry_date()) {
  210. const base::Time expiry_date =
  211. ConvertStudyDateToBaseTime(study.expiry_date());
  212. return date_time >= expiry_date;
  213. }
  214. return false;
  215. }
  216. bool ShouldAddStudy(const Study& study,
  217. const ClientFilterableState& client_state,
  218. const VariationsLayers& layers) {
  219. if (study.has_layer()) {
  220. if (!layers.IsLayerMemberActive(study.layer().layer_id(),
  221. study.layer().layer_member_id())) {
  222. DVLOG(1) << "Filtered out study " << study.name()
  223. << " due to layer member not being active.";
  224. return false;
  225. }
  226. if (VariationsSeedProcessor::ShouldStudyUseLowEntropy(study) &&
  227. layers.IsLayerUsingDefaultEntropy(study.layer().layer_id())) {
  228. DVLOG(1) << "Filtered out study " << study.name()
  229. << " due to requiring a low entropy source yet being a member "
  230. "of a layer using the default entropy source.";
  231. return false;
  232. }
  233. }
  234. if (study.has_filter()) {
  235. if (!CheckStudyChannel(study.filter(), client_state.channel)) {
  236. DVLOG(1) << "Filtered out study " << study.name() << " due to channel.";
  237. return false;
  238. }
  239. if (!CheckStudyFormFactor(study.filter(), client_state.form_factor)) {
  240. DVLOG(1) << "Filtered out study " << study.name() <<
  241. " due to form factor.";
  242. return false;
  243. }
  244. if (!CheckStudyCpuArchitecture(study.filter(),
  245. client_state.cpu_architecture)) {
  246. DVLOG(1) << "Filtered out study " << study.name()
  247. << " due to cpu architecture.";
  248. return false;
  249. }
  250. if (!CheckStudyLocale(study.filter(), client_state.locale)) {
  251. DVLOG(1) << "Filtered out study " << study.name() << " due to locale.";
  252. return false;
  253. }
  254. if (!CheckStudyPlatform(study.filter(), client_state.platform)) {
  255. DVLOG(1) << "Filtered out study " << study.name() << " due to platform.";
  256. return false;
  257. }
  258. if (!CheckStudyVersion(study.filter(), client_state.version)) {
  259. DVLOG(1) << "Filtered out study " << study.name() << " due to version.";
  260. return false;
  261. }
  262. if (!CheckStudyStartDate(study.filter(), client_state.reference_date)) {
  263. DVLOG(1) << "Filtered out study " << study.name() <<
  264. " due to start date.";
  265. return false;
  266. }
  267. if (!CheckStudyEndDate(study.filter(), client_state.reference_date)) {
  268. DVLOG(1) << "Filtered out study " << study.name() << " due to end date.";
  269. return false;
  270. }
  271. if (!CheckStudyHardwareClass(study.filter(), client_state.hardware_class)) {
  272. DVLOG(1) << "Filtered out study " << study.name() <<
  273. " due to hardware_class.";
  274. return false;
  275. }
  276. if (!CheckStudyLowEndDevice(study.filter(),
  277. client_state.is_low_end_device)) {
  278. DVLOG(1) << "Filtered out study " << study.name()
  279. << " due to is_low_end_device.";
  280. return false;
  281. }
  282. if (!CheckStudyPolicyRestriction(study.filter(),
  283. client_state.policy_restriction)) {
  284. DVLOG(1) << "Filtered out study " << study.name()
  285. << " due to policy restriction.";
  286. return false;
  287. }
  288. if (!CheckStudyOSVersion(study.filter(), client_state.os_version)) {
  289. DVLOG(1) << "Filtered out study " << study.name()
  290. << " due to os_version.";
  291. return false;
  292. }
  293. const std::string& country = GetClientCountryForStudy(study, client_state);
  294. if (!CheckStudyCountry(study.filter(), country)) {
  295. DVLOG(1) << "Filtered out study " << study.name() << " due to country.";
  296. return false;
  297. }
  298. // Check for enterprise status last as checking whether the client is
  299. // enterprise can be slow.
  300. if (!CheckStudyEnterprise(study.filter(), client_state)) {
  301. DVLOG(1) << "Filtered out study " << study.name()
  302. << " due to enterprise state.";
  303. return false;
  304. }
  305. }
  306. DVLOG(1) << "Kept study " << study.name() << ".";
  307. return true;
  308. }
  309. } // namespace internal
  310. void FilterAndValidateStudies(const VariationsSeed& seed,
  311. const ClientFilterableState& client_state,
  312. const VariationsLayers& layers,
  313. std::vector<ProcessedStudy>* filtered_studies) {
  314. DCHECK(client_state.version.IsValid());
  315. // Add expired studies (in a disabled state) only after all the non-expired
  316. // studies have been added (and do not add an expired study if a corresponding
  317. // non-expired study got added). This way, if there's both an expired and a
  318. // non-expired study that applies, the non-expired study takes priority.
  319. std::set<std::string> created_studies;
  320. std::vector<ProcessedStudy> expired_studies;
  321. for (int i = 0; i < seed.study_size(); ++i) {
  322. const Study& study = seed.study(i);
  323. ProcessedStudy processed_study;
  324. bool is_expired =
  325. internal::IsStudyExpired(study, client_state.reference_date);
  326. if (!processed_study.Init(&study, is_expired))
  327. continue;
  328. if (!internal::ShouldAddStudy(*processed_study.study(), client_state,
  329. layers)) {
  330. continue;
  331. }
  332. if (processed_study.is_expired()) {
  333. expired_studies.push_back(processed_study);
  334. } else if (!base::Contains(created_studies,
  335. processed_study.study()->name())) {
  336. filtered_studies->push_back(processed_study);
  337. created_studies.insert(processed_study.study()->name());
  338. }
  339. }
  340. for (auto& expired_study : expired_studies) {
  341. if (!base::Contains(created_studies, expired_study.study()->name())) {
  342. filtered_studies->push_back(expired_study);
  343. }
  344. }
  345. }
  346. } // namespace variations