blob: 79fc10c042a3a49c429823e50f4e3891f2847b8f [file] [log] [blame]
mlliuf00ece62016-10-17 17:29:011// Copyright 2016 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 "components/prefs/command_line_pref_store.h"
6
Jinho Bang84b58bd2018-01-01 21:44:487#include <memory>
mlliuf00ece62016-10-17 17:29:018#include <string>
9
10#include "base/files/file_path.h"
mlliuf00ece62016-10-17 17:29:0111#include "base/strings/string_number_conversions.h"
12
13CommandLinePrefStore::CommandLinePrefStore(
14 const base::CommandLine* command_line)
15 : command_line_(command_line) {}
16
17CommandLinePrefStore::~CommandLinePrefStore() {}
18
19void CommandLinePrefStore::ApplyStringSwitches(
20 const CommandLinePrefStore::SwitchToPreferenceMapEntry string_switch[],
21 size_t size) {
22 for (size_t i = 0; i < size; ++i) {
23 if (command_line_->HasSwitch(string_switch[i].switch_name)) {
24 SetValue(string_switch[i].preference_path,
Jinho Bang84b58bd2018-01-01 21:44:4825 std::make_unique<base::Value>(command_line_->GetSwitchValueASCII(
jdoerrie122c4da2017-03-06 11:12:0426 string_switch[i].switch_name)),
mlliuf00ece62016-10-17 17:29:0127 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
28 }
29 }
30}
31
32void CommandLinePrefStore::ApplyPathSwitches(
33 const CommandLinePrefStore::SwitchToPreferenceMapEntry path_switch[],
34 size_t size) {
35 for (size_t i = 0; i < size; ++i) {
36 if (command_line_->HasSwitch(path_switch[i].switch_name)) {
jdoerrie122c4da2017-03-06 11:12:0437 SetValue(path_switch[i].preference_path,
Jinho Bang84b58bd2018-01-01 21:44:4838 std::make_unique<base::Value>(
jdoerrie122c4da2017-03-06 11:12:0439 command_line_->GetSwitchValuePath(path_switch[i].switch_name)
40 .value()),
41 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
mlliuf00ece62016-10-17 17:29:0142 }
43 }
44}
45
46void CommandLinePrefStore::ApplyIntegerSwitches(
47 const CommandLinePrefStore::SwitchToPreferenceMapEntry integer_switch[],
48 size_t size) {
49 for (size_t i = 0; i < size; ++i) {
50 if (command_line_->HasSwitch(integer_switch[i].switch_name)) {
51 std::string str_value = command_line_->GetSwitchValueASCII(
52 integer_switch[i].switch_name);
53 int int_value = 0;
54 if (!base::StringToInt(str_value, &int_value)) {
55 LOG(ERROR) << "The value " << str_value << " of "
56 << integer_switch[i].switch_name
57 << " can not be converted to integer, ignoring!";
58 continue;
59 }
60 SetValue(integer_switch[i].preference_path,
Jinho Bang84b58bd2018-01-01 21:44:4861 std::make_unique<base::Value>(int_value),
mlliuf00ece62016-10-17 17:29:0162 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
63 }
64 }
65}
66
67void CommandLinePrefStore::ApplyBooleanSwitches(
68 const CommandLinePrefStore::BooleanSwitchToPreferenceMapEntry
69 boolean_switch_map[], size_t size) {
70 for (size_t i = 0; i < size; ++i) {
71 if (command_line_->HasSwitch(boolean_switch_map[i].switch_name)) {
72 SetValue(boolean_switch_map[i].preference_path,
Jinho Bang84b58bd2018-01-01 21:44:4873 std::make_unique<base::Value>(boolean_switch_map[i].set_value),
mlliuf00ece62016-10-17 17:29:0174 WriteablePrefStore::DEFAULT_PREF_WRITE_FLAGS);
75 }
76 }
77}