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/files/file_path.h"
11 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/string_util.h"
14 #include "base/values.h"
15
CommandLinePrefStore(const base::CommandLine * command_line)16 CommandLinePrefStore::CommandLinePrefStore(
17 const base::CommandLine* command_line)
18 : command_line_(command_line) {}
19
~CommandLinePrefStore()20 CommandLinePrefStore::~CommandLinePrefStore() {}
21
ApplyStringSwitches(const CommandLinePrefStore::SwitchToPreferenceMapEntry string_switch[],size_t size)22 void CommandLinePrefStore::ApplyStringSwitches(
23 const CommandLinePrefStore::SwitchToPreferenceMapEntry string_switch[],
24 size_t size) {
25 for (size_t i = 0; i < size; ++i) {
26 if (command_line_->HasSwitch(string_switch[i].switch_name)) {
27 SetValue(string_switch[i].preference_path,
28 base::Value(command_line_->GetSwitchValueASCII(
29 string_switch[i].switch_name)),
30 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
31 }
32 }
33 }
34
ApplyPathSwitches(const CommandLinePrefStore::SwitchToPreferenceMapEntry path_switch[],size_t size)35 void CommandLinePrefStore::ApplyPathSwitches(
36 const CommandLinePrefStore::SwitchToPreferenceMapEntry path_switch[],
37 size_t size) {
38 for (size_t i = 0; i < size; ++i) {
39 if (command_line_->HasSwitch(path_switch[i].switch_name)) {
40 SetValue(path_switch[i].preference_path,
41 base::Value(
42 command_line_->GetSwitchValuePath(path_switch[i].switch_name)
43 .AsUTF8Unsafe()),
44 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
45 }
46 }
47 }
48
ApplyIntegerSwitches(const CommandLinePrefStore::SwitchToPreferenceMapEntry integer_switch[],size_t size)49 void CommandLinePrefStore::ApplyIntegerSwitches(
50 const CommandLinePrefStore::SwitchToPreferenceMapEntry integer_switch[],
51 size_t size) {
52 for (size_t i = 0; i < size; ++i) {
53 if (command_line_->HasSwitch(integer_switch[i].switch_name)) {
54 std::string str_value = command_line_->GetSwitchValueASCII(
55 integer_switch[i].switch_name);
56 int int_value = 0;
57 if (!base::StringToInt(str_value, &int_value)) {
58 LOG(ERROR) << "The value " << str_value << " of "
59 << integer_switch[i].switch_name
60 << " can not be converted to integer, ignoring!";
61 continue;
62 }
63 SetValue(integer_switch[i].preference_path, base::Value(int_value),
64 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
65 }
66 }
67 }
68
ApplyBooleanSwitches(const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry boolean_switch_map[],size_t size)69 void CommandLinePrefStore::ApplyBooleanSwitches(
70 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
71 boolean_switch_map[], size_t size) {
72 for (size_t i = 0; i < size; ++i) {
73 if (command_line_->HasSwitch(boolean_switch_map[i].switch_name)) {
74 SetValue(boolean_switch_map[i].preference_path,
75 base::Value(boolean_switch_map[i].set_value),
76 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
77 }
78 }
79 }
80