• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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 "components/prefs/command_line_pref_store.h"
6 
7 #include <memory>
8 #include <string>
9 
10 #include "base/command_line.h"
11 #include "base/containers/span.h"
12 #include "base/files/file_path.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_util.h"
16 #include "base/values.h"
17 
CommandLinePrefStore(const base::CommandLine * command_line)18 CommandLinePrefStore::CommandLinePrefStore(
19     const base::CommandLine* command_line)
20     : command_line_(command_line) {}
21 
22 CommandLinePrefStore::~CommandLinePrefStore() = default;
23 
ApplyStringSwitches(base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry> string_switch_map)24 void CommandLinePrefStore::ApplyStringSwitches(
25     base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry>
26         string_switch_map) {
27   for (const auto& entry : string_switch_map) {
28     if (command_line_->HasSwitch(entry.switch_name)) {
29       SetValue(
30           entry.preference_path,
31           base::Value(command_line_->GetSwitchValueASCII(entry.switch_name)),
32           WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
33     }
34   }
35 }
36 
ApplyPathSwitches(base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry> path_switch_map)37 void CommandLinePrefStore::ApplyPathSwitches(
38     base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry>
39         path_switch_map) {
40   for (const auto& entry : path_switch_map) {
41     if (command_line_->HasSwitch(entry.switch_name)) {
42       SetValue(entry.preference_path,
43                base::Value(command_line_->GetSwitchValuePath(entry.switch_name)
44                                .AsUTF8Unsafe()),
45                WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
46     }
47   }
48 }
49 
ApplyIntegerSwitches(base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry> integer_switch_map)50 void CommandLinePrefStore::ApplyIntegerSwitches(
51     base::span<const CommandLinePrefStore::SwitchToPreferenceMapEntry>
52         integer_switch_map) {
53   for (const auto& entry : integer_switch_map) {
54     if (command_line_->HasSwitch(entry.switch_name)) {
55       std::string str_value =
56           command_line_->GetSwitchValueASCII(entry.switch_name);
57       int int_value = 0;
58       if (!base::StringToInt(str_value, &int_value)) {
59         LOG(ERROR) << "The value " << str_value << " of " << entry.switch_name
60                    << " can not be converted to integer, ignoring!";
61         continue;
62       }
63       SetValue(entry.preference_path, base::Value(int_value),
64                WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
65     }
66   }
67 }
68 
ApplyBooleanSwitches(base::span<const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry> boolean_switch_map)69 void CommandLinePrefStore::ApplyBooleanSwitches(
70     base::span<const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry>
71         boolean_switch_map) {
72   for (const auto& entry : boolean_switch_map) {
73     if (command_line_->HasSwitch(entry.switch_name)) {
74       SetValue(entry.preference_path, base::Value(entry.set_value),
75                WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
76     }
77   }
78 }
79