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 // FieldTrial is a class for handling details of statistical experiments | 5 // FieldTrial is a class for handling details of statistical experiments |
6 // performed by actual users in the field (i.e., in a shipped or beta product). | 6 // performed by actual users in the field (i.e., in a shipped or beta product). |
7 // All code is called exclusively on the UI thread currently. | 7 // All code is called exclusively on the UI thread currently. |
8 // | 8 // |
9 // The simplest example is an experiment to see whether one of two options | 9 // The simplest example is an experiment to see whether one of two options |
10 // produces "better" results across our user population. In that scenario, UMA | 10 // produces "better" results across our user population. In that scenario, UMA |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 #include <set> | 62 #include <set> |
63 #include <string> | 63 #include <string> |
64 #include <vector> | 64 #include <vector> |
65 | 65 |
66 #include "base/base_export.h" | 66 #include "base/base_export.h" |
67 #include "base/command_line.h" | 67 #include "base/command_line.h" |
68 #include "base/gtest_prod_util.h" | 68 #include "base/gtest_prod_util.h" |
69 #include "base/macros.h" | 69 #include "base/macros.h" |
70 #include "base/memory/ref_counted.h" | 70 #include "base/memory/ref_counted.h" |
71 #include "base/memory/shared_memory.h" | 71 #include "base/memory/shared_memory.h" |
| 72 #include "base/metrics/persistent_memory_allocator.h" |
72 #include "base/observer_list_threadsafe.h" | 73 #include "base/observer_list_threadsafe.h" |
| 74 #include "base/process/launch.h" |
73 #include "base/strings/string_piece.h" | 75 #include "base/strings/string_piece.h" |
74 #include "base/synchronization/lock.h" | 76 #include "base/synchronization/lock.h" |
75 #include "base/time/time.h" | 77 #include "base/time/time.h" |
76 | 78 |
77 namespace base { | 79 namespace base { |
78 | 80 |
79 class FieldTrialList; | 81 class FieldTrialList; |
80 | 82 |
81 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { | 83 class BASE_EXPORT FieldTrial : public RefCounted<FieldTrial> { |
82 public: | 84 public: |
(...skipping 389 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 // If using shared memory to pass around the list of field trials, then | 474 // If using shared memory to pass around the list of field trials, then |
473 // expects |field_trial_handle_switch| command line argument to | 475 // expects |field_trial_handle_switch| command line argument to |
474 // contain the shared memory handle. | 476 // contain the shared memory handle. |
475 // If not, then create the trials as before (using the kForceFieldTrials | 477 // If not, then create the trials as before (using the kForceFieldTrials |
476 // switch). Needs the |field_trial_handle_switch| argument to be passed in | 478 // switch). Needs the |field_trial_handle_switch| argument to be passed in |
477 // since base/ can't depend on content/. | 479 // since base/ can't depend on content/. |
478 static void CreateTrialsFromCommandLine( | 480 static void CreateTrialsFromCommandLine( |
479 const base::CommandLine& cmd_line, | 481 const base::CommandLine& cmd_line, |
480 const char* field_trial_handle_switch); | 482 const char* field_trial_handle_switch); |
481 | 483 |
| 484 #if defined(OS_WIN) |
| 485 static void AppendFieldTrialHandleIfNeeded( |
| 486 base::HandlesToInheritVector& handles); |
| 487 #endif |
| 488 |
482 // Adds a switch to the command line containing the field trial state as a | 489 // Adds a switch to the command line containing the field trial state as a |
483 // string (if not using shared memory to share field trial state), or the | 490 // string (if not using shared memory to share field trial state), or the |
484 // shared memory handle + length. | 491 // shared memory handle + length. |
485 // Needs the |field_trial_handle_switch| argument to be passed in since base/ | 492 // Needs the |field_trial_handle_switch| argument to be passed in since base/ |
486 // can't depend on content/. | 493 // can't depend on content/. |
487 static std::unique_ptr<base::SharedMemory> CopyFieldTrialStateToFlags( | 494 static void CopyFieldTrialStateToFlags(const char* field_trial_handle_switch, |
488 const char* field_trial_handle_switch, | 495 base::CommandLine* cmd_line); |
489 base::CommandLine* cmd_line); | |
490 | 496 |
491 // Create a FieldTrial with the given |name| and using 100% probability for | 497 // Create a FieldTrial with the given |name| and using 100% probability for |
492 // the FieldTrial, force FieldTrial to have the same group string as | 498 // the FieldTrial, force FieldTrial to have the same group string as |
493 // |group_name|. This is commonly used in a non-browser process, to carry | 499 // |group_name|. This is commonly used in a non-browser process, to carry |
494 // randomly selected state in a browser process into this non-browser process. | 500 // randomly selected state in a browser process into this non-browser process. |
495 // It returns NULL if there is a FieldTrial that is already registered with | 501 // It returns NULL if there is a FieldTrial that is already registered with |
496 // the same |name| but has different finalized group string (|group_name|). | 502 // the same |name| but has different finalized group string (|group_name|). |
497 static FieldTrial* CreateFieldTrial(const std::string& name, | 503 static FieldTrial* CreateFieldTrial(const std::string& name, |
498 const std::string& group_name); | 504 const std::string& group_name); |
499 | 505 |
500 // Add an observer to be notified when a field trial is irrevocably committed | 506 // Add an observer to be notified when a field trial is irrevocably committed |
501 // to being part of some specific field_group (and hence the group_name is | 507 // to being part of some specific field_group (and hence the group_name is |
502 // also finalized for that field_trial). | 508 // also finalized for that field_trial). |
503 static void AddObserver(Observer* observer); | 509 static void AddObserver(Observer* observer); |
504 | 510 |
505 // Remove an observer. | 511 // Remove an observer. |
506 static void RemoveObserver(Observer* observer); | 512 static void RemoveObserver(Observer* observer); |
507 | 513 |
508 // Notify all observers that a group has been finalized for |field_trial|. | 514 // Notify all observers that a group has been finalized for |field_trial|. |
509 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); | 515 static void NotifyFieldTrialGroupSelection(FieldTrial* field_trial); |
510 | 516 |
| 517 // Called in NotifyFieldTrialGroupSelection(). Updates field_trial_allocator |
| 518 // by adding the newly-created |field_trial|. |
| 519 static void UpdateFieldTrialAllocator(FieldTrial* field_trial); |
| 520 |
511 // Return the number of active field trials. | 521 // Return the number of active field trials. |
512 static size_t GetFieldTrialCount(); | 522 static size_t GetFieldTrialCount(); |
513 | 523 |
514 private: | 524 private: |
| 525 // Expects a mapped piece of shared memory |shm| that was created from the |
| 526 // browser process's field_trial_allocator and shared via the command line. |
| 527 // This function recreates the allocator, iterates through all the field |
| 528 // trials in it, and creates them via CreateFieldTrial(). |
| 529 static void CreateTrialsFromSharedMemory( |
| 530 std::unique_ptr<base::SharedMemory> shm); |
| 531 |
| 532 // Allocator used to instantiate field trial in child processes. In the |
| 533 // future, we may want to move this to a more generic place if we want to |
| 534 // start passing more data other than field trials. |
| 535 static base::SharedPersistentMemoryAllocator* field_trial_allocator; |
| 536 |
515 // A map from FieldTrial names to the actual instances. | 537 // A map from FieldTrial names to the actual instances. |
516 typedef std::map<std::string, FieldTrial*> RegistrationMap; | 538 typedef std::map<std::string, FieldTrial*> RegistrationMap; |
517 | 539 |
518 // If one-time randomization is enabled, returns a weak pointer to the | 540 // If one-time randomization is enabled, returns a weak pointer to the |
519 // corresponding EntropyProvider. Otherwise, returns NULL. | 541 // corresponding EntropyProvider. Otherwise, returns NULL. |
520 static const FieldTrial::EntropyProvider* | 542 static const FieldTrial::EntropyProvider* |
521 GetEntropyProviderForOneTimeRandomization(); | 543 GetEntropyProviderForOneTimeRandomization(); |
522 | 544 |
523 // Helper function should be called only while holding lock_. | 545 // Helper function should be called only while holding lock_. |
524 FieldTrial* PreLockedFind(const std::string& name); | 546 FieldTrial* PreLockedFind(const std::string& name); |
(...skipping 23 matching lines...) Expand all Loading... |
548 | 570 |
549 // List of observers to be notified when a group is selected for a FieldTrial. | 571 // List of observers to be notified when a group is selected for a FieldTrial. |
550 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; | 572 scoped_refptr<ObserverListThreadSafe<Observer> > observer_list_; |
551 | 573 |
552 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); | 574 DISALLOW_COPY_AND_ASSIGN(FieldTrialList); |
553 }; | 575 }; |
554 | 576 |
555 } // namespace base | 577 } // namespace base |
556 | 578 |
557 #endif // BASE_METRICS_FIELD_TRIAL_H_ | 579 #endif // BASE_METRICS_FIELD_TRIAL_H_ |
OLD | NEW |