| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/build_time.h" | 9 #include "base/build_time.h" |
| 10 #include "base/callback.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 12 #include "base/sha1.h" | 13 #include "base/sha1.h" |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/sys_byteorder.h" | 17 #include "base/sys_byteorder.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 | 20 |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 392 | 393 |
| 393 for (RegistrationMap::iterator it = global_->registered_.begin(); | 394 for (RegistrationMap::iterator it = global_->registered_.begin(); |
| 394 it != global_->registered_.end(); ++it) { | 395 it != global_->registered_.end(); ++it) { |
| 395 FieldTrial::ActiveGroup active_group; | 396 FieldTrial::ActiveGroup active_group; |
| 396 if (it->second->GetActiveGroup(&active_group)) | 397 if (it->second->GetActiveGroup(&active_group)) |
| 397 active_groups->push_back(active_group); | 398 active_groups->push_back(active_group); |
| 398 } | 399 } |
| 399 } | 400 } |
| 400 | 401 |
| 401 // static | 402 // static |
| 402 bool FieldTrialList::CreateTrialsFromString(const std::string& trials_string, | 403 bool FieldTrialList::CreateTrialsFromString( |
| 403 FieldTrialActivationMode mode) { | 404 const std::string& trials_string, |
| 405 FieldTrialActivationMode mode, |
| 406 const IsAcceptedFieldTrialCallback& is_accepted_callback) { |
| 404 DCHECK(global_); | 407 DCHECK(global_); |
| 405 if (trials_string.empty() || !global_) | 408 if (trials_string.empty() || !global_) |
| 406 return true; | 409 return true; |
| 407 | 410 |
| 408 size_t next_item = 0; | 411 size_t next_item = 0; |
| 409 while (next_item < trials_string.length()) { | 412 while (next_item < trials_string.length()) { |
| 410 size_t name_end = trials_string.find(kPersistentStringSeparator, next_item); | 413 size_t name_end = trials_string.find(kPersistentStringSeparator, next_item); |
| 411 if (name_end == trials_string.npos || next_item == name_end) | 414 if (name_end == trials_string.npos || next_item == name_end) |
| 412 return false; | 415 return false; |
| 413 size_t group_name_end = trials_string.find(kPersistentStringSeparator, | 416 size_t group_name_end = trials_string.find(kPersistentStringSeparator, |
| 414 name_end + 1); | 417 name_end + 1); |
| 415 if (group_name_end == trials_string.npos || name_end + 1 == group_name_end) | 418 if (group_name_end == trials_string.npos || name_end + 1 == group_name_end) |
| 416 return false; | 419 return false; |
| 417 std::string name(trials_string, next_item, name_end - next_item); | 420 std::string name(trials_string, next_item, name_end - next_item); |
| 418 std::string group_name(trials_string, name_end + 1, | 421 std::string group_name(trials_string, name_end + 1, |
| 419 group_name_end - name_end - 1); | 422 group_name_end - name_end - 1); |
| 420 next_item = group_name_end + 1; | 423 next_item = group_name_end + 1; |
| 421 | 424 |
| 422 FieldTrial* trial = CreateFieldTrial(name, group_name); | 425 if (is_accepted_callback.is_null() || is_accepted_callback.Run(name)) { |
| 423 if (!trial) | 426 FieldTrial* trial = CreateFieldTrial(name, group_name); |
| 424 return false; | 427 if (!trial) |
| 425 if (mode == ACTIVATE_TRIALS) { | 428 return false; |
| 426 // Call |group()| to mark the trial as "used" and notify observers, if | 429 if (mode == ACTIVATE_TRIALS) { |
| 427 // any. This is useful to ensure that field trials created in child | 430 // Call |group()| to mark the trial as "used" and notify observers, if |
| 428 // processes are properly reported in crash reports. | 431 // any. This is useful to ensure that field trials created in child |
| 429 trial->group(); | 432 // processes are properly reported in crash reports. |
| 433 trial->group(); |
| 434 } |
| 430 } | 435 } |
| 431 } | 436 } |
| 432 return true; | 437 return true; |
| 433 } | 438 } |
| 434 | 439 |
| 435 // static | 440 // static |
| 436 FieldTrial* FieldTrialList::CreateFieldTrial( | 441 FieldTrial* FieldTrialList::CreateFieldTrial( |
| 437 const std::string& name, | 442 const std::string& name, |
| 438 const std::string& group_name) { | 443 const std::string& group_name) { |
| 439 DCHECK(global_); | 444 DCHECK(global_); |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 526 return; | 531 return; |
| 527 } | 532 } |
| 528 AutoLock auto_lock(global_->lock_); | 533 AutoLock auto_lock(global_->lock_); |
| 529 DCHECK(!global_->PreLockedFind(trial->trial_name())); | 534 DCHECK(!global_->PreLockedFind(trial->trial_name())); |
| 530 trial->AddRef(); | 535 trial->AddRef(); |
| 531 trial->SetTrialRegistered(); | 536 trial->SetTrialRegistered(); |
| 532 global_->registered_[trial->trial_name()] = trial; | 537 global_->registered_[trial->trial_name()] = trial; |
| 533 } | 538 } |
| 534 | 539 |
| 535 } // namespace base | 540 } // namespace base |
| OLD | NEW |