15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/metrics/field_trial.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace variations {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Unique ID of a trial and its active group, where the name and group
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// identifiers are hashes of the trial and group name strings.
17b2df76ea8fec9e32f6f3718986dba0d95315b29cTorne (Richard Coles)struct ActiveGroupId {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  uint32 name;
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  uint32 group;
202a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns an ActiveGroupId struct for the given trial and group names.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)ActiveGroupId MakeActiveGroupId(const std::string& trial_name,
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                const std::string& group_name);
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// We need to supply a Compare class for templates since ActiveGroupId is a
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// user-defined type.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct ActiveGroupIdCompare {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool operator() (const ActiveGroupId& lhs, const ActiveGroupId& rhs) const {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The group and name fields are just SHA-1 Hashes, so we just need to treat
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // them as IDs and do a less-than comparison. We test group first, since
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // name is more likely to collide.
332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    if (lhs.group != rhs.group)
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      return lhs.group < rhs.group;
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    return lhs.name < rhs.name;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fills the supplied vector |name_group_ids| (which must be empty when called)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with unique ActiveGroupIds for each Field Trial that has a chosen group.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Field Trials for which a group has not been chosen yet are NOT returned in
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this list.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetFieldTrialActiveGroupIds(std::vector<ActiveGroupId>* name_group_ids);
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Fills the supplied vector |output| (which must be empty when called) with
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// unique string representations of ActiveGroupIds for each Field Trial that
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// has a chosen group. The strings are formatted as "<TrialName>-<GroupName>",
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// with the names as hex strings. Field Trials for which a group has not been
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// chosen yet are NOT returned in this list.
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void GetFieldTrialActiveGroupIdsAsStrings(std::vector<std::string>* output);
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Expose some functions for testing. These functions just wrap functionality
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// that is implemented above.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace testing {
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void TestGetFieldTrialActiveGroupIds(
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const base::FieldTrial::ActiveGroups& active_groups,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::vector<ActiveGroupId>* name_group_ids);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace testing
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace variations
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // COMPONENTS_VARIATIONS_ACTIVE_FIELD_TRIALS_H_
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)