OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/about_flags.h" | 5 #include "chrome/browser/about_flags.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <utility> | 10 #include <utility> |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 {kOsAndroid, "Android"}, | 112 {kOsAndroid, "Android"}, |
113 {kOsCrOSOwnerOnly, "Chrome OS (owner only)"}, | 113 {kOsCrOSOwnerOnly, "Chrome OS (owner only)"}, |
114 }; | 114 }; |
115 for (size_t i = 0; i < arraysize(kBitsToOs); ++i) { | 115 for (size_t i = 0; i < arraysize(kBitsToOs); ++i) { |
116 if (bitmask & kBitsToOs[i].bit) | 116 if (bitmask & kBitsToOs[i].bit) |
117 list->Append(new base::StringValue(kBitsToOs[i].name)); | 117 list->Append(new base::StringValue(kBitsToOs[i].name)); |
118 } | 118 } |
119 } | 119 } |
120 | 120 |
121 // Convert switch constants to proper CommandLine::StringType strings. | 121 // Convert switch constants to proper CommandLine::StringType strings. |
122 CommandLine::StringType GetSwitchString(const std::string& flag) { | 122 base::CommandLine::StringType GetSwitchString(const std::string& flag) { |
123 CommandLine cmd_line(CommandLine::NO_PROGRAM); | 123 base::CommandLine cmd_line(base::CommandLine::NO_PROGRAM); |
124 cmd_line.AppendSwitch(flag); | 124 cmd_line.AppendSwitch(flag); |
125 DCHECK_EQ(2U, cmd_line.argv().size()); | 125 DCHECK_EQ(2U, cmd_line.argv().size()); |
126 return cmd_line.argv()[1]; | 126 return cmd_line.argv()[1]; |
127 } | 127 } |
128 | 128 |
129 // Scoops flags from a command line. | 129 // Scoops flags from a command line. |
130 std::set<CommandLine::StringType> ExtractFlagsFromCommandLine( | 130 std::set<base::CommandLine::StringType> ExtractFlagsFromCommandLine( |
131 const CommandLine& cmdline) { | 131 const base::CommandLine& cmdline) { |
132 std::set<CommandLine::StringType> flags; | 132 std::set<base::CommandLine::StringType> flags; |
133 // First do the ones between --flag-switches-begin and --flag-switches-end. | 133 // First do the ones between --flag-switches-begin and --flag-switches-end. |
134 CommandLine::StringVector::const_iterator first = | 134 base::CommandLine::StringVector::const_iterator first = |
135 std::find(cmdline.argv().begin(), cmdline.argv().end(), | 135 std::find(cmdline.argv().begin(), cmdline.argv().end(), |
136 GetSwitchString(switches::kFlagSwitchesBegin)); | 136 GetSwitchString(switches::kFlagSwitchesBegin)); |
137 CommandLine::StringVector::const_iterator last = | 137 base::CommandLine::StringVector::const_iterator last = |
138 std::find(cmdline.argv().begin(), cmdline.argv().end(), | 138 std::find(cmdline.argv().begin(), cmdline.argv().end(), |
139 GetSwitchString(switches::kFlagSwitchesEnd)); | 139 GetSwitchString(switches::kFlagSwitchesEnd)); |
140 if (first != cmdline.argv().end() && last != cmdline.argv().end()) | 140 if (first != cmdline.argv().end() && last != cmdline.argv().end()) |
141 flags.insert(first + 1, last); | 141 flags.insert(first + 1, last); |
142 #if defined(OS_CHROMEOS) | 142 #if defined(OS_CHROMEOS) |
143 // Then add those between --policy-switches-begin and --policy-switches-end. | 143 // Then add those between --policy-switches-begin and --policy-switches-end. |
144 first = std::find(cmdline.argv().begin(), cmdline.argv().end(), | 144 first = std::find(cmdline.argv().begin(), cmdline.argv().end(), |
145 GetSwitchString(chromeos::switches::kPolicySwitchesBegin)); | 145 GetSwitchString(chromeos::switches::kPolicySwitchesBegin)); |
146 last = std::find(cmdline.argv().begin(), cmdline.argv().end(), | 146 last = std::find(cmdline.argv().begin(), cmdline.argv().end(), |
147 GetSwitchString(chromeos::switches::kPolicySwitchesEnd)); | 147 GetSwitchString(chromeos::switches::kPolicySwitchesEnd)); |
(...skipping 1946 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2094 }; | 2094 }; |
2095 | 2095 |
2096 const Experiment* experiments = kExperiments; | 2096 const Experiment* experiments = kExperiments; |
2097 size_t num_experiments = arraysize(kExperiments); | 2097 size_t num_experiments = arraysize(kExperiments); |
2098 | 2098 |
2099 // Stores and encapsulates the little state that about:flags has. | 2099 // Stores and encapsulates the little state that about:flags has. |
2100 class FlagsState { | 2100 class FlagsState { |
2101 public: | 2101 public: |
2102 FlagsState() : needs_restart_(false) {} | 2102 FlagsState() : needs_restart_(false) {} |
2103 void ConvertFlagsToSwitches(FlagsStorage* flags_storage, | 2103 void ConvertFlagsToSwitches(FlagsStorage* flags_storage, |
2104 CommandLine* command_line, | 2104 base::CommandLine* command_line, |
2105 SentinelsMode sentinels); | 2105 SentinelsMode sentinels); |
2106 bool IsRestartNeededToCommitChanges(); | 2106 bool IsRestartNeededToCommitChanges(); |
2107 void SetExperimentEnabled( | 2107 void SetExperimentEnabled( |
2108 FlagsStorage* flags_storage, | 2108 FlagsStorage* flags_storage, |
2109 const std::string& internal_name, | 2109 const std::string& internal_name, |
2110 bool enable); | 2110 bool enable); |
2111 void RemoveFlagsSwitches( | 2111 void RemoveFlagsSwitches( |
2112 std::map<std::string, CommandLine::StringType>* switch_list); | 2112 std::map<std::string, base::CommandLine::StringType>* switch_list); |
2113 void ResetAllFlags(FlagsStorage* flags_storage); | 2113 void ResetAllFlags(FlagsStorage* flags_storage); |
2114 void reset(); | 2114 void reset(); |
2115 | 2115 |
2116 // Returns the singleton instance of this class | 2116 // Returns the singleton instance of this class |
2117 static FlagsState* GetInstance() { | 2117 static FlagsState* GetInstance() { |
2118 return Singleton<FlagsState>::get(); | 2118 return Singleton<FlagsState>::get(); |
2119 } | 2119 } |
2120 | 2120 |
2121 private: | 2121 private: |
2122 bool needs_restart_; | 2122 bool needs_restart_; |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2284 IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED, | 2284 IDS_GENERIC_EXPERIMENT_CHOICE_DISABLED, |
2285 }; | 2285 }; |
2286 description_id = kEnableDisableDescriptionIds[index]; | 2286 description_id = kEnableDisableDescriptionIds[index]; |
2287 } else { | 2287 } else { |
2288 description_id = choices[index].description_id; | 2288 description_id = choices[index].description_id; |
2289 } | 2289 } |
2290 return l10n_util::GetStringUTF16(description_id); | 2290 return l10n_util::GetStringUTF16(description_id); |
2291 } | 2291 } |
2292 | 2292 |
2293 void ConvertFlagsToSwitches(FlagsStorage* flags_storage, | 2293 void ConvertFlagsToSwitches(FlagsStorage* flags_storage, |
2294 CommandLine* command_line, | 2294 base::CommandLine* command_line, |
2295 SentinelsMode sentinels) { | 2295 SentinelsMode sentinels) { |
2296 FlagsState::GetInstance()->ConvertFlagsToSwitches(flags_storage, | 2296 FlagsState::GetInstance()->ConvertFlagsToSwitches(flags_storage, |
2297 command_line, | 2297 command_line, |
2298 sentinels); | 2298 sentinels); |
2299 } | 2299 } |
2300 | 2300 |
2301 bool AreSwitchesIdenticalToCurrentCommandLine( | 2301 bool AreSwitchesIdenticalToCurrentCommandLine( |
2302 const CommandLine& new_cmdline, | 2302 const base::CommandLine& new_cmdline, |
2303 const CommandLine& active_cmdline, | 2303 const base::CommandLine& active_cmdline, |
2304 std::set<CommandLine::StringType>* out_difference) { | 2304 std::set<base::CommandLine::StringType>* out_difference) { |
2305 std::set<CommandLine::StringType> new_flags = | 2305 std::set<base::CommandLine::StringType> new_flags = |
2306 ExtractFlagsFromCommandLine(new_cmdline); | 2306 ExtractFlagsFromCommandLine(new_cmdline); |
2307 std::set<CommandLine::StringType> active_flags = | 2307 std::set<base::CommandLine::StringType> active_flags = |
2308 ExtractFlagsFromCommandLine(active_cmdline); | 2308 ExtractFlagsFromCommandLine(active_cmdline); |
2309 | 2309 |
2310 bool result = false; | 2310 bool result = false; |
2311 // Needed because std::equal doesn't check if the 2nd set is empty. | 2311 // Needed because std::equal doesn't check if the 2nd set is empty. |
2312 if (new_flags.size() == active_flags.size()) { | 2312 if (new_flags.size() == active_flags.size()) { |
2313 result = | 2313 result = |
2314 std::equal(new_flags.begin(), new_flags.end(), active_flags.begin()); | 2314 std::equal(new_flags.begin(), new_flags.end(), active_flags.begin()); |
2315 } | 2315 } |
2316 | 2316 |
2317 if (out_difference && !result) { | 2317 if (out_difference && !result) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2385 } | 2385 } |
2386 | 2386 |
2387 void SetExperimentEnabled(FlagsStorage* flags_storage, | 2387 void SetExperimentEnabled(FlagsStorage* flags_storage, |
2388 const std::string& internal_name, | 2388 const std::string& internal_name, |
2389 bool enable) { | 2389 bool enable) { |
2390 FlagsState::GetInstance()->SetExperimentEnabled(flags_storage, | 2390 FlagsState::GetInstance()->SetExperimentEnabled(flags_storage, |
2391 internal_name, enable); | 2391 internal_name, enable); |
2392 } | 2392 } |
2393 | 2393 |
2394 void RemoveFlagsSwitches( | 2394 void RemoveFlagsSwitches( |
2395 std::map<std::string, CommandLine::StringType>* switch_list) { | 2395 std::map<std::string, base::CommandLine::StringType>* switch_list) { |
2396 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list); | 2396 FlagsState::GetInstance()->RemoveFlagsSwitches(switch_list); |
2397 } | 2397 } |
2398 | 2398 |
2399 void ResetAllFlags(FlagsStorage* flags_storage) { | 2399 void ResetAllFlags(FlagsStorage* flags_storage) { |
2400 FlagsState::GetInstance()->ResetAllFlags(flags_storage); | 2400 FlagsState::GetInstance()->ResetAllFlags(flags_storage); |
2401 } | 2401 } |
2402 | 2402 |
2403 int GetCurrentPlatform() { | 2403 int GetCurrentPlatform() { |
2404 #if defined(OS_MACOSX) | 2404 #if defined(OS_MACOSX) |
2405 return kOsMac; | 2405 return kOsMac; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2472 | 2472 |
2473 void SetFlagToSwitchMapping(const std::string& key, | 2473 void SetFlagToSwitchMapping(const std::string& key, |
2474 const std::string& switch_name, | 2474 const std::string& switch_name, |
2475 const std::string& switch_value, | 2475 const std::string& switch_value, |
2476 NameToSwitchAndValueMap* name_to_switch_map) { | 2476 NameToSwitchAndValueMap* name_to_switch_map) { |
2477 DCHECK(name_to_switch_map->end() == name_to_switch_map->find(key)); | 2477 DCHECK(name_to_switch_map->end() == name_to_switch_map->find(key)); |
2478 (*name_to_switch_map)[key] = std::make_pair(switch_name, switch_value); | 2478 (*name_to_switch_map)[key] = std::make_pair(switch_name, switch_value); |
2479 } | 2479 } |
2480 | 2480 |
2481 void FlagsState::ConvertFlagsToSwitches(FlagsStorage* flags_storage, | 2481 void FlagsState::ConvertFlagsToSwitches(FlagsStorage* flags_storage, |
2482 CommandLine* command_line, | 2482 base::CommandLine* command_line, |
2483 SentinelsMode sentinels) { | 2483 SentinelsMode sentinels) { |
2484 if (command_line->HasSwitch(switches::kNoExperiments)) | 2484 if (command_line->HasSwitch(switches::kNoExperiments)) |
2485 return; | 2485 return; |
2486 | 2486 |
2487 std::set<std::string> enabled_experiments; | 2487 std::set<std::string> enabled_experiments; |
2488 | 2488 |
2489 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, | 2489 GetSanitizedEnabledFlagsForCurrentPlatform(flags_storage, |
2490 &enabled_experiments); | 2490 &enabled_experiments); |
2491 | 2491 |
2492 NameToSwitchAndValueMap name_to_switch_map; | 2492 NameToSwitchAndValueMap name_to_switch_map; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2610 // experiment for this choice was enabled. | 2610 // experiment for this choice was enabled. |
2611 } | 2611 } |
2612 } | 2612 } |
2613 } | 2613 } |
2614 } | 2614 } |
2615 | 2615 |
2616 flags_storage->SetFlags(enabled_experiments); | 2616 flags_storage->SetFlags(enabled_experiments); |
2617 } | 2617 } |
2618 | 2618 |
2619 void FlagsState::RemoveFlagsSwitches( | 2619 void FlagsState::RemoveFlagsSwitches( |
2620 std::map<std::string, CommandLine::StringType>* switch_list) { | 2620 std::map<std::string, base::CommandLine::StringType>* switch_list) { |
2621 for (const auto& entry : flags_switches_) | 2621 for (const auto& entry : flags_switches_) |
2622 switch_list->erase(entry.first); | 2622 switch_list->erase(entry.first); |
2623 } | 2623 } |
2624 | 2624 |
2625 void FlagsState::ResetAllFlags(FlagsStorage* flags_storage) { | 2625 void FlagsState::ResetAllFlags(FlagsStorage* flags_storage) { |
2626 needs_restart_ = true; | 2626 needs_restart_ = true; |
2627 | 2627 |
2628 std::set<std::string> no_experiments; | 2628 std::set<std::string> no_experiments; |
2629 flags_storage->SetFlags(no_experiments); | 2629 flags_storage->SetFlags(no_experiments); |
2630 } | 2630 } |
(...skipping 28 matching lines...) Expand all Loading... |
2659 } | 2659 } |
2660 | 2660 |
2661 const Experiment* GetExperiments(size_t* count) { | 2661 const Experiment* GetExperiments(size_t* count) { |
2662 *count = num_experiments; | 2662 *count = num_experiments; |
2663 return experiments; | 2663 return experiments; |
2664 } | 2664 } |
2665 | 2665 |
2666 } // namespace testing | 2666 } // namespace testing |
2667 | 2667 |
2668 } // namespace about_flags | 2668 } // namespace about_flags |
OLD | NEW |