Commit 50177f5c authored by msw@chromium.org's avatar msw@chromium.org

Revert 75405 - Register Application Restart with Windows Restart Manager on...

Revert 75405 - Register Application Restart with Windows Restart Manager on browser_main startup for Windows Vista / Server 2008 and beyond. This launches Chrome and restores tabs if the computer is restarted as the result of an update. Make changes to support necessary CommandLine operations. Other minor cleanup and refactoring.

BUG=70824
TEST=Reboot from update or ::ExitWindowsEx(EWX_RESTARTAPPS... Chrome should restart and restore tabs on login. Test launching Chrome with a variety of command-line switches and arguments.

Review URL: http://codereview.chromium.org/6462024

TBR=msw@chromium.org
Review URL: http://codereview.chromium.org/6538059

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@75410 0039d316-1c4b-4281-b951-d872f2087c98
parent a5fb9b2c
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -41,59 +41,13 @@ const char kSwitchValueSeparator[] = "=";
#endif
#if defined(OS_WIN)
namespace {
// Lowercase a string. This is used to lowercase switch names.
// Is this what we really want? It seems crazy to me. I've left it in
// for backwards compatibility on Windows.
void Lowercase(std::string* parameter) {
static void Lowercase(std::string* parameter) {
transform(parameter->begin(), parameter->end(), parameter->begin(),
tolower);
}
// Quote a string if necessary, such that CommandLineToArgvW() will
// always process it as a single argument.
std::wstring WindowsStyleQuote(const std::wstring& arg) {
// We follow the quoting rules of CommandLineToArgvW.
// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
// No quoting necessary.
return arg;
}
std::wstring out;
out.push_back(L'"');
for (size_t i = 0; i < arg.size(); ++i) {
if (arg[i] == '\\') {
// Find the extent of this run of backslashes.
size_t start = i, end = start + 1;
for (; end < arg.size() && arg[end] == '\\'; ++end)
/* empty */;
size_t backslash_count = end - start;
// Backslashes are escapes only if the run is followed by a double quote.
// Since we also will end the string with a double quote, we escape for
// either a double quote or the end of the string.
if (end == arg.size() || arg[end] == '"') {
// To quote, we need to output 2x as many backslashes.
backslash_count *= 2;
}
for (size_t j = 0; j < backslash_count; ++j)
out.push_back('\\');
// Advance i to one before the end to balance i++ in loop.
i = end - 1;
} else if (arg[i] == '"') {
out.push_back('\\');
out.push_back('"');
} else {
out.push_back(arg[i]);
}
}
out.push_back('"');
return out;
}
} // namespace
#endif
CommandLine::~CommandLine() {
......@@ -337,6 +291,55 @@ void CommandLine::AppendSwitch(const std::string& switch_string) {
switches_[switch_string] = L"";
}
void CommandLine::AppendSwitchASCII(const std::string& switch_string,
const std::string& value_string) {
AppendSwitchNative(switch_string, ASCIIToWide(value_string));
}
// Quote a string if necessary, such that CommandLineToArgvW() will
// always process it as a single argument.
static std::wstring WindowsStyleQuote(const std::wstring& arg) {
// We follow the quoting rules of CommandLineToArgvW.
// http://msdn.microsoft.com/en-us/library/17w5ykft.aspx
if (arg.find_first_of(L" \\\"") == std::wstring::npos) {
// No quoting necessary.
return arg;
}
std::wstring out;
out.push_back(L'"');
for (size_t i = 0; i < arg.size(); ++i) {
if (arg[i] == '\\') {
// Find the extent of this run of backslashes.
size_t start = i, end = start + 1;
for (; end < arg.size() && arg[end] == '\\'; ++end)
/* empty */;
size_t backslash_count = end - start;
// Backslashes are escapes only if the run is followed by a double quote.
// Since we also will end the string with a double quote, we escape for
// either a double quote or the end of the string.
if (end == arg.size() || arg[end] == '"') {
// To quote, we need to output 2x as many backslashes.
backslash_count *= 2;
}
for (size_t j = 0; j < backslash_count; ++j)
out.push_back('\\');
// Advance i to one before the end to balance i++ in loop.
i = end - 1;
} else if (arg[i] == '"') {
out.push_back('\\');
out.push_back('"');
} else {
out.push_back(arg[i]);
}
}
out.push_back('"');
return out;
}
void CommandLine::AppendSwitchNative(const std::string& switch_string,
const std::wstring& value) {
std::wstring combined_switch_string =
......@@ -350,11 +353,6 @@ void CommandLine::AppendSwitchNative(const std::string& switch_string,
switches_[switch_string] = value;
}
void CommandLine::AppendSwitchASCII(const std::string& switch_string,
const std::string& value_string) {
AppendSwitchNative(switch_string, ASCIIToWide(value_string));
}
void CommandLine::AppendArg(const std::string& value) {
DCHECK(IsStringUTF8(value));
AppendArgNative(UTF8ToWide(value));
......@@ -369,7 +367,8 @@ void CommandLine::AppendArgNative(const std::wstring& value) {
void CommandLine::AppendArguments(const CommandLine& other,
bool include_program) {
// Verify include_program is used correctly.
DCHECK(!include_program || !other.GetProgram().empty());
// Logic could be shorter but this is clearer.
DCHECK_EQ(include_program, !other.GetProgram().empty());
if (include_program)
program_ = other.program_;
......@@ -452,31 +451,13 @@ void CommandLine::PrependWrapper(const std::string& wrapper) {
#endif
void CommandLine::AppendSwitchPath(const std::string& switch_string,
const FilePath& path) {
AppendSwitchNative(switch_string, path.value());
}
void CommandLine::AppendSwitches(const CommandLine& other) {
std::map<std::string, StringType>::const_iterator i;
for (i = other.switches_.begin(); i != other.switches_.end(); ++i)
AppendSwitchNative(i->first, i->second);
}
void CommandLine::AppendArgPath(const FilePath& path) {
AppendArgNative(path.value());
}
void CommandLine::AppendArgs(const CommandLine& other) {
if(other.args_.size() <= 0)
return;
#if defined(OS_WIN)
command_line_string_.append(L" --");
#endif // OS_WIN
std::vector<StringType>::const_iterator i;
for (i = other.args_.begin(); i != other.args_.end(); ++i)
AppendArgNative(*i);
void CommandLine::AppendSwitchPath(const std::string& switch_string,
const FilePath& path) {
AppendSwitchNative(switch_string, path.value());
}
void CommandLine::CopySwitchesFrom(const CommandLine& source,
......
......@@ -129,13 +129,11 @@ class CommandLine {
void AppendSwitch(const std::string& switch_string);
// Append a switch and value to the command line.
// CAUTION! Appending a switch after the "--" kSwitchTerminator is futile!
void AppendSwitchPath(const std::string& switch_string, const FilePath& path);
void AppendSwitchNative(const std::string& switch_string,
const StringType& value);
void AppendSwitchASCII(const std::string& switch_string,
const std::string& value);
void AppendSwitches(const CommandLine& other);
// Append an argument to the command line.
// Note on quoting: the argument will be quoted properly such that it is
......@@ -145,7 +143,6 @@ class CommandLine {
void AppendArg(const std::string& value);
void AppendArgPath(const FilePath& value);
void AppendArgNative(const StringType& value);
void AppendArgs(const CommandLine& other);
// Append the arguments from another command line to this one.
// If |include_program| is true, include |other|'s program as well.
......
......@@ -136,7 +136,6 @@
#include <windows.h>
#include "app/win/scoped_com_initializer.h"
#include "base/win/windows_version.h"
#include "chrome/browser/browser_trial.h"
#include "chrome/browser/metrics/user_metrics.h"
#include "chrome/browser/net/url_fixer_upper.h"
......@@ -1584,16 +1583,6 @@ int BrowserMain(const MainFunctionParams& parameters) {
// testing against a bunch of special cases that are taken care early on.
PrepareRestartOnCrashEnviroment(parsed_command_line);
#if defined(OS_WIN)
// Registers Chrome with the Windows Restart Manager, which will restore the
// Chrome session when the computer is restarted after a system update.
// This could be run as late as WM_QUERYENDSESSION for system update reboots,
// but should run on startup if extended to handle crashes/hangs/patches.
// Also, better to run once here than once for each HWND's WM_QUERYENDSESSION.
if (base::win::GetVersion() >= base::win::VERSION_VISTA)
DCHECK(RegisterApplicationRestart(parsed_command_line));
#endif // OS_WIN
// Initialize and maintain network predictor module, which handles DNS
// pre-resolution, as well as TCP/IP connection pre-warming.
// This also registers an observer to discard data when closing incognito
......@@ -1616,8 +1605,8 @@ int BrowserMain(const MainFunctionParams& parameters) {
// file thread to be run sometime later. If this is the first run we record
// the installation event.
RLZTracker::InitRlzDelayed(is_first_run, master_prefs.ping_delay);
#endif // GOOGLE_CHROME_BUILD
#endif // OS_WIN
#endif
#endif
// Configure modules that need access to resources.
net::NetModule::SetResourceProvider(chrome_common_net::NetResourceProvider);
......@@ -1797,8 +1786,8 @@ int BrowserMain(const MainFunctionParams& parameters) {
parameters.ui_task->Run();
delete parameters.ui_task;
} else {
// We are in regular browser boot sequence. Open initial tabs and enter the
// main message loop.
// We are in regular browser boot sequence. Open initial stabs and enter
// the main message loop.
if (browser_init.Start(parsed_command_line, FilePath(), profile,
&result_code)) {
#if (defined(OS_WIN) || defined(OS_LINUX)) && !defined(OS_CHROMEOS)
......
......@@ -114,7 +114,7 @@ int DoUninstallTasks(bool chrome_still_running) {
// the user if the browser process dies. These strings are stored in the
// environment block so they are accessible in the early stages of the
// chrome executable's lifetime.
void PrepareRestartOnCrashEnviroment(const CommandLine& parsed_command_line) {
void PrepareRestartOnCrashEnviroment(const CommandLine &parsed_command_line) {
// Clear this var so child processes don't show the dialog by default.
scoped_ptr<base::Environment> env(base::Environment::Create());
env->UnSetVar(env_vars::kShowRestart);
......@@ -146,29 +146,11 @@ void PrepareRestartOnCrashEnviroment(const CommandLine& parsed_command_line) {
env->SetVar(env_vars::kRestartInfo, UTF16ToUTF8(dlg_strings));
}
bool RegisterApplicationRestart(const CommandLine& parsed_command_line) {
// The Windows Restart Manager expects a string of command line flags only,
// without the program.
CommandLine command_line(CommandLine::NO_PROGRAM);
command_line.AppendSwitches(parsed_command_line);
command_line.AppendArgs(parsed_command_line);
// Ensure restore last session is set.
if (!command_line.HasSwitch(switches::kRestoreLastSession))
command_line.AppendSwitch(switches::kRestoreLastSession);
const wchar_t *command_string = command_line.command_line_string().c_str();
// Restart Chrome if the computer is restarted as the result of an update.
// This could be extended to handle crashes, hangs, and patches.
HRESULT hr = ::RegisterApplicationRestart(command_string,
RESTART_NO_CRASH | RESTART_NO_HANG | RESTART_NO_PATCH);
DCHECK(SUCCEEDED(hr)) << "RegisterApplicationRestart failed.";
return SUCCEEDED(hr);
}
// This method handles the --hide-icons and --show-icons command line options
// for chrome that get triggered by Windows from registry entries
// HideIconsCommand & ShowIconsCommand. Chrome doesn't support hide icons
// functionality so we just ask the users if they want to uninstall Chrome.
int HandleIconsCommands(const CommandLine& parsed_command_line) {
int HandleIconsCommands(const CommandLine &parsed_command_line) {
if (parsed_command_line.HasSwitch(switches::kHideIcons)) {
string16 cp_applet;
base::win::Version version = base::win::GetVersion();
......
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
......@@ -19,17 +19,13 @@ int DoUninstallTasks(bool chrome_still_running);
// the user if the browser process dies. These strings are stored in the
// environment block so they are accessible in the early stages of the
// chrome executable's lifetime.
void PrepareRestartOnCrashEnviroment(const CommandLine& parsed_command_line);
// Registers Chrome with the Windows Restart Manager, which will restore the
// Chrome session when the computer is restarted after a system update.
bool RegisterApplicationRestart(const CommandLine& parsed_command_line);
void PrepareRestartOnCrashEnviroment(const CommandLine &parsed_command_line);
// This method handles the --hide-icons and --show-icons command line options
// for chrome that get triggered by Windows from registry entries
// HideIconsCommand & ShowIconsCommand. Chrome doesn't support hide icons
// functionality so we just ask the users if they want to uninstall Chrome.
int HandleIconsCommands(const CommandLine& parsed_command_line);
int HandleIconsCommands(const CommandLine &parsed_command_line);
// Check if there is any machine level Chrome installed on the current
// machine. If yes and the current Chrome process is user level, we do not
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment