ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / chromeos / login / chrome_restart_request.cc
blobb2eae7589e73254707c11b1e49808090b9aca058
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::kDisableGpuShaderDiskCache,
81 ::switches::kDisableGpuWatchdog,
82 ::switches::kDisableGpuCompositing,
83 ::switches::kDisableGpuRasterization,
84 ::switches::kDisableLowResTiling,
85 ::switches::kDisableMediaSource,
86 ::switches::kDisableOneCopy,
87 ::switches::kDisablePersistentGpuMemoryBuffer,
88 ::switches::kDisablePreferCompositingToLCDText,
89 ::switches::kDisablePrefixedEncryptedMedia,
90 ::switches::kDisablePanelFitting,
91 ::switches::kDisableRGBA4444Textures,
92 ::switches::kDisableSeccompFilterSandbox,
93 ::switches::kDisableSetuidSandbox,
94 ::switches::kDisableSlimmingPaint,
95 ::switches::kDisableSurfaces,
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::kEnableGpuMemoryBufferVideoFrames,
109 ::switches::kEnableGpuRasterization,
110 ::switches::kEnableImageColorProfiles,
111 ::switches::kEnableLogging,
112 ::switches::kEnableLowResTiling,
113 ::switches::kEnablePersistentGpuMemoryBuffer,
114 ::switches::kEnablePinch,
115 ::switches::kEnablePreferCompositingToLCDText,
116 ::switches::kEnablePluginPlaceholderShadowDom,
117 ::switches::kEnableRGBA4444Textures,
118 ::switches::kEnableSlimmingPaint,
119 ::switches::kEnableSlimmingPaintV2,
120 ::switches::kEnableTouchDragDrop,
121 ::switches::kEnableTouchEditing,
122 ::switches::kEnableViewport,
123 ::switches::kEnableViewportMeta,
124 ::switches::kEnableZeroCopy,
125 #if defined(USE_OZONE)
126 ::switches::kExtraTouchNoiseFiltering,
127 #endif
128 ::switches::kMainFrameResizesAreOrientationChanges,
129 ::switches::kForceDeviceScaleFactor,
130 ::switches::kForceGpuRasterization,
131 ::switches::kGpuRasterizationMSAASampleCount,
132 ::switches::kGpuStartupDialog,
133 ::switches::kGpuSandboxAllowSysVShm,
134 ::switches::kGpuSandboxFailuresFatal,
135 ::switches::kGpuSandboxStartEarly,
136 ::switches::kNoSandbox,
137 ::switches::kNumRasterThreads,
138 ::switches::kPpapiFlashArgs,
139 ::switches::kPpapiFlashPath,
140 ::switches::kPpapiFlashVersion,
141 ::switches::kPpapiInProcess,
142 ::switches::kRendererStartupDialog,
143 ::switches::kRootLayerScrolls,
144 ::switches::kEnableShareGroupAsyncTextureUpload,
145 ::switches::kTabCaptureUpscaleQuality,
146 ::switches::kTabCaptureDownscaleQuality,
147 #if defined(USE_X11) || defined(USE_OZONE)
148 ::switches::kTouchCalibration,
149 #endif
150 ::switches::kTouchDevices,
151 ::switches::kTouchEvents,
152 #if defined(ENABLE_TOPCHROME_MD)
153 ::switches::kTopChromeMD,
154 #endif
155 ::switches::kUIEnableCompositorAnimationTimelines,
156 ::switches::kUIPrioritizeInGpuProcess,
157 #if defined(USE_CRAS)
158 ::switches::kUseCras,
159 #endif
160 ::switches::kUseGL,
161 ::switches::kUseNormalPriorityForTileTaskWorkerThreads,
162 ::switches::kUserDataDir,
163 ::switches::kV,
164 ::switches::kVModule,
165 ::switches::kEnableWebGLDraftExtensions,
166 ::switches::kEnableWebGLImageChromium,
167 ::switches::kEnableWebVR,
168 #if defined(ENABLE_WEBRTC)
169 ::switches::kDisableWebRtcHWDecoding,
170 ::switches::kDisableWebRtcHWEncoding,
171 ::switches::kEnableWebRtcHWH264Encoding,
172 #endif
173 ::switches::kDisableVaapiAcceleratedVideoEncode,
174 #if defined(USE_OZONE)
175 ::switches::kOzoneInitialDisplayBounds,
176 ::switches::kOzoneInitialDisplayPhysicalSizeMm,
177 ::switches::kOzonePlatform,
178 ::switches::kOzoneUseSurfaceless,
179 #endif
180 app_list::switches::kDisableSyncAppList,
181 app_list::switches::kEnableCenteredAppList,
182 app_list::switches::kEnableSyncAppList,
183 ash::switches::kAshEnablePowerButtonQuickLock,
184 ash::switches::kAshEnableUnifiedDesktop,
185 ash::switches::kAshHostWindowBounds,
186 ash::switches::kAshTouchHud,
187 ash::switches::kAuraLegacyPowerButton,
188 chromeos::switches::kDefaultWallpaperLarge,
189 chromeos::switches::kDefaultWallpaperSmall,
190 chromeos::switches::kGuestWallpaperLarge,
191 chromeos::switches::kGuestWallpaperSmall,
192 // Please keep these in alphabetical order. Non-UI Compositor switches
193 // here should also be added to
194 // content/browser/renderer_host/render_process_host_impl.cc.
195 cc::switches::kCompositeToMailbox,
196 cc::switches::kDisableCompositedAntialiasing,
197 cc::switches::kDisableMainFrameBeforeActivation,
198 cc::switches::kDisableThreadedAnimation,
199 cc::switches::kEnableBeginFrameScheduling,
200 cc::switches::kEnableGpuBenchmarking,
201 cc::switches::kEnablePropertyTreeVerification,
202 cc::switches::kEnableMainFrameBeforeActivation,
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::kLoginProfile,
220 chromeos::switches::kNaturalScrollDefault,
221 chromeos::switches::kSystemInDevMode,
222 policy::switches::kDeviceManagementUrl,
223 wm::switches::kWindowAnimationsDisabled,
225 command_line->CopySwitchesFrom(base_command_line,
226 kForwardSwitches,
227 arraysize(kForwardSwitches));
229 if (start_url.is_valid())
230 command_line->AppendArg(start_url.spec());
232 for (base::DictionaryValue::Iterator it(new_switches);
233 !it.IsAtEnd();
234 it.Advance()) {
235 std::string value;
236 CHECK(it.value().GetAsString(&value));
237 command_line->AppendSwitchASCII(it.key(), value);
241 // Simulates a session manager restart by launching give command line
242 // and exit current process.
243 void ReLaunch(const base::CommandLine& command_line) {
244 base::LaunchProcess(command_line.argv(), base::LaunchOptions());
245 chrome::AttemptUserExit();
248 // Empty function that run by the local state task runner to ensure last
249 // commit goes through.
250 void EnsureLocalStateIsWritten() {}
252 // Wraps the work of sending chrome restart request to session manager.
253 // If local state is present, try to commit it first. The request is fired when
254 // the commit goes through or some time (3 seconds) has elapsed.
255 class ChromeRestartRequest
256 : public base::SupportsWeakPtr<ChromeRestartRequest> {
257 public:
258 explicit ChromeRestartRequest(const std::vector<std::string>& argv);
259 ~ChromeRestartRequest();
261 // Starts the request.
262 void Start();
264 private:
265 // Fires job restart request to session manager.
266 void RestartJob();
268 const std::vector<std::string> argv_;
269 base::OneShotTimer<ChromeRestartRequest> timer_;
271 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest);
274 ChromeRestartRequest::ChromeRestartRequest(const std::vector<std::string>& argv)
275 : argv_(argv) {}
277 ChromeRestartRequest::~ChromeRestartRequest() {}
279 void ChromeRestartRequest::Start() {
280 VLOG(1) << "Requesting a restart with command line: "
281 << base::JoinString(argv_, " ");
283 // Session Manager may kill the chrome anytime after this point.
284 // Write exit_cleanly and other stuff to the disk here.
285 g_browser_process->EndSession();
287 PrefService* local_state = g_browser_process->local_state();
288 if (!local_state) {
289 RestartJob();
290 return;
293 // XXX: normally this call must not be needed, however RestartJob
294 // just kills us so settings may be lost. See http://crosbug.com/13102
295 local_state->CommitPendingWrite();
296 timer_.Start(
297 FROM_HERE, base::TimeDelta::FromSeconds(3), this,
298 &ChromeRestartRequest::RestartJob);
300 // Post a task to local state task runner thus it occurs last on the task
301 // queue, so it would be executed after committing pending write on that
302 // thread.
303 scoped_refptr<base::SequencedTaskRunner> local_state_task_runner =
304 JsonPrefStore::GetTaskRunnerForFile(
305 base::FilePath(chrome::kLocalStorePoolName),
306 BrowserThread::GetBlockingPool());
307 local_state_task_runner->PostTaskAndReply(
308 FROM_HERE,
309 base::Bind(&EnsureLocalStateIsWritten),
310 base::Bind(&ChromeRestartRequest::RestartJob, AsWeakPtr()));
313 void ChromeRestartRequest::RestartJob() {
314 DCHECK_CURRENTLY_ON(BrowserThread::UI);
316 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(argv_);
318 delete this;
321 } // namespace
323 void GetOffTheRecordCommandLine(const GURL& start_url,
324 bool is_oobe_completed,
325 const base::CommandLine& base_command_line,
326 base::CommandLine* command_line) {
327 base::DictionaryValue otr_switches;
328 otr_switches.SetString(switches::kGuestSession, std::string());
329 otr_switches.SetString(::switches::kIncognito, std::string());
330 otr_switches.SetString(::switches::kLoggingLevel, kGuestModeLoggingLevel);
331 otr_switches.SetString(switches::kLoginUser, chromeos::login::kGuestUserName);
333 // Override the home page.
334 otr_switches.SetString(::switches::kHomePage,
335 GURL(chrome::kChromeUINewTabURL).spec());
337 // If OOBE is not finished yet, lock down the guest session to not allow
338 // surfing the web. Guest mode is still useful to inspect logs and run network
339 // diagnostics.
340 if (!is_oobe_completed)
341 otr_switches.SetString(switches::kOobeGuestSession, std::string());
343 DeriveCommandLine(start_url, base_command_line, otr_switches, command_line);
346 void RestartChrome(const base::CommandLine& command_line) {
347 DCHECK_CURRENTLY_ON(BrowserThread::UI);
348 BootTimesRecorder::Get()->set_restart_requested();
350 static bool restart_requested = false;
351 if (restart_requested) {
352 NOTREACHED() << "Request chrome restart for more than once.";
354 restart_requested = true;
356 if (!base::SysInfo::IsRunningOnChromeOS()) {
357 // Relaunch chrome without session manager on dev box.
358 ReLaunch(command_line);
359 return;
362 // ChromeRestartRequest deletes itself after request sent to session manager.
363 (new ChromeRestartRequest(command_line.argv()))->Start();
366 } // namespace chromeos