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/path_service.h"
14 #include "base/prefs/json_pref_store.h"
15 #include "base/prefs/pref_service.h"
16 #include "base/process/launch.h"
17 #include "base/strings/string_split.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/sys_info.h"
20 #include "base/timer/timer.h"
21 #include "base/values.h"
22 #include "cc/base/switches.h"
23 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/chromeos/login/user_manager.h"
25 #include "chrome/browser/lifetime/application_lifetime.h"
26 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/url_constants.h"
30 #include "chromeos/chromeos_switches.h"
31 #include "chromeos/dbus/dbus_thread_manager.h"
32 #include "chromeos/dbus/session_manager_client.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 "ui/app_list/app_list_switches.h"
39 #include "ui/base/ui_base_switches.h"
40 #include "ui/compositor/compositor_switches.h"
41 #include "ui/events/event_switches.h"
42 #include "ui/gfx/switches.h"
43 #include "ui/gl/gl_switches.h"
44 #include "ui/ozone/ozone_switches.h"
45 #include "ui/wm/core/wm_core_switches.h"
48 using content::BrowserThread
;
54 // Increase logging level for Guest mode to avoid INFO messages in logs.
55 const char kGuestModeLoggingLevel
[] = "1";
57 // Format of command line switch.
58 const char kSwitchFormatString
[] = " --%s=\"%s\"";
60 // Derives the new command line from |base_command_line| by doing the following:
61 // - Forward a given switches list to new command;
62 // - Set start url if given;
63 // - Append/override switches using |new_switches|;
64 std::string
DeriveCommandLine(const GURL
& start_url
,
65 const CommandLine
& base_command_line
,
66 const base::DictionaryValue
& new_switches
,
67 CommandLine
* command_line
) {
68 DCHECK_NE(&base_command_line
, command_line
);
70 static const char* kForwardSwitches
[] = {
71 ::switches::kDisableAccelerated2dCanvas
,
72 ::switches::kDisableAcceleratedOverflowScroll
,
73 ::switches::kDisableAcceleratedVideoDecode
,
74 ::switches::kDisableBrowserPluginCompositing
,
75 ::switches::kDisableDelegatedRenderer
,
76 ::switches::kDisableFastTextAutosizing
,
77 ::switches::kDisableFiltersOverIPC
,
78 ::switches::kDisableForceCompositingMode
,
79 ::switches::kDisableGpuShaderDiskCache
,
80 ::switches::kDisableGpuWatchdog
,
81 ::switches::kDisableGpuCompositing
,
82 ::switches::kDisableGpuRasterization
,
83 ::switches::kDisableImplSidePainting
,
84 ::switches::kDisableLowResTiling
,
85 ::switches::kDisableMapImage
,
86 ::switches::kDisablePrefixedEncryptedMedia
,
87 ::switches::kDisablePanelFitting
,
88 ::switches::kDisableRepaintAfterLayout
,
89 ::switches::kDisableSeccompFilterSandbox
,
90 ::switches::kDisableSetuidSandbox
,
91 ::switches::kDisableThreadedCompositing
,
92 ::switches::kDisableTouchDragDrop
,
93 ::switches::kDisableTouchEditing
,
94 ::switches::kDisableUniversalAcceleratedOverflowScroll
,
95 ::switches::kDisableUnprefixedMediaSource
,
96 ::switches::kDisableWebKitMediaSource
,
97 ::switches::kDisableAcceleratedFixedRootBackground
,
98 ::switches::kEnableAcceleratedFixedRootBackground
,
99 ::switches::kEnableAcceleratedOverflowScroll
,
100 ::switches::kEnableBeginFrameScheduling
,
101 ::switches::kEnableCompositingForFixedPosition
,
102 ::switches::kEnableDelegatedRenderer
,
103 ::switches::kEnableEncryptedMedia
,
104 ::switches::kEnableFastTextAutosizing
,
105 ::switches::kEnableGestureTapHighlight
,
106 ::switches::kDisableGestureTapHighlight
,
107 ::switches::kDisableGpuSandbox
,
108 ::switches::kEnableDeferredFilters
,
109 ::switches::kEnableContainerCulling
,
110 ::switches::kEnableGpuRasterization
,
111 ::switches::kEnableImplSidePainting
,
112 ::switches::kEnableLogging
,
113 ::switches::kEnableLowResTiling
,
114 ::switches::kEnableMapImage
,
115 ::switches::kEnablePinch
,
116 ::switches::kEnableRepaintAfterLayout
,
117 ::switches::kEnableThreadedCompositing
,
118 ::switches::kEnableTouchDragDrop
,
119 ::switches::kEnableTouchEditing
,
120 ::switches::kEnableUniversalAcceleratedOverflowScroll
,
121 ::switches::kEnableViewport
,
122 ::switches::kEnableViewportMeta
,
123 ::switches::kMainFrameResizesAreOrientationChanges
,
124 ::switches::kForceDeviceScaleFactor
,
125 ::switches::kForceGpuRasterization
,
126 ::switches::kGpuStartupDialog
,
127 ::switches::kGpuSandboxAllowSysVShm
,
128 ::switches::kGpuSandboxFailuresFatal
,
129 ::switches::kGpuSandboxStartAfterInitialization
,
130 ::switches::kMultiProfiles
,
131 ::switches::kNoSandbox
,
132 ::switches::kNumRasterThreads
,
133 ::switches::kPpapiFlashArgs
,
134 ::switches::kPpapiFlashPath
,
135 ::switches::kPpapiFlashVersion
,
136 ::switches::kPpapiInProcess
,
137 ::switches::kRendererStartupDialog
,
138 ::switches::kEnableShareGroupAsyncTextureUpload
,
139 ::switches::kTabCaptureUpscaleQuality
,
140 ::switches::kTabCaptureDownscaleQuality
,
141 #if defined(USE_XI2_MT)
142 ::switches::kTouchCalibration
,
144 ::switches::kTouchDevices
,
145 ::switches::kTouchEvents
,
146 ::switches::kTouchOptimizedUI
,
147 ::switches::kUIDisableThreadedCompositing
,
148 ::switches::kUIPrioritizeInGpuProcess
,
149 #if defined(USE_CRAS)
150 ::switches::kUseCras
,
152 ::switches::kUseDiscardableMemory
,
154 ::switches::kUserDataDir
,
156 ::switches::kVModule
,
157 ::switches::kWebGLCommandBufferSizeKb
,
158 ::switches::kEnableWebGLDraftExtensions
,
159 #if defined(ENABLE_WEBRTC)
160 ::switches::kDisableWebRtcHWDecoding
,
161 ::switches::kDisableWebRtcHWEncoding
,
162 ::switches::kEnableAudioTrackProcessing
,
163 ::switches::kEnableWebRtcHWVp8Encoding
,
165 #if defined(USE_OZONE)
166 ::switches::kOzonePlatform
,
168 app_list::switches::kDisableSyncAppList
,
169 app_list::switches::kEnableSyncAppList
,
170 ash::switches::kAshDefaultWallpaperLarge
,
171 ash::switches::kAshDefaultWallpaperSmall
,
172 ash::switches::kAshGuestWallpaperLarge
,
173 ash::switches::kAshGuestWallpaperSmall
,
174 ash::switches::kAshHostWindowBounds
,
175 ash::switches::kAshTouchHud
,
176 ash::switches::kAuraLegacyPowerButton
,
177 // Please keep these in alphabetical order. Non-UI Compositor switches
178 // here should also be added to
179 // content/browser/renderer_host/render_process_host_impl.cc.
180 cc::switches::kCompositeToMailbox
,
181 cc::switches::kDisableCompositedAntialiasing
,
182 cc::switches::kDisableCompositorTouchHitTesting
,
183 cc::switches::kDisableMainFrameBeforeActivation
,
184 cc::switches::kDisableMainFrameBeforeDraw
,
185 cc::switches::kDisableThreadedAnimation
,
186 cc::switches::kEnableGpuBenchmarking
,
187 cc::switches::kEnablePinchVirtualViewport
,
188 cc::switches::kEnableMainFrameBeforeActivation
,
189 cc::switches::kEnableTopControlsPositionCalculation
,
190 cc::switches::kMaxTilesForInterestArea
,
191 cc::switches::kMaxUnusedResourceMemoryUsagePercentage
,
192 cc::switches::kShowCompositedLayerBorders
,
193 cc::switches::kShowFPSCounter
,
194 cc::switches::kShowLayerAnimationBounds
,
195 cc::switches::kShowNonOccludingRects
,
196 cc::switches::kShowOccludingRects
,
197 cc::switches::kShowPropertyChangedRects
,
198 cc::switches::kShowReplicaScreenSpaceRects
,
199 cc::switches::kShowScreenSpaceRects
,
200 cc::switches::kShowSurfaceDamageRects
,
201 cc::switches::kSlowDownRasterScaleFactor
,
202 cc::switches::kUIDisablePartialSwap
,
203 chromeos::switches::kDbusStub
,
204 chromeos::switches::kDisableLoginAnimations
,
205 chromeos::switches::kHasChromeOSDiamondKey
,
206 chromeos::switches::kHasChromeOSKeyboard
,
207 chromeos::switches::kLoginProfile
,
208 chromeos::switches::kNaturalScrollDefault
,
209 ::switches::kEnableBrowserTextSubpixelPositioning
,
210 ::switches::kEnableWebkitTextSubpixelPositioning
,
211 policy::switches::kDeviceManagementUrl
,
212 wm::switches::kWindowAnimationsDisabled
,
214 command_line
->CopySwitchesFrom(base_command_line
,
216 arraysize(kForwardSwitches
));
218 if (start_url
.is_valid())
219 command_line
->AppendArg(start_url
.spec());
221 for (base::DictionaryValue::Iterator
it(new_switches
);
225 CHECK(it
.value().GetAsString(&value
));
226 command_line
->AppendSwitchASCII(it
.key(), value
);
229 std::string cmd_line_str
= command_line
->GetCommandLineString();
230 // Special workaround for the arguments that should be quoted.
231 // Copying switches won't be needed when Guest mode won't need restart
232 // http://crosbug.com/6924
233 if (base_command_line
.HasSwitch(::switches::kRegisterPepperPlugins
)) {
234 cmd_line_str
+= base::StringPrintf(
236 ::switches::kRegisterPepperPlugins
,
237 base_command_line
.GetSwitchValueNative(
238 ::switches::kRegisterPepperPlugins
).c_str());
244 // Simulates a session manager restart by launching give command line
245 // and exit current process.
246 void ReLaunch(const std::string
& command_line
) {
247 std::vector
<std::string
> argv
;
249 // This is not a proper way to get |argv| but it's good enough for debugging.
250 base::SplitString(command_line
, ' ', &argv
);
252 base::LaunchProcess(argv
, base::LaunchOptions(), NULL
);
253 chrome::AttemptUserExit();
256 // Empty function that run by the local state task runner to ensure last
257 // commit goes through.
258 void EnsureLocalStateIsWritten() {}
260 // Wraps the work of sending chrome restart request to session manager.
261 // If local state is present, try to commit it first. The request is fired when
262 // the commit goes through or some time (3 seconds) has elapsed.
263 class ChromeRestartRequest
264 : public base::SupportsWeakPtr
<ChromeRestartRequest
> {
266 explicit ChromeRestartRequest(const std::string
& command_line
);
267 ~ChromeRestartRequest();
269 // Starts the request.
273 // Fires job restart request to session manager.
277 const std::string command_line_
;
278 base::OneShotTimer
<ChromeRestartRequest
> timer_
;
280 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest
);
283 ChromeRestartRequest::ChromeRestartRequest(const std::string
& command_line
)
285 command_line_(command_line
) {}
287 ChromeRestartRequest::~ChromeRestartRequest() {}
289 void ChromeRestartRequest::Start() {
290 VLOG(1) << "Requesting a restart with PID " << pid_
291 << " and command line: " << command_line_
;
293 // Session Manager may kill the chrome anytime after this point.
294 // Write exit_cleanly and other stuff to the disk here.
295 g_browser_process
->EndSession();
297 PrefService
* local_state
= g_browser_process
->local_state();
303 // XXX: normally this call must not be needed, however RestartJob
304 // just kills us so settings may be lost. See http://crosbug.com/13102
305 local_state
->CommitPendingWrite();
307 FROM_HERE
, base::TimeDelta::FromSeconds(3), this,
308 &ChromeRestartRequest::RestartJob
);
310 // Post a task to local state task runner thus it occurs last on the task
311 // queue, so it would be executed after committing pending write on that
313 scoped_refptr
<base::SequencedTaskRunner
> local_state_task_runner
=
314 JsonPrefStore::GetTaskRunnerForFile(
315 base::FilePath(chrome::kLocalStorePoolName
),
316 BrowserThread::GetBlockingPool());
317 local_state_task_runner
->PostTaskAndReply(
319 base::Bind(&EnsureLocalStateIsWritten
),
320 base::Bind(&ChromeRestartRequest::RestartJob
, AsWeakPtr()));
323 void ChromeRestartRequest::RestartJob() {
324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
326 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(
327 pid_
, command_line_
);
334 std::string
GetOffTheRecordCommandLine(
335 const GURL
& start_url
,
336 bool is_oobe_completed
,
337 const CommandLine
& base_command_line
,
338 CommandLine
* command_line
) {
339 base::DictionaryValue otr_switches
;
340 otr_switches
.SetString(switches::kGuestSession
, std::string());
341 otr_switches
.SetString(::switches::kIncognito
, std::string());
342 otr_switches
.SetString(::switches::kLoggingLevel
, kGuestModeLoggingLevel
);
343 otr_switches
.SetString(switches::kLoginUser
, UserManager::kGuestUserName
);
345 // Override the home page.
346 otr_switches
.SetString(::switches::kHomePage
,
347 GURL(chrome::kChromeUINewTabURL
).spec());
349 // If OOBE is not finished yet, lock down the guest session to not allow
350 // surfing the web. Guest mode is still useful to inspect logs and run network
352 if (!is_oobe_completed
)
353 otr_switches
.SetString(switches::kOobeGuestSession
, std::string());
355 return DeriveCommandLine(start_url
,
361 void RestartChrome(const std::string
& command_line
) {
362 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
364 static bool restart_requested
= false;
365 if (restart_requested
) {
366 NOTREACHED() << "Request chrome restart for more than once.";
368 restart_requested
= true;
370 if (!base::SysInfo::IsRunningOnChromeOS()) {
371 // Relaunch chrome without session manager on dev box.
372 ReLaunch(command_line
);
376 // ChromeRestartRequest deletes itself after request sent to session manager.
377 (new ChromeRestartRequest(command_line
))->Start();
380 } // namespace chromeos