1 // Copyright 2013 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.
5 #include "chrome/browser/chromeos/login/chrome_restart_request.h"
9 #include "ash/ash_switches.h"
10 #include "base/command_line.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/prefs/json_pref_store.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/process/launch.h"
16 #include "base/strings/string_split.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/sys_info.h"
19 #include "base/timer/timer.h"
20 #include "base/values.h"
21 #include "cc/base/switches.h"
22 #include "chrome/browser/browser_process.h"
23 #include "chrome/browser/chromeos/boot_times_recorder.h"
24 #include "chrome/browser/lifetime/application_lifetime.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/chrome_switches.h"
28 #include "chrome/common/url_constants.h"
29 #include "chromeos/chromeos_switches.h"
30 #include "chromeos/dbus/dbus_thread_manager.h"
31 #include "chromeos/dbus/session_manager_client.h"
32 #include "chromeos/login/user_names.h"
33 #include "components/policy/core/common/policy_switches.h"
34 #include "content/public/browser/browser_thread.h"
35 #include "content/public/common/content_switches.h"
36 #include "gpu/command_buffer/service/gpu_switches.h"
37 #include "media/base/media_switches.h"
38 #include "third_party/cros_system_api/switches/chrome_switches.h"
39 #include "ui/app_list/app_list_switches.h"
40 #include "ui/base/ui_base_switches.h"
41 #include "ui/compositor/compositor_switches.h"
42 #include "ui/events/event_switches.h"
43 #include "ui/gfx/switches.h"
44 #include "ui/gl/gl_switches.h"
45 #include "ui/ozone/public/ozone_switches.h"
46 #include "ui/wm/core/wm_core_switches.h"
49 using content::BrowserThread
;
55 // Increase logging level for Guest mode to avoid INFO messages in logs.
56 const char kGuestModeLoggingLevel
[] = "1";
58 // Format of command line switch.
59 const char kSwitchFormatString
[] = " --%s=\"%s\"";
61 // Derives the new command line from |base_command_line| by doing the following:
62 // - Forward a given switches list to new command;
63 // - Set start url if given;
64 // - Append/override switches using |new_switches|;
65 std::string
DeriveCommandLine(const GURL
& start_url
,
66 const base::CommandLine
& base_command_line
,
67 const base::DictionaryValue
& new_switches
,
68 base::CommandLine
* command_line
) {
69 DCHECK_NE(&base_command_line
, command_line
);
71 static const char* const kForwardSwitches
[] = {
72 ::switches::kBlinkSettings
,
73 ::switches::kDisableAccelerated2dCanvas
,
74 ::switches::kDisableAcceleratedJpegDecoding
,
75 ::switches::kDisableAcceleratedVideoDecode
,
76 ::switches::kDisableBlinkFeatures
,
77 ::switches::kDisableCastStreamingHWEncoding
,
78 ::switches::kDisableDelegatedRenderer
,
79 ::switches::kDisableDistanceFieldText
,
80 ::switches::kDisableGpu
,
81 ::switches::kDisableGpuShaderDiskCache
,
82 ::switches::kDisableGpuWatchdog
,
83 ::switches::kDisableGpuCompositing
,
84 ::switches::kDisableGpuRasterization
,
85 ::switches::kDisableLowResTiling
,
86 ::switches::kDisableMediaSource
,
87 ::switches::kDisableOneCopy
,
88 ::switches::kDisablePreferCompositingToLCDText
,
89 ::switches::kDisablePrefixedEncryptedMedia
,
90 ::switches::kDisablePanelFitting
,
91 ::switches::kDisableSeccompFilterSandbox
,
92 ::switches::kDisableSetuidSandbox
,
93 ::switches::kDisableSlimmingPaint
,
94 ::switches::kDisableSurfaces
,
95 ::switches::kDisableTextBlobs
,
96 ::switches::kDisableThreadedScrolling
,
97 ::switches::kDisableTouchDragDrop
,
98 ::switches::kDisableTouchEditing
,
99 ::switches::kEnableAcceleratedMjpegDecode
,
100 ::switches::kEnableBlinkFeatures
,
101 ::switches::kEnableCompositorAnimationTimelines
,
102 ::switches::kEnableDelegatedRenderer
,
103 ::switches::kDisableDisplayList2dCanvas
,
104 ::switches::kEnableDisplayList2dCanvas
,
105 ::switches::kForceDisplayList2dCanvas
,
106 ::switches::kDisableEncryptedMedia
,
107 ::switches::kDisableGpuSandbox
,
108 ::switches::kEnableDistanceFieldText
,
109 ::switches::kEnableGpuRasterization
,
110 ::switches::kEnableImageColorProfiles
,
111 ::switches::kEnableLogging
,
112 ::switches::kEnableLowResTiling
,
113 ::switches::kEnableOneCopy
,
114 ::switches::kEnablePinch
,
115 ::switches::kEnablePreferCompositingToLCDText
,
116 ::switches::kEnablePluginPlaceholderShadowDom
,
117 ::switches::kEnableSlimmingPaint
,
118 ::switches::kEnableTouchDragDrop
,
119 ::switches::kEnableTouchEditing
,
120 ::switches::kEnableViewport
,
121 ::switches::kEnableViewportMeta
,
122 ::switches::kEnableZeroCopy
,
123 #if defined(USE_OZONE)
124 ::switches::kExtraTouchNoiseFiltering
,
126 ::switches::kMainFrameResizesAreOrientationChanges
,
127 ::switches::kForceDeviceScaleFactor
,
128 ::switches::kForceGpuRasterization
,
129 ::switches::kGpuRasterizationMSAASampleCount
,
130 ::switches::kGpuStartupDialog
,
131 ::switches::kGpuSandboxAllowSysVShm
,
132 ::switches::kGpuSandboxFailuresFatal
,
133 ::switches::kGpuSandboxStartEarly
,
134 ::switches::kNoSandbox
,
135 ::switches::kNumRasterThreads
,
136 ::switches::kPpapiFlashArgs
,
137 ::switches::kPpapiFlashPath
,
138 ::switches::kPpapiFlashVersion
,
139 ::switches::kPpapiInProcess
,
140 ::switches::kRendererStartupDialog
,
141 ::switches::kRootLayerScrolls
,
142 ::switches::kEnableShareGroupAsyncTextureUpload
,
143 ::switches::kTabCaptureUpscaleQuality
,
144 ::switches::kTabCaptureDownscaleQuality
,
145 #if defined(USE_X11) || defined(USE_OZONE)
146 ::switches::kTouchCalibration
,
148 ::switches::kTouchDevices
,
149 ::switches::kTouchEvents
,
150 #if defined(ENABLE_TOPCHROME_MD)
151 ::switches::kTopChromeMD
,
153 ::switches::kUIDisableThreadedCompositing
,
154 ::switches::kUIEnableCompositorAnimationTimelines
,
155 ::switches::kUIPrioritizeInGpuProcess
,
156 #if defined(USE_CRAS)
157 ::switches::kUseCras
,
160 ::switches::kUseNormalPriorityForTileTaskWorkerThreads
,
161 ::switches::kUserDataDir
,
163 ::switches::kVModule
,
164 ::switches::kEnableWebGLDraftExtensions
,
165 ::switches::kEnableWebGLImageChromium
,
166 ::switches::kEnableWebVR
,
167 #if defined(ENABLE_WEBRTC)
168 ::switches::kDisableWebRtcHWDecoding
,
169 ::switches::kDisableWebRtcHWEncoding
,
170 ::switches::kEnableWebRtcHWH264Encoding
,
172 ::switches::kDisableVaapiAcceleratedVideoEncode
,
173 #if defined(USE_OZONE)
174 ::switches::kOzoneInitialDisplayBounds
,
175 ::switches::kOzoneInitialDisplayPhysicalSizeMm
,
176 ::switches::kOzonePlatform
,
177 ::switches::kOzoneUseSurfaceless
,
179 app_list::switches::kDisableSyncAppList
,
180 app_list::switches::kEnableCenteredAppList
,
181 app_list::switches::kEnableSyncAppList
,
182 ash::switches::kAshEnablePowerButtonQuickLock
,
183 ash::switches::kAshEnableUnifiedDesktop
,
184 ash::switches::kAshHostWindowBounds
,
185 ash::switches::kAshTouchHud
,
186 ash::switches::kAuraLegacyPowerButton
,
187 chromeos::switches::kDefaultWallpaperLarge
,
188 chromeos::switches::kDefaultWallpaperSmall
,
189 chromeos::switches::kGuestWallpaperLarge
,
190 chromeos::switches::kGuestWallpaperSmall
,
191 // Please keep these in alphabetical order. Non-UI Compositor switches
192 // here should also be added to
193 // content/browser/renderer_host/render_process_host_impl.cc.
194 cc::switches::kCompositeToMailbox
,
195 cc::switches::kDisableCompositedAntialiasing
,
196 cc::switches::kDisableMainFrameBeforeActivation
,
197 cc::switches::kDisableThreadedAnimation
,
198 cc::switches::kEnableBeginFrameScheduling
,
199 cc::switches::kEnableGpuBenchmarking
,
200 cc::switches::kEnablePropertyTreeVerification
,
201 cc::switches::kEnableMainFrameBeforeActivation
,
202 cc::switches::kMaxUnusedResourceMemoryUsagePercentage
,
203 cc::switches::kShowCompositedLayerBorders
,
204 cc::switches::kShowFPSCounter
,
205 cc::switches::kShowLayerAnimationBounds
,
206 cc::switches::kShowPropertyChangedRects
,
207 cc::switches::kShowReplicaScreenSpaceRects
,
208 cc::switches::kShowScreenSpaceRects
,
209 cc::switches::kShowSurfaceDamageRects
,
210 cc::switches::kSlowDownRasterScaleFactor
,
211 cc::switches::kUIDisablePartialSwap
,
212 chromeos::switches::kConsumerDeviceManagementUrl
,
213 chromeos::switches::kDbusStub
,
214 chromeos::switches::kDbusUnstubClients
,
215 chromeos::switches::kDisableLoginAnimations
,
216 chromeos::switches::kEnableConsumerManagement
,
217 chromeos::switches::kEnterpriseEnableForcedReEnrollment
,
218 chromeos::switches::kHasChromeOSDiamondKey
,
219 chromeos::switches::kHasChromeOSKeyboard
,
220 chromeos::switches::kLoginProfile
,
221 chromeos::switches::kNaturalScrollDefault
,
222 chromeos::switches::kSystemInDevMode
,
223 policy::switches::kDeviceManagementUrl
,
224 wm::switches::kWindowAnimationsDisabled
,
226 command_line
->CopySwitchesFrom(base_command_line
,
228 arraysize(kForwardSwitches
));
230 if (start_url
.is_valid())
231 command_line
->AppendArg(start_url
.spec());
233 for (base::DictionaryValue::Iterator
it(new_switches
);
237 CHECK(it
.value().GetAsString(&value
));
238 command_line
->AppendSwitchASCII(it
.key(), value
);
241 std::string cmd_line_str
= command_line
->GetCommandLineString();
242 // Special workaround for the arguments that should be quoted.
243 // Copying switches won't be needed when Guest mode won't need restart
244 // http://crosbug.com/6924
245 if (base_command_line
.HasSwitch(::switches::kRegisterPepperPlugins
)) {
246 cmd_line_str
+= base::StringPrintf(
248 ::switches::kRegisterPepperPlugins
,
249 base_command_line
.GetSwitchValueNative(
250 ::switches::kRegisterPepperPlugins
).c_str());
256 // Simulates a session manager restart by launching give command line
257 // and exit current process.
258 void ReLaunch(const std::string
& command_line
) {
259 // This is not a proper way to get |argv| but it's good enough for debugging.
260 std::vector
<std::string
> argv
= base::SplitString(
261 command_line
, " ", base::TRIM_WHITESPACE
, base::SPLIT_WANT_ALL
);
263 base::LaunchProcess(argv
, base::LaunchOptions());
264 chrome::AttemptUserExit();
267 // Empty function that run by the local state task runner to ensure last
268 // commit goes through.
269 void EnsureLocalStateIsWritten() {}
271 // Wraps the work of sending chrome restart request to session manager.
272 // If local state is present, try to commit it first. The request is fired when
273 // the commit goes through or some time (3 seconds) has elapsed.
274 class ChromeRestartRequest
275 : public base::SupportsWeakPtr
<ChromeRestartRequest
> {
277 explicit ChromeRestartRequest(const std::string
& command_line
);
278 ~ChromeRestartRequest();
280 // Starts the request.
284 // Fires job restart request to session manager.
288 const std::string command_line_
;
289 base::OneShotTimer
<ChromeRestartRequest
> timer_
;
291 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest
);
294 ChromeRestartRequest::ChromeRestartRequest(const std::string
& command_line
)
296 command_line_(command_line
) {}
298 ChromeRestartRequest::~ChromeRestartRequest() {}
300 void ChromeRestartRequest::Start() {
301 VLOG(1) << "Requesting a restart with PID " << pid_
302 << " and command line: " << command_line_
;
304 // Session Manager may kill the chrome anytime after this point.
305 // Write exit_cleanly and other stuff to the disk here.
306 g_browser_process
->EndSession();
308 PrefService
* local_state
= g_browser_process
->local_state();
314 // XXX: normally this call must not be needed, however RestartJob
315 // just kills us so settings may be lost. See http://crosbug.com/13102
316 local_state
->CommitPendingWrite();
318 FROM_HERE
, base::TimeDelta::FromSeconds(3), this,
319 &ChromeRestartRequest::RestartJob
);
321 // Post a task to local state task runner thus it occurs last on the task
322 // queue, so it would be executed after committing pending write on that
324 scoped_refptr
<base::SequencedTaskRunner
> local_state_task_runner
=
325 JsonPrefStore::GetTaskRunnerForFile(
326 base::FilePath(chrome::kLocalStorePoolName
),
327 BrowserThread::GetBlockingPool());
328 local_state_task_runner
->PostTaskAndReply(
330 base::Bind(&EnsureLocalStateIsWritten
),
331 base::Bind(&ChromeRestartRequest::RestartJob
, AsWeakPtr()));
334 void ChromeRestartRequest::RestartJob() {
335 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
337 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(
338 pid_
, command_line_
);
345 std::string
GetOffTheRecordCommandLine(
346 const GURL
& start_url
,
347 bool is_oobe_completed
,
348 const base::CommandLine
& base_command_line
,
349 base::CommandLine
* command_line
) {
350 base::DictionaryValue otr_switches
;
351 otr_switches
.SetString(switches::kGuestSession
, std::string());
352 otr_switches
.SetString(::switches::kIncognito
, std::string());
353 otr_switches
.SetString(::switches::kLoggingLevel
, kGuestModeLoggingLevel
);
354 otr_switches
.SetString(switches::kLoginUser
, chromeos::login::kGuestUserName
);
356 // Override the home page.
357 otr_switches
.SetString(::switches::kHomePage
,
358 GURL(chrome::kChromeUINewTabURL
).spec());
360 // If OOBE is not finished yet, lock down the guest session to not allow
361 // surfing the web. Guest mode is still useful to inspect logs and run network
363 if (!is_oobe_completed
)
364 otr_switches
.SetString(switches::kOobeGuestSession
, std::string());
366 return DeriveCommandLine(start_url
,
372 void RestartChrome(const std::string
& command_line
) {
373 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
374 BootTimesRecorder::Get()->set_restart_requested();
376 static bool restart_requested
= false;
377 if (restart_requested
) {
378 NOTREACHED() << "Request chrome restart for more than once.";
380 restart_requested
= true;
382 if (!base::SysInfo::IsRunningOnChromeOS()) {
383 // Relaunch chrome without session manager on dev box.
384 ReLaunch(command_line
);
388 // ChromeRestartRequest deletes itself after request sent to session manager.
389 (new ChromeRestartRequest(command_line
))->Start();
392 } // namespace chromeos