davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 1 | // Copyright (c) 2014 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 "content/common/content_switches_internal.h" |
| 6 | |
horo | f157bc22 | 2015-02-17 22:40:19 | [diff] [blame] | 7 | #include <string> |
| 8 | |
davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 9 | #include "base/command_line.h" |
wfh | 6575fb4 | 2015-01-28 04:05:21 | [diff] [blame] | 10 | #include "base/metrics/field_trial.h" |
Nate Chapin | 82a20b6 | 2017-12-01 17:09:24 | [diff] [blame] | 11 | #include "base/metrics/field_trial_params.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 12 | #include "base/strings/string_number_conversions.h" |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
| 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/stringprintf.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 16 | #include "base/strings/utf_string_conversions.h" |
Nico Weber | 3fa7813 | 2019-07-29 21:54:33 | [diff] [blame] | 17 | #include "build/branding_buildflags.h" |
avi | a9aa7a8 | 2015-12-25 03:06:31 | [diff] [blame] | 18 | #include "build/build_config.h" |
davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 19 | #include "content/public/common/content_switches.h" |
Jaebaek Seo | efa2fd96 | 2017-12-15 02:36:29 | [diff] [blame] | 20 | #include "content/public/common/use_zoom_for_dsf_policy.h" |
Gyuyoung Kim | 36287592 | 2020-09-17 15:19:52 | [diff] [blame] | 21 | #include "third_party/blink/public/mojom/v8_cache_options.mojom.h" |
davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 22 | |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 23 | #if defined(OS_ANDROID) |
| 24 | #include "base/debug/debugger.h" |
Jaebaek Seo | efa2fd96 | 2017-12-15 02:36:29 | [diff] [blame] | 25 | #include "base/feature_list.h" |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 26 | #endif |
| 27 | |
| 28 | #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 29 | #include <signal.h> |
| 30 | static void SigUSR1Handler(int signal) {} |
| 31 | #endif |
| 32 | |
bokan@chromium.org | bdc06d4 | 2014-02-05 21:48:17 | [diff] [blame] | 33 | #if defined(OS_WIN) |
| 34 | #include "base/win/windows_version.h" |
| 35 | #endif |
| 36 | |
davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 37 | namespace content { |
| 38 | |
wfh | 1a90e03 | 2015-04-14 17:10:17 | [diff] [blame] | 39 | namespace { |
| 40 | |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 41 | #if defined(OS_WIN) |
| 42 | |
Peter Kasting | 57fd6de4 | 2021-01-28 23:05:39 | [diff] [blame] | 43 | std::wstring ToNativeString(base::StringPiece string) { |
| 44 | return base::ASCIIToWide(string); |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 45 | } |
| 46 | |
Peter Kasting | 57fd6de4 | 2021-01-28 23:05:39 | [diff] [blame] | 47 | std::string FromNativeString(base::WStringPiece string) { |
| 48 | return base::WideToASCII(string); |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | #else // defined(OS_WIN) |
| 52 | |
| 53 | std::string ToNativeString(const std::string& string) { |
| 54 | return string; |
| 55 | } |
| 56 | |
| 57 | std::string FromNativeString(const std::string& string) { |
| 58 | return string; |
| 59 | } |
| 60 | |
| 61 | #endif // defined(OS_WIN) |
| 62 | |
wfh | 1a90e03 | 2015-04-14 17:10:17 | [diff] [blame] | 63 | } // namespace |
| 64 | |
bokan@chromium.org | bdc06d4 | 2014-02-05 21:48:17 | [diff] [blame] | 65 | bool IsPinchToZoomEnabled() { |
brettw@chromium.org | 47927870 | 2014-08-11 20:32:09 | [diff] [blame] | 66 | const base::CommandLine& command_line = |
| 67 | *base::CommandLine::ForCurrentProcess(); |
bokan@chromium.org | bdc06d4 | 2014-02-05 21:48:17 | [diff] [blame] | 68 | |
bokan | a2e28537 | 2016-02-09 21:48:38 | [diff] [blame] | 69 | // Enable pinch everywhere unless it's been explicitly disabled. |
| 70 | return !command_line.HasSwitch(switches::kDisablePinch); |
bokan@chromium.org | bdc06d4 | 2014-02-05 21:48:17 | [diff] [blame] | 71 | } |
| 72 | |
Leon Han | caae6db | 2019-02-03 03:37:54 | [diff] [blame] | 73 | blink::mojom::V8CacheOptions GetV8CacheOptions() { |
horo | f157bc22 | 2015-02-17 22:40:19 | [diff] [blame] | 74 | const base::CommandLine& command_line = |
| 75 | *base::CommandLine::ForCurrentProcess(); |
| 76 | std::string v8_cache_options = |
| 77 | command_line.GetSwitchValueASCII(switches::kV8CacheOptions); |
| 78 | if (v8_cache_options.empty()) |
| 79 | v8_cache_options = base::FieldTrialList::FindFullName("V8CacheOptions"); |
vogelheim | 9ef1576 | 2015-06-23 09:55:07 | [diff] [blame] | 80 | if (v8_cache_options == "none") { |
Leon Han | caae6db | 2019-02-03 03:37:54 | [diff] [blame] | 81 | return blink::mojom::V8CacheOptions::kNone; |
horo | f157bc22 | 2015-02-17 22:40:19 | [diff] [blame] | 82 | } else if (v8_cache_options == "code") { |
Leon Han | caae6db | 2019-02-03 03:37:54 | [diff] [blame] | 83 | return blink::mojom::V8CacheOptions::kCode; |
horo | f157bc22 | 2015-02-17 22:40:19 | [diff] [blame] | 84 | } else { |
Leon Han | caae6db | 2019-02-03 03:37:54 | [diff] [blame] | 85 | return blink::mojom::V8CacheOptions::kDefault; |
horo | f157bc22 | 2015-02-17 22:40:19 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 89 | void WaitForDebugger(const std::string& label) { |
| 90 | #if defined(OS_WIN) |
Nico Weber | 3fa7813 | 2019-07-29 21:54:33 | [diff] [blame] | 91 | #if BUILDFLAG(GOOGLE_CHROME_BRANDING) |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 92 | std::string title = "Google Chrome"; |
Nico Weber | 897593f | 2019-07-25 23:17:55 | [diff] [blame] | 93 | #else // BUILDFLAG(CHROMIUM_BRANDING) |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 94 | std::string title = "Chromium"; |
Nico Weber | 897593f | 2019-07-25 23:17:55 | [diff] [blame] | 95 | #endif // BUILDFLAG(CHROMIUM_BRANDING) |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 96 | title += " "; |
| 97 | title += label; // makes attaching to process easier |
| 98 | std::string message = label; |
| 99 | message += " starting with pid: "; |
Raul Tambre | 6c0c3f5b | 2019-02-04 17:44:17 | [diff] [blame] | 100 | message += base::NumberToString(base::GetCurrentProcId()); |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 101 | ::MessageBox(NULL, base::UTF8ToWide(message).c_str(), |
| 102 | base::UTF8ToWide(title).c_str(), MB_OK | MB_SETFOREGROUND); |
| 103 | #elif defined(OS_POSIX) |
| 104 | #if defined(OS_ANDROID) |
| 105 | LOG(ERROR) << label << " waiting for GDB."; |
| 106 | // Wait 24 hours for a debugger to be attached to the current process. |
| 107 | base::debug::WaitForDebugger(24 * 60 * 60, true); |
| 108 | #else |
| 109 | // TODO(playmobil): In the long term, overriding this flag doesn't seem |
| 110 | // right, either use our own flag or open a dialog we can use. |
| 111 | // This is just to ease debugging in the interim. |
| 112 | LOG(ERROR) << label << " (" << getpid() |
| 113 | << ") paused waiting for debugger to attach. " |
| 114 | << "Send SIGUSR1 to unpause."; |
| 115 | // Install a signal handler so that pause can be woken. |
| 116 | struct sigaction sa; |
| 117 | memset(&sa, 0, sizeof(sa)); |
| 118 | sa.sa_handler = SigUSR1Handler; |
Ivan Kotenkov | 2c0d2bb3 | 2017-11-01 15:41:28 | [diff] [blame] | 119 | sigaction(SIGUSR1, &sa, nullptr); |
John Abd-El-Malek | 884291c | 2017-08-09 06:43:48 | [diff] [blame] | 120 | |
| 121 | pause(); |
| 122 | #endif // defined(OS_ANDROID) |
| 123 | #endif // defined(OS_POSIX) |
| 124 | } |
| 125 | |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 126 | std::vector<std::string> FeaturesFromSwitch( |
| 127 | const base::CommandLine& command_line, |
| 128 | const char* switch_name) { |
| 129 | using NativeString = base::CommandLine::StringType; |
Jan Wilken Dörrie | 8ed6fce | 2021-03-25 23:00:38 | [diff] [blame^] | 130 | using NativeStringPiece = base::CommandLine::StringPieceType; |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 131 | |
| 132 | std::vector<std::string> features; |
| 133 | if (!command_line.HasSwitch(switch_name)) |
| 134 | return features; |
| 135 | |
| 136 | // Store prefix as native string to avoid conversions for every arg. |
| 137 | // (No string copies for the args that don't match the prefix.) |
| 138 | NativeString prefix = |
| 139 | ToNativeString(base::StringPrintf("--%s=", switch_name)); |
| 140 | for (NativeStringPiece arg : command_line.argv()) { |
| 141 | // Switch names are case insensitive on Windows, but base::CommandLine has |
| 142 | // already made them lowercase when building argv(). |
| 143 | if (!StartsWith(arg, prefix, base::CompareCase::SENSITIVE)) |
| 144 | continue; |
| 145 | arg.remove_prefix(prefix.size()); |
| 146 | if (!IsStringASCII(arg)) |
| 147 | continue; |
Jan Wilken Dörrie | 4aa966b | 2020-08-24 18:20:29 | [diff] [blame] | 148 | auto vals = SplitString(FromNativeString(NativeString(arg)), ",", |
Steve Kobes | 773cdbaa | 2017-08-19 22:03:05 | [diff] [blame] | 149 | base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 150 | features.insert(features.end(), vals.begin(), vals.end()); |
| 151 | } |
| 152 | return features; |
| 153 | } |
| 154 | |
davemoore@chromium.org | 54c8d28 | 2014-01-24 17:31:08 | [diff] [blame] | 155 | } // namespace content |