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::kDisableImplSidePainting
,
86 ::switches::kDisableLowResTiling
,
87 ::switches::kDisableMediaSource
,
88 ::switches::kDisableOneCopy
,
89 ::switches::kDisablePreferCompositingToLCDText
,
90 ::switches::kDisablePrefixedEncryptedMedia
,
91 ::switches::kDisablePanelFitting
,
92 ::switches::kDisableSeccompFilterSandbox
,
93 ::switches::kDisableSetuidSandbox
,
94 ::switches::kDisableSurfaces
,
95 ::switches::kDisableTextBlobs
,
96 ::switches::kDisableThreadedScrolling
,
97 ::switches::kDisableTouchDragDrop
,
98 ::switches::kDisableTouchEditing
,
99 ::switches::kEnableBlinkFeatures
,
100 ::switches::kEnableCompositorAnimationTimelines
,
101 ::switches::kEnableDelegatedRenderer
,
102 ::switches::kDisableDisplayList2dCanvas
,
103 ::switches::kEnableDisplayList2dCanvas
,
104 ::switches::kForceDisplayList2dCanvas
,
105 ::switches::kDisableEncryptedMedia
,
106 ::switches::kDisableGpuSandbox
,
107 ::switches::kEnableDistanceFieldText
,
108 ::switches::kEnableGpuRasterization
,
109 ::switches::kEnableImageColorProfiles
,
110 ::switches::kEnableLogging
,
111 ::switches::kEnableLowResTiling
,
112 ::switches::kEnableOneCopy
,
113 ::switches::kEnablePinch
,
114 ::switches::kEnablePreferCompositingToLCDText
,
115 ::switches::kEnablePluginPlaceholderShadowDom
,
116 ::switches::kEnableSlimmingPaint
,
117 ::switches::kEnableTouchDragDrop
,
118 ::switches::kEnableTouchEditing
,
119 ::switches::kEnableViewport
,
120 ::switches::kEnableViewportMeta
,
121 ::switches::kEnableZeroCopy
,
122 #if defined(USE_OZONE)
123 ::switches::kExtraTouchNoiseFiltering
,
125 ::switches::kMainFrameResizesAreOrientationChanges
,
126 ::switches::kForceDeviceScaleFactor
,
127 ::switches::kForceGpuRasterization
,
128 ::switches::kGpuRasterizationMSAASampleCount
,
129 ::switches::kGpuStartupDialog
,
130 ::switches::kGpuSandboxAllowSysVShm
,
131 ::switches::kGpuSandboxFailuresFatal
,
132 ::switches::kGpuSandboxStartEarly
,
133 ::switches::kNoSandbox
,
134 ::switches::kNumRasterThreads
,
135 ::switches::kPpapiFlashArgs
,
136 ::switches::kPpapiFlashPath
,
137 ::switches::kPpapiFlashVersion
,
138 ::switches::kPpapiInProcess
,
139 ::switches::kRendererStartupDialog
,
140 ::switches::kRootLayerScrolls
,
141 ::switches::kEnableShareGroupAsyncTextureUpload
,
142 ::switches::kTabCaptureUpscaleQuality
,
143 ::switches::kTabCaptureDownscaleQuality
,
144 #if defined(USE_X11) || defined(USE_OZONE)
145 ::switches::kTouchCalibration
,
147 ::switches::kTouchDevices
,
148 ::switches::kTouchEvents
,
149 ::switches::kUIDisableThreadedCompositing
,
150 ::switches::kUIEnableCompositorAnimationTimelines
,
151 ::switches::kUIPrioritizeInGpuProcess
,
152 #if defined(USE_CRAS)
153 ::switches::kUseCras
,
156 ::switches::kUseNormalPriorityForTileTaskWorkerThreads
,
157 ::switches::kUserDataDir
,
159 ::switches::kVModule
,
160 ::switches::kEnableWebGLDraftExtensions
,
161 ::switches::kEnableWebGLImageChromium
,
162 #if defined(ENABLE_WEBRTC)
163 ::switches::kDisableWebRtcHWDecoding
,
164 ::switches::kDisableWebRtcHWEncoding
,
165 ::switches::kEnableWebRtcHWH264Encoding
,
167 ::switches::kDisableVaapiAcceleratedVideoEncode
,
168 #if defined(USE_OZONE)
169 ::switches::kOzoneInitialDisplayBounds
,
170 ::switches::kOzoneInitialDisplayPhysicalSizeMm
,
171 ::switches::kOzonePlatform
,
172 ::switches::kOzoneUseSurfaceless
,
174 app_list::switches::kDisableSyncAppList
,
175 app_list::switches::kEnableCenteredAppList
,
176 app_list::switches::kEnableSyncAppList
,
177 ash::switches::kAshEnablePowerButtonQuickLock
,
178 ash::switches::kAshHostWindowBounds
,
179 ash::switches::kAshTouchHud
,
180 ash::switches::kAuraLegacyPowerButton
,
181 chromeos::switches::kDefaultWallpaperLarge
,
182 chromeos::switches::kDefaultWallpaperSmall
,
183 chromeos::switches::kGuestWallpaperLarge
,
184 chromeos::switches::kGuestWallpaperSmall
,
185 // Please keep these in alphabetical order. Non-UI Compositor switches
186 // here should also be added to
187 // content/browser/renderer_host/render_process_host_impl.cc.
188 cc::switches::kCompositeToMailbox
,
189 cc::switches::kDisableCompositedAntialiasing
,
190 cc::switches::kDisableMainFrameBeforeActivation
,
191 cc::switches::kDisableThreadedAnimation
,
192 cc::switches::kEnableBeginFrameScheduling
,
193 cc::switches::kEnableGpuBenchmarking
,
194 cc::switches::kEnablePropertyTreeVerification
,
195 cc::switches::kEnableMainFrameBeforeActivation
,
196 cc::switches::kMaxUnusedResourceMemoryUsagePercentage
,
197 cc::switches::kShowCompositedLayerBorders
,
198 cc::switches::kShowFPSCounter
,
199 cc::switches::kShowLayerAnimationBounds
,
200 cc::switches::kShowPropertyChangedRects
,
201 cc::switches::kShowReplicaScreenSpaceRects
,
202 cc::switches::kShowScreenSpaceRects
,
203 cc::switches::kShowSurfaceDamageRects
,
204 cc::switches::kSlowDownRasterScaleFactor
,
205 cc::switches::kUIDisablePartialSwap
,
206 chromeos::switches::kConsumerDeviceManagementUrl
,
207 chromeos::switches::kDbusStub
,
208 chromeos::switches::kDbusUnstubClients
,
209 chromeos::switches::kDisableLoginAnimations
,
210 chromeos::switches::kEnableConsumerManagement
,
211 chromeos::switches::kEnterpriseEnableForcedReEnrollment
,
212 chromeos::switches::kHasChromeOSDiamondKey
,
213 chromeos::switches::kHasChromeOSKeyboard
,
214 chromeos::switches::kLoginProfile
,
215 chromeos::switches::kNaturalScrollDefault
,
216 chromeos::switches::kSystemInDevMode
,
217 policy::switches::kDeviceManagementUrl
,
218 wm::switches::kWindowAnimationsDisabled
,
220 command_line
->CopySwitchesFrom(base_command_line
,
222 arraysize(kForwardSwitches
));
224 if (start_url
.is_valid())
225 command_line
->AppendArg(start_url
.spec());
227 for (base::DictionaryValue::Iterator
it(new_switches
);
231 CHECK(it
.value().GetAsString(&value
));
232 command_line
->AppendSwitchASCII(it
.key(), value
);
235 std::string cmd_line_str
= command_line
->GetCommandLineString();
236 // Special workaround for the arguments that should be quoted.
237 // Copying switches won't be needed when Guest mode won't need restart
238 // http://crosbug.com/6924
239 if (base_command_line
.HasSwitch(::switches::kRegisterPepperPlugins
)) {
240 cmd_line_str
+= base::StringPrintf(
242 ::switches::kRegisterPepperPlugins
,
243 base_command_line
.GetSwitchValueNative(
244 ::switches::kRegisterPepperPlugins
).c_str());
250 // Simulates a session manager restart by launching give command line
251 // and exit current process.
252 void ReLaunch(const std::string
& command_line
) {
253 std::vector
<std::string
> argv
;
255 // This is not a proper way to get |argv| but it's good enough for debugging.
256 base::SplitString(command_line
, ' ', &argv
);
258 base::LaunchProcess(argv
, base::LaunchOptions());
259 chrome::AttemptUserExit();
262 // Empty function that run by the local state task runner to ensure last
263 // commit goes through.
264 void EnsureLocalStateIsWritten() {}
266 // Wraps the work of sending chrome restart request to session manager.
267 // If local state is present, try to commit it first. The request is fired when
268 // the commit goes through or some time (3 seconds) has elapsed.
269 class ChromeRestartRequest
270 : public base::SupportsWeakPtr
<ChromeRestartRequest
> {
272 explicit ChromeRestartRequest(const std::string
& command_line
);
273 ~ChromeRestartRequest();
275 // Starts the request.
279 // Fires job restart request to session manager.
283 const std::string command_line_
;
284 base::OneShotTimer
<ChromeRestartRequest
> timer_
;
286 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest
);
289 ChromeRestartRequest::ChromeRestartRequest(const std::string
& command_line
)
291 command_line_(command_line
) {}
293 ChromeRestartRequest::~ChromeRestartRequest() {}
295 void ChromeRestartRequest::Start() {
296 VLOG(1) << "Requesting a restart with PID " << pid_
297 << " and command line: " << command_line_
;
299 // Session Manager may kill the chrome anytime after this point.
300 // Write exit_cleanly and other stuff to the disk here.
301 g_browser_process
->EndSession();
303 PrefService
* local_state
= g_browser_process
->local_state();
309 // XXX: normally this call must not be needed, however RestartJob
310 // just kills us so settings may be lost. See http://crosbug.com/13102
311 local_state
->CommitPendingWrite();
313 FROM_HERE
, base::TimeDelta::FromSeconds(3), this,
314 &ChromeRestartRequest::RestartJob
);
316 // Post a task to local state task runner thus it occurs last on the task
317 // queue, so it would be executed after committing pending write on that
319 scoped_refptr
<base::SequencedTaskRunner
> local_state_task_runner
=
320 JsonPrefStore::GetTaskRunnerForFile(
321 base::FilePath(chrome::kLocalStorePoolName
),
322 BrowserThread::GetBlockingPool());
323 local_state_task_runner
->PostTaskAndReply(
325 base::Bind(&EnsureLocalStateIsWritten
),
326 base::Bind(&ChromeRestartRequest::RestartJob
, AsWeakPtr()));
329 void ChromeRestartRequest::RestartJob() {
330 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
332 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(
333 pid_
, command_line_
);
340 std::string
GetOffTheRecordCommandLine(
341 const GURL
& start_url
,
342 bool is_oobe_completed
,
343 const base::CommandLine
& base_command_line
,
344 base::CommandLine
* command_line
) {
345 base::DictionaryValue otr_switches
;
346 otr_switches
.SetString(switches::kGuestSession
, std::string());
347 otr_switches
.SetString(::switches::kIncognito
, std::string());
348 otr_switches
.SetString(::switches::kLoggingLevel
, kGuestModeLoggingLevel
);
349 otr_switches
.SetString(switches::kLoginUser
, chromeos::login::kGuestUserName
);
351 // Override the home page.
352 otr_switches
.SetString(::switches::kHomePage
,
353 GURL(chrome::kChromeUINewTabURL
).spec());
355 // If OOBE is not finished yet, lock down the guest session to not allow
356 // surfing the web. Guest mode is still useful to inspect logs and run network
358 if (!is_oobe_completed
)
359 otr_switches
.SetString(switches::kOobeGuestSession
, std::string());
361 return DeriveCommandLine(start_url
,
367 void RestartChrome(const std::string
& command_line
) {
368 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
369 BootTimesRecorder::Get()->set_restart_requested();
371 static bool restart_requested
= false;
372 if (restart_requested
) {
373 NOTREACHED() << "Request chrome restart for more than once.";
375 restart_requested
= true;
377 if (!base::SysInfo::IsRunningOnChromeOS()) {
378 // Relaunch chrome without session manager on dev box.
379 ReLaunch(command_line
);
383 // ChromeRestartRequest deletes itself after request sent to session manager.
384 (new ChromeRestartRequest(command_line
))->Start();
387 } // namespace chromeos