Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(979)

Side by Side Diff: base/metrics/field_trial.cc

Issue 2365273004: Initial implementation for sharing field trial state (win) (Closed)
Patch Set: Address comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include <utility> 8 #include <utility>
9 9
10 #include "base/base_switches.h"
10 #include "base/build_time.h" 11 #include "base/build_time.h"
12 #include "base/command_line.h"
13 #include "base/feature_list.h"
11 #include "base/logging.h" 14 #include "base/logging.h"
12 #include "base/rand_util.h" 15 #include "base/rand_util.h"
13 #include "base/strings/string_number_conversions.h" 16 #include "base/strings/string_number_conversions.h"
14 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
15 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
16 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
17 20
18 namespace base { 21 namespace base {
19 22
20 namespace { 23 namespace {
(...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 // Call |group()| to mark the trial as "used" and notify observers, if 554 // Call |group()| to mark the trial as "used" and notify observers, if
552 // any. This is useful to ensure that field trials created in child 555 // any. This is useful to ensure that field trials created in child
553 // processes are properly reported in crash reports. 556 // processes are properly reported in crash reports.
554 trial->group(); 557 trial->group();
555 } 558 }
556 } 559 }
557 return true; 560 return true;
558 } 561 }
559 562
560 // static 563 // static
564 void FieldTrialList::CreateTrialsFromCommandLine(
565 const base::CommandLine cmd_line,
566 const char* field_trial_handle_switch) {
567 DCHECK(global_);
568
569 if (cmd_line.HasSwitch(field_trial_handle_switch)) {
570 #if defined(OS_WIN)
Alexei Svitkine (slow) 2016/10/06 13:45:30 Ifdef should be around the if.
lawrencewu 2016/10/06 20:14:57 Done.
571 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch);
572 size_t token = arg.find(",");
573 int field_trial_handle = std::stoi(arg.substr(0, token));
574 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length()));
575
576 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle);
577 base::SharedMemoryHandle shm_handle =
578 base::SharedMemoryHandle(handle, base::GetCurrentProcId());
579
580 base::SharedMemory shared_memory(shm_handle, false);
581 shared_memory.Map(field_trial_length);
582
583 char* field_trial_state = static_cast<char*>(shared_memory.memory());
584 bool result = FieldTrialList::CreateTrialsFromString(
585 std::string(field_trial_state), std::set<std::string>());
586 DCHECK(result);
587 #endif
588 }
Alexei Svitkine (slow) 2016/10/06 13:45:30 Add a return statement to the block above.
lawrencewu 2016/10/06 20:14:57 Done.
589
590 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) {
591 bool result = FieldTrialList::CreateTrialsFromString(
592 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials),
593 std::set<std::string>());
594 DCHECK(result);
595 }
596 }
597
598 // static
599 std::unique_ptr<base::SharedMemory>
600 FieldTrialList::CopyFieldTrialStateToSharedMemory(
601 base::CommandLine* cmd_line,
Alexei Svitkine (slow) 2016/10/06 13:45:30 Non-const params should be last.
lawrencewu 2016/10/06 20:14:57 Done.
602 const char* field_trial_handle_switch) {
603 std::string field_trial_states;
604 FieldTrialList::AllStatesToString(&field_trial_states);
605 if (!field_trial_states.empty()) {
606 #if defined(OS_WIN)
607 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory());
608 size_t length = field_trial_states.size() + 1;
609 shm->CreateAndMapAnonymous(length);
610 memcpy(shm->memory(), field_trial_states.c_str(), length);
611
612 // HANDLE is just typedef'd to void *
613 auto uintptr_handle =
614 reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle());
615 std::string field_trial_handle =
616 std::to_string(uintptr_handle) + "," + std::to_string(length);
617
618 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle);
619 return shm;
620 #else
621 return std::unique_ptr<base::SharedMemory>(nullptr);
622 #endif
623 } else {
624 return std::unique_ptr<base::SharedMemory>(nullptr);
625 }
626 }
627
628 // static
561 FieldTrial* FieldTrialList::CreateFieldTrial( 629 FieldTrial* FieldTrialList::CreateFieldTrial(
562 const std::string& name, 630 const std::string& name,
563 const std::string& group_name) { 631 const std::string& group_name) {
564 DCHECK(global_); 632 DCHECK(global_);
565 DCHECK_GE(name.size(), 0u); 633 DCHECK_GE(name.size(), 0u);
566 DCHECK_GE(group_name.size(), 0u); 634 DCHECK_GE(group_name.size(), 0u);
567 if (name.empty() || group_name.empty() || !global_) 635 if (name.empty() || group_name.empty() || !global_)
568 return NULL; 636 return NULL;
569 637
570 FieldTrial* field_trial = FieldTrialList::Find(name); 638 FieldTrial* field_trial = FieldTrialList::Find(name);
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
650 return; 718 return;
651 } 719 }
652 AutoLock auto_lock(global_->lock_); 720 AutoLock auto_lock(global_->lock_);
653 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); 721 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name();
654 trial->AddRef(); 722 trial->AddRef();
655 trial->SetTrialRegistered(); 723 trial->SetTrialRegistered();
656 global_->registered_[trial->trial_name()] = trial; 724 global_->registered_[trial->trial_name()] = trial;
657 } 725 }
658 726
659 } // namespace base 727 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698