1// Copyright 2013 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
5#include "chrome/browser/chromeos/settings/owner_flags_storage.h"
6
7#include "base/command_line.h"
8#include "base/prefs/pref_service.h"
9#include "base/values.h"
10#include "chrome/browser/about_flags.h"
11#include "chrome/browser/browser_process.h"
12#include "chrome/browser/chromeos/settings/cros_settings.h"
13#include "chrome/common/pref_names.h"
14#include "chromeos/settings/cros_settings_names.h"
15
16namespace chromeos {
17namespace about_flags {
18
19OwnerFlagsStorage::OwnerFlagsStorage(PrefService *prefs,
20                                     CrosSettings *cros_settings)
21    : ::about_flags::PrefServiceFlagsStorage(prefs),
22      cros_settings_(cros_settings) {
23  // Make this code more unit test friendly.
24  if (g_browser_process->local_state()) {
25    const base::ListValue* legacy_experiments =
26        g_browser_process->local_state()->GetList(
27            prefs::kEnabledLabsExperiments);
28    if (!legacy_experiments->empty()) {
29      // If there are any flags set in local state migrate them to the owner's
30      // prefs and device settings.
31      std::set<std::string> flags;
32      for (base::ListValue::const_iterator it = legacy_experiments->begin();
33           it != legacy_experiments->end(); ++it) {
34        std::string experiment_name;
35        if (!(*it)->GetAsString(&experiment_name)) {
36          LOG(WARNING) << "Invalid entry in " << prefs::kEnabledLabsExperiments;
37          continue;
38        }
39        flags.insert(experiment_name);
40      }
41      SetFlags(flags);
42      g_browser_process->local_state()->ClearPref(
43          prefs::kEnabledLabsExperiments);
44    }
45  }
46}
47
48OwnerFlagsStorage::~OwnerFlagsStorage() {}
49
50bool OwnerFlagsStorage::SetFlags(const std::set<std::string>& flags) {
51  PrefServiceFlagsStorage::SetFlags(flags);
52
53  base::ListValue experiments_list;
54
55  CommandLine command_line(CommandLine::NO_PROGRAM);
56  ::about_flags::ConvertFlagsToSwitches(this, &command_line,
57                                        ::about_flags::kNoSentinels);
58  CommandLine::StringVector switches = command_line.argv();
59  for (CommandLine::StringVector::const_iterator it = switches.begin() + 1;
60       it != switches.end(); ++it) {
61    experiments_list.Append(new base::StringValue(*it));
62  }
63  cros_settings_->Set(kStartUpFlags, experiments_list);
64
65  return true;
66}
67
68}  // namespace about_flags
69}  // namespace chromeos
70