Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / chromeos / login / chrome_restart_request.cc
blobb5c1bc8b4c666570e084b661a2e7d23acc38ab83
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::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::kEnableRGBA4444Textures,
117 ::switches::kEnableSlimmingPaint,
118 ::switches::kEnableSlimmingPaintV2,
119 ::switches::kEnableTouchDragDrop,
120 ::switches::kEnableTouchEditing,
121 ::switches::kEnableViewport,
122 ::switches::kEnableZeroCopy,
123 #if defined(USE_OZONE)
124 ::switches::kExtraTouchNoiseFiltering,
125 #endif
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,
147 #endif
148 ::switches::kTouchDevices,
149 ::switches::kTouchEvents,
150 #if defined(ENABLE_TOPCHROME_MD)
151 ::switches::kTopChromeMD,
152 #endif
153 ::switches::kUIEnableCompositorAnimationTimelines,
154 ::switches::kUIPrioritizeInGpuProcess,
155 #if defined(USE_CRAS)
156 ::switches::kUseCras,
157 #endif
158 ::switches::kUseGL,
159 ::switches::kUseNormalPriorityForTileTaskWorkerThreads,
160 ::switches::kUserDataDir,
161 ::switches::kV,
162 ::switches::kVModule,
163 ::switches::kEnableWebGLDraftExtensions,
164 ::switches::kEnableWebGLImageChromium,
165 ::switches::kEnableWebVR,
166 #if defined(ENABLE_WEBRTC)
167 ::switches::kDisableWebRtcHWDecoding,
168 ::switches::kDisableWebRtcHWEncoding,
169 ::switches::kEnableWebRtcHWH264Encoding,
170 #endif
171 ::switches::kDisableVaapiAcceleratedVideoEncode,
172 #if defined(USE_OZONE)
173 ::switches::kOzoneInitialDisplayBounds,
174 ::switches::kOzoneInitialDisplayPhysicalSizeMm,
175 ::switches::kOzonePlatform,
176 ::switches::kOzoneUseSurfaceless,
177 #endif
178 app_list::switches::kDisableSyncAppList,
179 app_list::switches::kEnableCenteredAppList,
180 app_list::switches::kEnableSyncAppList,
181 ash::switches::kAshEnablePowerButtonQuickLock,
182 ash::switches::kAshEnableUnifiedDesktop,
183 ash::switches::kAshHostWindowBounds,
184 ash::switches::kAshTouchHud,
185 ash::switches::kAuraLegacyPowerButton,
186 chromeos::switches::kDefaultWallpaperLarge,
187 chromeos::switches::kDefaultWallpaperSmall,
188 chromeos::switches::kGuestWallpaperLarge,
189 chromeos::switches::kGuestWallpaperSmall,
190 // Please keep these in alphabetical order. Non-UI Compositor switches
191 // here should also be added to
192 // content/browser/renderer_host/render_process_host_impl.cc.
193 cc::switches::kCompositeToMailbox,
194 cc::switches::kDisableCompositedAntialiasing,
195 cc::switches::kDisableMainFrameBeforeActivation,
196 cc::switches::kDisableThreadedAnimation,
197 cc::switches::kEnableBeginFrameScheduling,
198 cc::switches::kEnableGpuBenchmarking,
199 cc::switches::kEnablePropertyTreeVerification,
200 cc::switches::kEnableMainFrameBeforeActivation,
201 cc::switches::kShowCompositedLayerBorders,
202 cc::switches::kShowFPSCounter,
203 cc::switches::kShowLayerAnimationBounds,
204 cc::switches::kShowPropertyChangedRects,
205 cc::switches::kShowReplicaScreenSpaceRects,
206 cc::switches::kShowScreenSpaceRects,
207 cc::switches::kShowSurfaceDamageRects,
208 cc::switches::kSlowDownRasterScaleFactor,
209 cc::switches::kUIDisablePartialSwap,
210 chromeos::switches::kConsumerDeviceManagementUrl,
211 chromeos::switches::kDbusStub,
212 chromeos::switches::kDbusUnstubClients,
213 chromeos::switches::kDisableLoginAnimations,
214 chromeos::switches::kEnableConsumerManagement,
215 chromeos::switches::kEnterpriseEnableForcedReEnrollment,
216 chromeos::switches::kHasChromeOSDiamondKey,
217 chromeos::switches::kLoginProfile,
218 chromeos::switches::kNaturalScrollDefault,
219 chromeos::switches::kSystemInDevMode,
220 policy::switches::kDeviceManagementUrl,
221 wm::switches::kWindowAnimationsDisabled,
223 command_line->CopySwitchesFrom(base_command_line,
224 kForwardSwitches,
225 arraysize(kForwardSwitches));
227 if (start_url.is_valid())
228 command_line->AppendArg(start_url.spec());
230 for (base::DictionaryValue::Iterator it(new_switches);
231 !it.IsAtEnd();
232 it.Advance()) {
233 std::string value;
234 CHECK(it.value().GetAsString(&value));
235 command_line->AppendSwitchASCII(it.key(), value);
239 // Simulates a session manager restart by launching give command line
240 // and exit current process.
241 void ReLaunch(const base::CommandLine& command_line) {
242 base::LaunchProcess(command_line.argv(), base::LaunchOptions());
243 chrome::AttemptUserExit();
246 // Empty function that run by the local state task runner to ensure last
247 // commit goes through.
248 void EnsureLocalStateIsWritten() {}
250 // Wraps the work of sending chrome restart request to session manager.
251 // If local state is present, try to commit it first. The request is fired when
252 // the commit goes through or some time (3 seconds) has elapsed.
253 class ChromeRestartRequest
254 : public base::SupportsWeakPtr<ChromeRestartRequest> {
255 public:
256 explicit ChromeRestartRequest(const std::vector<std::string>& argv);
257 ~ChromeRestartRequest();
259 // Starts the request.
260 void Start();
262 private:
263 // Fires job restart request to session manager.
264 void RestartJob();
266 const std::vector<std::string> argv_;
267 base::OneShotTimer<ChromeRestartRequest> timer_;
269 DISALLOW_COPY_AND_ASSIGN(ChromeRestartRequest);
272 ChromeRestartRequest::ChromeRestartRequest(const std::vector<std::string>& argv)
273 : argv_(argv) {}
275 ChromeRestartRequest::~ChromeRestartRequest() {}
277 void ChromeRestartRequest::Start() {
278 VLOG(1) << "Requesting a restart with command line: "
279 << base::JoinString(argv_, " ");
281 // Session Manager may kill the chrome anytime after this point.
282 // Write exit_cleanly and other stuff to the disk here.
283 g_browser_process->EndSession();
285 PrefService* local_state = g_browser_process->local_state();
286 if (!local_state) {
287 RestartJob();
288 return;
291 // XXX: normally this call must not be needed, however RestartJob
292 // just kills us so settings may be lost. See http://crosbug.com/13102
293 local_state->CommitPendingWrite();
294 timer_.Start(
295 FROM_HERE, base::TimeDelta::FromSeconds(3), this,
296 &ChromeRestartRequest::RestartJob);
298 // Post a task to local state task runner thus it occurs last on the task
299 // queue, so it would be executed after committing pending write on that
300 // thread.
301 scoped_refptr<base::SequencedTaskRunner> local_state_task_runner =
302 JsonPrefStore::GetTaskRunnerForFile(
303 base::FilePath(chrome::kLocalStorePoolName),
304 BrowserThread::GetBlockingPool());
305 local_state_task_runner->PostTaskAndReply(
306 FROM_HERE,
307 base::Bind(&EnsureLocalStateIsWritten),
308 base::Bind(&ChromeRestartRequest::RestartJob, AsWeakPtr()));
311 void ChromeRestartRequest::RestartJob() {
312 DCHECK_CURRENTLY_ON(BrowserThread::UI);
314 DBusThreadManager::Get()->GetSessionManagerClient()->RestartJob(argv_);
316 delete this;
319 } // namespace
321 void GetOffTheRecordCommandLine(const GURL& start_url,
322 bool is_oobe_completed,
323 const base::CommandLine& base_command_line,
324 base::CommandLine* command_line) {
325 base::DictionaryValue otr_switches;
326 otr_switches.SetString(switches::kGuestSession, std::string());
327 otr_switches.SetString(::switches::kIncognito, std::string());
328 otr_switches.SetString(::switches::kLoggingLevel, kGuestModeLoggingLevel);
329 otr_switches.SetString(switches::kLoginUser, chromeos::login::kGuestUserName);
331 // Override the home page.
332 otr_switches.SetString(::switches::kHomePage,
333 GURL(chrome::kChromeUINewTabURL).spec());
335 // If OOBE is not finished yet, lock down the guest session to not allow
336 // surfing the web. Guest mode is still useful to inspect logs and run network
337 // diagnostics.
338 if (!is_oobe_completed)
339 otr_switches.SetString(switches::kOobeGuestSession, std::string());
341 DeriveCommandLine(start_url, base_command_line, otr_switches, command_line);
344 void RestartChrome(const base::CommandLine& command_line) {
345 DCHECK_CURRENTLY_ON(BrowserThread::UI);
346 BootTimesRecorder::Get()->set_restart_requested();
348 static bool restart_requested = false;
349 if (restart_requested) {
350 NOTREACHED() << "Request chrome restart for more than once.";
352 restart_requested = true;
354 if (!base::SysInfo::IsRunningOnChromeOS()) {
355 // Relaunch chrome without session manager on dev box.
356 ReLaunch(command_line);
357 return;
360 // ChromeRestartRequest deletes itself after request sent to session manager.
361 (new ChromeRestartRequest(command_line.argv()))->Start();
364 } // namespace chromeos