Elim cr-checkbox
[chromium-blink-merge.git] / chrome / browser / chromeos / login / chrome_restart_request.cc
blobabe46988745473c53f14be29064058aa6a1ff207
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"
7 #include <vector>
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/string_util.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/boot_times_recorder.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 "chromeos/login/user_names.h"
34 #include "components/policy/core/common/policy_switches.h"
35 #include "content/public/browser/browser_thread.h"
36 #include "content/public/common/content_switches.h"
37 #include "gpu/command_buffer/service/gpu_switches.h"
38 #include "media/base/media_switches.h"
39 #include "third_party/cros_system_api/switches/chrome_switches.h"
40 #include "ui/app_list/app_list_switches.h"
41 #include "ui/base/ui_base_switches.h"
42 #include "ui/compositor/compositor_switches.h"
43 #include "ui/events/event_switches.h"
44 #include "ui/gfx/switches.h"
45 #include "ui/gl/gl_switches.h"
46 #include "ui/ozone/public/ozone_switches.h"
47 #include "ui/wm/core/wm_core_switches.h"
48 #include "url/gurl.h"
50 using content::BrowserThread;
52 namespace chromeos {
54 namespace {
56 // Increase logging level for Guest mode to avoid INFO messages in logs.
57 const char kGuestModeLoggingLevel[] = "1";
59 // Derives the new command line from |base_command_line| by doing the following:
60 // - Forward a given switches list to new command;
61 // - Set start url if given;
62 // - Append/override switches using |new_switches|;
63 void DeriveCommandLine(const GURL& start_url,
64 const base::CommandLine& base_command_line,
65 const base::DictionaryValue& new_switches,
66 base::CommandLine* command_line) {
67 DCHECK_NE(&base_command_line, command_line);
69 static const char* const kForwardSwitches[] = {
70 ::switches::kBlinkSettings,
71 ::switches::kDisableAccelerated2dCanvas,
72 ::switches::kDisableAcceleratedJpegDecoding,
73 ::switches::kDisableAcceleratedMjpegDecode,
74 ::switches::kDisableAcceleratedVideoDecode,
75 ::switches::kDisableBlinkFeatures,
76 ::switches::kDisableCastStreamingHWEncoding,
77 ::switches::kDisableDelegatedRenderer,
78 ::switches::kDisableDistanceFieldText,
79 ::switches::kDisableGpu,
80 ::switches::kDisableGpuMemoryBufferVideoFrames,
81 ::switches::kDisableGpuShaderDiskCache,
82 ::switches::kDisableGpuWatchdog,
83 ::switches::kDisableGpuCompositing,
84 ::switches::kDisableGpuRasterization,
85 ::switches::kDisableLowResTiling,
86 ::switches::kDisableMediaSource,
87 ::switches::kDisablePersistentGpuMemoryBuffer,
88 ::switches::kDisablePreferCompositingToLCDText,
89 ::switches::kEnablePrefixedEncryptedMedia,
90 ::switches::kDisablePanelFitting,
91 ::switches::kDisableRGBA4444Textures,
92 ::switches::kDisableSeccompFilterSandbox,
93 ::switches::kDisableSetuidSandbox,
94 ::switches::kDisableSurfaces,
95 ::switches::kDisableThreadedScrolling,
96 ::switches::kDisableTouchDragDrop,
97 ::switches::kDisableTouchEditing,
98 ::switches::kEnableBlinkFeatures,
99 ::switches::kEnableCompositorAnimationTimelines,
100 ::switches::kEnableDelegatedRenderer,
101 ::switches::kDisableDisplayList2dCanvas,
102 ::switches::kEnableDisplayList2dCanvas,
103 ::switches::kForceDisplayList2dCanvas,
104 ::switches::kDisableEncryptedMedia,
105 ::switches::kDisableGpuSandbox,
106 ::switches::kEnableDistanceFieldText,
107 ::switches::kEnableGpuMemoryBufferVideoFrames,
108 ::switches::kEnableGpuRasterization,
109 ::switches::kEnableImageColorProfiles,
110 ::switches::kEnableLogging,
111 ::switches::kEnableLowResTiling,
112 ::switches::kEnablePersistentGpuMemoryBuffer,
113 ::switches::kEnablePinch,
114 ::switches::kEnablePreferCompositingToLCDText,
115 ::switches::kEnableRGBA4444Textures,
116 ::switches::kEnableSlimmingPaintV2,
117 ::switches::kEnableTouchDragDrop,
118 ::switches::kEnableTouchEditing,
119 ::switches::kEnableViewport,
120 ::switches::kEnableZeroCopy,
121 #if defined(USE_OZONE)
122 ::switches::kExtraTouchNoiseFiltering,
123 #endif
124 ::switches::kMainFrameResizesAreOrientationChanges,
125 ::switches::kForceDeviceScaleFactor,
126 ::switches::kForceGpuRasterization,
127 ::switches::kGpuRasterizationMSAASampleCount,
128 ::switches::kGpuStartupDialog,
129 ::switches::kGpuSandboxAllowSysVShm,
130 ::switches::kGpuSandboxFailuresFatal,
131 ::switches::kGpuSandboxStartEarly,
132 ::switches::kNoSandbox,
133 ::switches::kNumRasterThreads,
134 ::switches::kPpapiFlashArgs,
135 ::switches::kPpapiFlashPath,
136 ::switches::kPpapiFlashVersion,
137 ::switches::kPpapiInProcess,
138 ::switches::kRendererStartupDialog,
139 ::switches::kRootLayerScrolls,
140 ::switches::kEnableShareGroupAsyncTextureUpload,
141 ::switches::kTabCaptureUpscaleQuality,
142 ::switches::kTabCaptureDownscaleQuality,
143 #if defined(USE_X11) || defined(USE_OZONE)
144 ::switches::kTouchCalibration,
145 #endif
146 ::switches::kTouchDevices,
147 ::switches::kTouchEvents,
148 #if defined(ENABLE_TOPCHROME_MD)
149 ::switches::kTopChromeMD,
150 #endif
151 ::switches::kUIEnableCompositorAnimationTimelines,
152 ::switches::kUIPrioritizeInGpuProcess,
153 #if defined(USE_CRAS)
154 ::switches::kUseCras,
155 #endif
156 ::switches::kUseGL,
157 ::switches::kUseNormalPriorityForTileTaskWorkerThreads,
158 ::switches::kUserDataDir,
159 ::switches::kV,
160 ::switches::kVModule,
161 ::switches::kEnableWebGLDraftExtensions,
162 ::switches::kEnableWebGLImageChromium,
163 ::switches::kEnableWebVR,
164 #if defined(ENABLE_WEBRTC)
165 ::switches::kDisableWebRtcHWDecoding,
166 ::switches::kDisableWebRtcHWEncoding,
167 ::switches::kEnableWebRtcHWH264Encoding,
168 #endif
169 ::switches::kDisableVaapiAcceleratedVideoEncode,
170 #if defined(USE_OZONE)
171 ::switches::kOzoneInitialDisplayBounds,
172 ::switches::kOzoneInitialDisplayPhysicalSizeMm,
173 ::switches::kOzonePlatform,
174 #endif
175 app_list::switches::kDisableSyncAppList,
176 app_list::switches::kEnableCenteredAppList,
177 app_list::switches::kEnableSyncAppList,
178 ash::switches::kAshEnablePowerButtonQuickLock,
179 ash::switches::kAshEnableUnifiedDesktop,
180 ash::switches::kAshHostWindowBounds,
181 ash::switches::kAshTouchHud,
182 ash::switches::kAuraLegacyPowerButton,
183 chromeos::switches::kDefaultWallpaperLarge,
184 chromeos::switches::kDefaultWallpaperSmall,
185 chromeos::switches::kGuestWallpaperLarge,
186 chromeos::switches::kGuestWallpaperSmall,
187 // Please keep these in alphabetical order. Non-UI Compositor switches
188 // here should also be added to
189 // content/browser/renderer_host/render_process_host_impl.cc.
190 cc::switches::kCompositeToMailbox,
191 cc::switches::kDisableCompositedAntialiasing,
192 cc::switches::kDisableMainFrameBeforeActivation,
193 cc::switches::kDisableThreadedAnimation,
194 cc::switches::kEnableBeginFrameScheduling,
195 cc::switches::kEnableGpuBenchmarking,
196 cc::switches::kEnablePropertyTreeVerification,
197 cc::switches::kEnableMainFrameBeforeActivation,
198 cc::switches::kShowCompositedLayerBorders,
199 cc::switches::kShowFPSCounter,
200 cc::switches::kShowLayerAnimationBounds,
201 cc::switches::kShowPropertyChangedRects,
202 cc::switches::kShowReplicaScreenSpaceRects,
203 cc::switches::kShowScreenSpaceRects,
204 cc::switches::kShowSurfaceDamageRects,
205 cc::switches::kSlowDownRasterScaleFactor,
206 cc::switches::kUIDisablePartialSwap,
207 chromeos::switches::kConsumerDeviceManagementUrl,
208 chromeos::switches::kDbusStub,
209 chromeos::switches::kDbusUnstubClients,
210 chromeos::switches::kDisableLoginAnimations,
211 chromeos::switches::kEnableConsumerManagement,
212 chromeos::switches::kEnterpriseEnableForcedReEnrollment,
213 chromeos::switches::kHasChromeOSDiamondKey,
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,
221 kForwardSwitches,
222 arraysize(kForwardSwitches));
224 if (start_url.is_valid())
225 command_line->AppendArg(start_url.spec());
227 for (base::DictionaryValue::Iterator it(new_switches);
228 !it.IsAtEnd();
229 it.Advance()) {
230 std::string value;
231 CHECK(it.value().GetAsString(&value));
232 command_line->AppendSwitchASCII(it.key(), value);
236 // Simulates a session manager restart by launching give command line
237 // and exit current process.
238 void ReLaunch(const base::CommandLine& command_line) {
239 base::LaunchProcess(command_line.argv(), base::LaunchOptions());
240 chrome::AttemptUserExit();
243 // Empty function that run by the local state task runner to ensure last
244 // commit goes through.
245 void EnsureLocalStateIsWritten() {}
247 // Wraps the work of sending chrome restart request to session manager.
248 // If local state is present, try to commit it first. The request is fired when
249 // the commit goes through or some time (3 seconds) has elapsed.
250 class ChromeRestartRequest
251 : public base::SupportsWeakPtr<ChromeRestartRequest> {
252 public:
253 explicit ChromeRestartRequest(const std::vector<std::string>& argv);
254 ~ChromeRestartRequest();
256 // Starts the request.
257 void Start();
259 private:
260 // Fires job restart request to session manager.
261 void RestartJob();
263 const std::vector<std::string> argv_;
264 base::OneShotTimer<ChromeRestartRequest> timer_;
266 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest);
269 ChromeRestartRequest::ChromeRestartRequest(const std::vector<std::string>& argv)
270 : argv_(argv) {}
272 ChromeRestartRequest::~ChromeRestartRequest() {}
274 void ChromeRestartRequest::Start() {
275 VLOG(1) << "Requesting a restart with command line: "
276 << base::JoinString(argv_, " ");
278 // Session Manager may kill the chrome anytime after this point.
279 // Write exit_cleanly and other stuff to the disk here.
280 g_browser_process->EndSession();
282 PrefService* local_state = g_browser_process->local_state();
283 if (!local_state) {
284 RestartJob();
285 return;
288 // XXX: normally this call must not be needed, however RestartJob
289 // just kills us so settings may be lost. See http://crosbug.com/13102
290 local_state->CommitPendingWrite();
291 timer_.Start(
292 FROM_HERE, base::TimeDelta::FromSeconds(3), this,
293 &ChromeRestartRequest::RestartJob);
295 // Post a task to local state task runner thus it occurs last on the task
296 // queue, so it would be executed after committing pending write on that
297 // thread.
298 scoped_refptr<base::SequencedTaskRunner> local_state_task_runner =
299 JsonPrefStore::GetTaskRunnerForFile(
300 base::FilePath(chrome::kLocalStorePoolName),
301 BrowserThread::GetBlockingPool());
302 local_state_task_runner->PostTaskAndReply(
303 FROM_HERE,
304 base::Bind(&EnsureLocalStateIsWritten),
305 base::Bind(&ChromeRestartRequest::RestartJob, AsWeakPtr()));
308 void ChromeRestartRequest::RestartJob() {
309 DCHECK_CURRENTLY_ON(BrowserThread::UI);
311 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(argv_);
313 delete this;
316 } // namespace
318 void GetOffTheRecordCommandLine(const GURL& start_url,
319 bool is_oobe_completed,
320 const base::CommandLine& base_command_line,
321 base::CommandLine* command_line) {
322 base::DictionaryValue otr_switches;
323 otr_switches.SetString(switches::kGuestSession, std::string());
324 otr_switches.SetString(::switches::kIncognito, std::string());
325 otr_switches.SetString(::switches::kLoggingLevel, kGuestModeLoggingLevel);
326 otr_switches.SetString(switches::kLoginUser, chromeos::login::kGuestUserName);
328 // Override the home page.
329 otr_switches.SetString(::switches::kHomePage,
330 GURL(chrome::kChromeUINewTabURL).spec());
332 // If OOBE is not finished yet, lock down the guest session to not allow
333 // surfing the web. Guest mode is still useful to inspect logs and run network
334 // diagnostics.
335 if (!is_oobe_completed)
336 otr_switches.SetString(switches::kOobeGuestSession, std::string());
338 DeriveCommandLine(start_url, base_command_line, otr_switches, command_line);
341 void RestartChrome(const base::CommandLine& command_line) {
342 DCHECK_CURRENTLY_ON(BrowserThread::UI);
343 BootTimesRecorder::Get()->set_restart_requested();
345 static bool restart_requested = false;
346 if (restart_requested) {
347 NOTREACHED() << "Request chrome restart for more than once.";
349 restart_requested = true;
351 if (!base::SysInfo::IsRunningOnChromeOS()) {
352 // Relaunch chrome without session manager on dev box.
353 ReLaunch(command_line);
354 return;
357 // ChromeRestartRequest deletes itself after request sent to session manager.
358 (new ChromeRestartRequest(command_line.argv()))->Start();
361 } // namespace chromeos