[go: nahoru, domu]

blob: 7cd1e0421daa0625330f4e9a4bb21837d9246b85 [file] [log] [blame]
davemoore@chromium.org54c8d282014-01-24 17:31:081// 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
horof157bc222015-02-17 22:40:197#include <string>
8
davemoore@chromium.org54c8d282014-01-24 17:31:089#include "base/command_line.h"
wfh6575fb42015-01-28 04:05:2110#include "base/metrics/field_trial.h"
Nate Chapin82a20b62017-12-01 17:09:2411#include "base/metrics/field_trial_params.h"
John Abd-El-Malek884291c2017-08-09 06:43:4812#include "base/strings/string_number_conversions.h"
Steve Kobes773cdbaa2017-08-19 22:03:0513#include "base/strings/string_split.h"
14#include "base/strings/string_util.h"
15#include "base/strings/stringprintf.h"
John Abd-El-Malek884291c2017-08-09 06:43:4816#include "base/strings/utf_string_conversions.h"
Nico Weber3fa78132019-07-29 21:54:3317#include "build/branding_buildflags.h"
avia9aa7a82015-12-25 03:06:3118#include "build/build_config.h"
davemoore@chromium.org54c8d282014-01-24 17:31:0819#include "content/public/common/content_switches.h"
Jaebaek Seoefa2fd962017-12-15 02:36:2920#include "content/public/common/use_zoom_for_dsf_policy.h"
Gyuyoung Kim362875922020-09-17 15:19:5221#include "third_party/blink/public/mojom/v8_cache_options.mojom.h"
davemoore@chromium.org54c8d282014-01-24 17:31:0822
John Abd-El-Malek884291c2017-08-09 06:43:4823#if defined(OS_ANDROID)
24#include "base/debug/debugger.h"
Jaebaek Seoefa2fd962017-12-15 02:36:2925#include "base/feature_list.h"
John Abd-El-Malek884291c2017-08-09 06:43:4826#endif
27
28#if defined(OS_POSIX) && !defined(OS_ANDROID)
29#include <signal.h>
30static void SigUSR1Handler(int signal) {}
31#endif
32
bokan@chromium.orgbdc06d42014-02-05 21:48:1733#if defined(OS_WIN)
34#include "base/win/windows_version.h"
35#endif
36
davemoore@chromium.org54c8d282014-01-24 17:31:0837namespace content {
38
wfh1a90e032015-04-14 17:10:1739namespace {
40
Steve Kobes773cdbaa2017-08-19 22:03:0541#if defined(OS_WIN)
42
Peter Kasting57fd6de42021-01-28 23:05:3943std::wstring ToNativeString(base::StringPiece string) {
44 return base::ASCIIToWide(string);
Steve Kobes773cdbaa2017-08-19 22:03:0545}
46
Peter Kasting57fd6de42021-01-28 23:05:3947std::string FromNativeString(base::WStringPiece string) {
48 return base::WideToASCII(string);
Steve Kobes773cdbaa2017-08-19 22:03:0549}
50
51#else // defined(OS_WIN)
52
53std::string ToNativeString(const std::string& string) {
54 return string;
55}
56
57std::string FromNativeString(const std::string& string) {
58 return string;
59}
60
61#endif // defined(OS_WIN)
62
wfh1a90e032015-04-14 17:10:1763} // namespace
64
bokan@chromium.orgbdc06d42014-02-05 21:48:1765bool IsPinchToZoomEnabled() {
brettw@chromium.org479278702014-08-11 20:32:0966 const base::CommandLine& command_line =
67 *base::CommandLine::ForCurrentProcess();
bokan@chromium.orgbdc06d42014-02-05 21:48:1768
bokana2e285372016-02-09 21:48:3869 // Enable pinch everywhere unless it's been explicitly disabled.
70 return !command_line.HasSwitch(switches::kDisablePinch);
bokan@chromium.orgbdc06d42014-02-05 21:48:1771}
72
Leon Hancaae6db2019-02-03 03:37:5473blink::mojom::V8CacheOptions GetV8CacheOptions() {
horof157bc222015-02-17 22:40:1974 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");
vogelheim9ef15762015-06-23 09:55:0780 if (v8_cache_options == "none") {
Leon Hancaae6db2019-02-03 03:37:5481 return blink::mojom::V8CacheOptions::kNone;
horof157bc222015-02-17 22:40:1982 } else if (v8_cache_options == "code") {
Leon Hancaae6db2019-02-03 03:37:5483 return blink::mojom::V8CacheOptions::kCode;
horof157bc222015-02-17 22:40:1984 } else {
Leon Hancaae6db2019-02-03 03:37:5485 return blink::mojom::V8CacheOptions::kDefault;
horof157bc222015-02-17 22:40:1986 }
87}
88
John Abd-El-Malek884291c2017-08-09 06:43:4889void WaitForDebugger(const std::string& label) {
90#if defined(OS_WIN)
Nico Weber3fa78132019-07-29 21:54:3391#if BUILDFLAG(GOOGLE_CHROME_BRANDING)
John Abd-El-Malek884291c2017-08-09 06:43:4892 std::string title = "Google Chrome";
Nico Weber897593f2019-07-25 23:17:5593#else // BUILDFLAG(CHROMIUM_BRANDING)
John Abd-El-Malek884291c2017-08-09 06:43:4894 std::string title = "Chromium";
Nico Weber897593f2019-07-25 23:17:5595#endif // BUILDFLAG(CHROMIUM_BRANDING)
John Abd-El-Malek884291c2017-08-09 06:43:4896 title += " ";
97 title += label; // makes attaching to process easier
98 std::string message = label;
99 message += " starting with pid: ";
Raul Tambre6c0c3f5b2019-02-04 17:44:17100 message += base::NumberToString(base::GetCurrentProcId());
John Abd-El-Malek884291c2017-08-09 06:43:48101 ::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 Kotenkov2c0d2bb32017-11-01 15:41:28119 sigaction(SIGUSR1, &sa, nullptr);
John Abd-El-Malek884291c2017-08-09 06:43:48120
121 pause();
122#endif // defined(OS_ANDROID)
123#endif // defined(OS_POSIX)
124}
125
Steve Kobes773cdbaa2017-08-19 22:03:05126std::vector<std::string> FeaturesFromSwitch(
127 const base::CommandLine& command_line,
128 const char* switch_name) {
129 using NativeString = base::CommandLine::StringType;
Jan Wilken Dörrie8ed6fce2021-03-25 23:00:38130 using NativeStringPiece = base::CommandLine::StringPieceType;
Steve Kobes773cdbaa2017-08-19 22:03:05131
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örrie4aa966b2020-08-24 18:20:29148 auto vals = SplitString(FromNativeString(NativeString(arg)), ",",
Steve Kobes773cdbaa2017-08-19 22:03:05149 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
150 features.insert(features.end(), vals.begin(), vals.end());
151 }
152 return features;
153}
154
davemoore@chromium.org54c8d282014-01-24 17:31:08155} // namespace content