diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/common/auto_start_linux.cc | 26 | ||||
-rw-r--r-- | chrome/common/auto_start_linux.h | 8 | ||||
-rw-r--r-- | chrome/common/service_process_util.cc | 23 | ||||
-rw-r--r-- | chrome/common/service_process_util.h | 7 | ||||
-rw-r--r-- | chrome/common/service_process_util_linux.cc | 28 | ||||
-rw-r--r-- | chrome/common/service_process_util_mac.mm | 2 | ||||
-rw-r--r-- | chrome/common/service_process_util_unittest.cc | 59 | ||||
-rw-r--r-- | chrome/common/service_process_util_win.cc | 6 | ||||
-rw-r--r-- | chrome/service/service_process.cc | 4 | ||||
-rw-r--r-- | chrome/service/service_process.h | 1 |
10 files changed, 150 insertions, 14 deletions
diff --git a/chrome/common/auto_start_linux.cc b/chrome/common/auto_start_linux.cc index 5508846..099cb5e 100644 --- a/chrome/common/auto_start_linux.cc +++ b/chrome/common/auto_start_linux.cc @@ -9,6 +9,7 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/nix/xdg_util.h" +#include "base/string_tokenizer.h" namespace { @@ -60,3 +61,28 @@ bool AutoStart::Remove(const std::string& autostart_filename) { FilePath autostart_file = autostart_directory.Append(autostart_filename); return file_util::Delete(autostart_file, false); } + +bool AutoStart::GetAutostartFileContents( + const std::string& autostart_filename, std::string* contents) { + scoped_ptr<base::Environment> environment(base::Environment::Create()); + FilePath autostart_directory = GetAutostartDirectory(environment.get()); + FilePath autostart_file = autostart_directory.Append(autostart_filename); + return file_util::ReadFileToString(autostart_file, contents); +} + +bool AutoStart::GetAutostartFileValue(const std::string& autostart_filename, + const std::string& value_name, + std::string* value) { + std::string contents; + if (!GetAutostartFileContents(autostart_filename, &contents)) + return false; + StringTokenizer tokenizer(contents, "\n"); + std::string token = value_name + "="; + while (tokenizer.GetNext()) { + if (tokenizer.token().substr(0, token.length()) == token) { + *value = tokenizer.token().substr(token.length()); + return true; + } + } + return false; +} diff --git a/chrome/common/auto_start_linux.h b/chrome/common/auto_start_linux.h index 9d7cdf0c..d6b1009 100644 --- a/chrome/common/auto_start_linux.h +++ b/chrome/common/auto_start_linux.h @@ -20,7 +20,13 @@ class AutoStart { bool is_terminal_app); // Removes an autostart file. static bool Remove(const std::string& autostart_filename); - + // Gets the entire contents of an autostart file. + static bool GetAutostartFileContents(const std::string& autostart_filename, + std::string* contents); + // Gets a specific value from an autostart file. + static bool GetAutostartFileValue(const std::string& autostart_filename, + const std::string& value_name, + std::string* value); private: DISALLOW_IMPLICIT_CONSTRUCTORS(AutoStart); }; diff --git a/chrome/common/service_process_util.cc b/chrome/common/service_process_util.cc index 6a2f02f..89e9e9a 100644 --- a/chrome/common/service_process_util.cc +++ b/chrome/common/service_process_util.cc @@ -4,6 +4,7 @@ #include <algorithm> +#include "base/command_line.h" #include "base/file_util.h" #include "base/logging.h" #include "base/mac/scoped_nsautorelease_pool.h" @@ -16,8 +17,10 @@ #include "base/string_util.h" #include "base/utf_string_conversions.h" #include "base/version.h" +#include "chrome/common/child_process_host.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" +#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_version_info.h" #include "chrome/common/service_process_util.h" @@ -158,6 +161,7 @@ IPC::ChannelHandle GetServiceProcessChannel() { #endif // !OS_MACOSX ServiceProcessState::ServiceProcessState() : state_(NULL) { + CreateAutoRunCommandLine(); } ServiceProcessState::~ServiceProcessState() { @@ -258,3 +262,22 @@ IPC::ChannelHandle ServiceProcessState::GetServiceProcessChannel() { } #endif // !OS_MACOSX + +void ServiceProcessState::CreateAutoRunCommandLine() { + FilePath exe_path = ChildProcessHost::GetChildPath(false); + if (exe_path.empty()) { + NOTREACHED() << "Unable to get service process binary name."; + } + autorun_command_line_.reset(new CommandLine(exe_path)); + autorun_command_line_->AppendSwitchASCII(switches::kProcessType, + switches::kServiceProcess); + + // The user data directory is the only other flag we currently want to + // possibly store. + const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); + FilePath user_data_dir = + browser_command_line.GetSwitchValuePath(switches::kUserDataDir); + if (!user_data_dir.empty()) + autorun_command_line_->AppendSwitchPath(switches::kUserDataDir, + user_data_dir); +} diff --git a/chrome/common/service_process_util.h b/chrome/common/service_process_util.h index 2ad72d7..1ec9d65 100644 --- a/chrome/common/service_process_util.h +++ b/chrome/common/service_process_util.h @@ -80,7 +80,7 @@ class ServiceProcessState { void SignalStopped(); // Register the service process to run on startup. - bool AddToAutoRun(CommandLine* command_line); + bool AddToAutoRun(); // Unregister the service process to run on startup. bool RemoveFromAutoRun(); @@ -111,11 +111,16 @@ class ServiceProcessState { // Tear down the platform specific state. void TearDownState(); + // Initializes the command-line that can be used to autorun the service + // process. + void CreateAutoRunCommandLine(); + // An opaque object that maintains state. The actual definition of this is // platform dependent. struct StateData; StateData* state_; scoped_ptr<base::SharedMemory> shared_mem_service_data_; + scoped_ptr<CommandLine> autorun_command_line_; friend struct DefaultSingletonTraits<ServiceProcessState>; }; diff --git a/chrome/common/service_process_util_linux.cc b/chrome/common/service_process_util_linux.cc index 1a53889..d51654b 100644 --- a/chrome/common/service_process_util_linux.cc +++ b/chrome/common/service_process_util_linux.cc @@ -7,9 +7,11 @@ #include <signal.h> #include <unistd.h> +#include "base/command_line.h" #include "base/file_util.h" #include "base/logging.h" #include "base/threading/platform_thread.h" +#include "chrome/common/auto_start_linux.h" #include "chrome/common/multi_process_lock.h" namespace { @@ -41,6 +43,13 @@ MultiProcessLock* TakeServiceInitializingLock(bool waiting) { return TakeNamedLock(lock_name, waiting); } +std::string GetBaseDesktopName() { +#if defined(GOOGLE_CHROME_BUILD) + return "google-chrome-service.desktop"; +#else // CHROMIUM_BUILD + return "chromium-service.desktop"; +#endif +} } // namespace MultiProcessLock* TakeServiceRunningLock(bool waiting) { @@ -68,12 +77,21 @@ bool ServiceProcessState::TakeSingletonLock() { return state_->initializing_lock_.get(); } -bool ServiceProcessState::AddToAutoRun(CommandLine* cmd_line) { - NOTIMPLEMENTED(); - return false; +bool ServiceProcessState::AddToAutoRun() { + DCHECK(autorun_command_line_.get()); +#if defined(GOOGLE_CHROME_BUILD) + std::string app_name = "Google Chrome Service"; +#else // CHROMIUM_BUILD + std::string app_name = "Chromium Service"; +#endif + return AutoStart::AddApplication( + GetServiceProcessScopedName(GetBaseDesktopName()), + app_name, + autorun_command_line_->command_line_string(), + false); } bool ServiceProcessState::RemoveFromAutoRun() { - NOTIMPLEMENTED(); - return false; + return AutoStart::Remove( + GetServiceProcessScopedName(GetBaseDesktopName())); } diff --git a/chrome/common/service_process_util_mac.mm b/chrome/common/service_process_util_mac.mm index 632538a..7f60f31 100644 --- a/chrome/common/service_process_util_mac.mm +++ b/chrome/common/service_process_util_mac.mm @@ -185,7 +185,7 @@ bool CheckServiceProcessReady() { return ready; } -bool ServiceProcessState::AddToAutoRun(CommandLine* cmd_line) { +bool ServiceProcessState::AddToAutoRun() { NOTIMPLEMENTED(); return false; } diff --git a/chrome/common/service_process_util_unittest.cc b/chrome/common/service_process_util_unittest.cc index eadf51f..03827e0 100644 --- a/chrome/common/service_process_util_unittest.cc +++ b/chrome/common/service_process_util_unittest.cc @@ -8,15 +8,28 @@ // TODO(dmaclach): Figure out tests that will work with launchd on Mac OS. #include "base/at_exit.h" +#include "base/command_line.h" #include "base/process_util.h" +#include "base/scoped_ptr.h" #include "base/string_util.h" #include "base/test/multiprocess_test.h" #include "base/test/test_timeouts.h" #include "base/threading/thread.h" +#include "base/utf_string_conversions.h" +#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_version_info.h" #include "chrome/common/service_process_util.h" #include "testing/multiprocess_func_list.h" +#if defined(OS_WIN) +#include "base/win/win_util.h" +#endif + +#if defined(OS_LINUX) +#include <glib.h> +#include "chrome/common/auto_start_linux.h" +#endif + namespace { bool g_good_shutdown = false; @@ -91,6 +104,52 @@ TEST_F(ServiceProcessStateTest, ReadyState) { LaunchAndWait("ServiceProcessStateTestReadyFalse"); } +TEST_F(ServiceProcessStateTest, AutoRun) { + ServiceProcessState* state = ServiceProcessState::GetInstance(); + ASSERT_TRUE(state->AddToAutoRun()); + scoped_ptr<CommandLine> autorun_command_line; +#if defined(OS_WIN) + std::string value_name = GetServiceProcessScopedName("_service_run"); + string16 value; + EXPECT_TRUE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, + UTF8ToWide(value_name), + &value)); + autorun_command_line.reset(new CommandLine(CommandLine::FromString(value))); +#elif defined(OS_LINUX) +#if defined(GOOGLE_CHROME_BUILD) + std::string base_desktop_name = "google-chrome-service.desktop"; +#else // CHROMIUM_BUILD + std::string base_desktop_name = "chromium-service.desktop"; +#endif + std::string exec_value; + EXPECT_TRUE(AutoStart::GetAutostartFileValue( + GetServiceProcessScopedName(base_desktop_name), "Exec", &exec_value)); + GError *error = NULL; + gchar **argv = NULL; + gint argc = NULL; + if (g_shell_parse_argv(exec_value.c_str(), &argc, &argv, &error)) { + autorun_command_line.reset(new CommandLine(argc, argv)); + g_strfreev(argv); + } else { + ADD_FAILURE(); + g_error_free(error); + } +#endif // defined(OS_WIN) + if (autorun_command_line.get()) { + EXPECT_EQ(autorun_command_line->GetSwitchValueASCII(switches::kProcessType), + std::string(switches::kServiceProcess)); + } + ASSERT_TRUE(state->RemoveFromAutoRun()); +#if defined(OS_WIN) + EXPECT_FALSE(base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, + UTF8ToWide(value_name), + &value)); +#elif defined(OS_LINUX) + EXPECT_FALSE(AutoStart::GetAutostartFileValue( + GetServiceProcessScopedName(base_desktop_name), "Exec", &exec_value)); +#endif // defined(OS_WIN) +} + TEST_F(ServiceProcessStateTest, SharedMem) { std::string version; base::ProcessId pid; diff --git a/chrome/common/service_process_util_win.cc b/chrome/common/service_process_util_win.cc index b617988..5ffb7f5 100644 --- a/chrome/common/service_process_util_win.cc +++ b/chrome/common/service_process_util_win.cc @@ -8,6 +8,7 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" +#include "base/scoped_ptr.h" #include "base/string16.h" #include "base/task.h" #include "base/utf_string_conversions.h" @@ -125,11 +126,12 @@ bool ServiceProcessState::SignalReady( return true; } -bool ServiceProcessState::AddToAutoRun(CommandLine* cmd_line) { +bool ServiceProcessState::AddToAutoRun() { + DCHECK(autorun_command_line_.get()); return base::win::AddCommandToAutoRun( HKEY_CURRENT_USER, UTF8ToWide(GetServiceProcessAutoRunKey()), - cmd_line->command_line_string()); + autorun_command_line_->command_line_string()); } bool ServiceProcessState::RemoveFromAutoRun() { diff --git a/chrome/service/service_process.cc b/chrome/service/service_process.cc index 9c0a8c8..68d02a7 100644 --- a/chrome/service/service_process.cc +++ b/chrome/service/service_process.cc @@ -218,8 +218,6 @@ bool ServiceProcess::Initialize(MessageLoopForUI* message_loop, // See if we need to stay running. ScheduleShutdownCheck(); - command_line_.reset(new CommandLine(command_line)); - return true; } @@ -309,7 +307,7 @@ void ServiceProcess::OnChromotingHostDisabled() { void ServiceProcess::OnServiceEnabled() { enabled_services_++; if (1 == enabled_services_) { - ServiceProcessState::GetInstance()->AddToAutoRun(command_line_.get()); + ServiceProcessState::GetInstance()->AddToAutoRun(); } } diff --git a/chrome/service/service_process.h b/chrome/service/service_process.h index 9d4d6f3..7c2606c 100644 --- a/chrome/service/service_process.h +++ b/chrome/service/service_process.h @@ -133,7 +133,6 @@ class ServiceProcess : public CloudPrintProxy::Client, #if defined(ENABLE_REMOTING) scoped_refptr<remoting::ChromotingHostManager> remoting_host_manager_; #endif - scoped_ptr<CommandLine> command_line_; DISALLOW_COPY_AND_ASSIGN(ServiceProcess); }; |