Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/daemon_process.h" | 5 #include "remoting/host/daemon_process.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" | |
| 10 #include "base/files/file_path_watcher.h" | |
| 11 #include "base/file_util.h" | |
| 9 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 10 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
| 14 #include "remoting/host/branding.h" | |
| 15 #include "remoting/host/chromoting_messages.h" | |
| 11 | 16 |
| 12 namespace { | 17 namespace { |
| 13 | 18 |
| 14 const char kIpcThreadName[] = "Daemon process IPC"; | 19 // The command line switch name that can be used to specify the host |
| 20 // configuration file name. | |
|
Wez
2012/08/21 00:18:57
nit: Suggest "The name of the command-line switch
alexeypa (please no reviews)
2012/08/23 23:53:01
Done.
| |
| 21 const char kHostConfigSwitchName[] = "host-config"; | |
| 22 | |
| 23 const FilePath::CharType kDefaultHostConfigFile[] = | |
| 24 FILE_PATH_LITERAL("host.json"); | |
| 15 | 25 |
| 16 } // namespace | 26 } // namespace |
| 17 | 27 |
| 18 namespace remoting { | 28 namespace remoting { |
| 19 | 29 |
| 20 DaemonProcess::~DaemonProcess() { | 30 DaemonProcess::~DaemonProcess() { |
| 31 CHECK_EQ(config_watcher_state_, kConfigWatcherStopped); | |
| 32 } | |
| 33 | |
| 34 void DaemonProcess::OnChannelConnected() { | |
| 35 DCHECK(main_task_runner()->BelongsToCurrentThread()); | |
| 36 | |
| 37 // Send the configuration to the network process. | |
| 38 Send(new ChromotingDaemonNetworkMsg_Configuration(config_)); | |
| 21 } | 39 } |
| 22 | 40 |
| 23 bool DaemonProcess::OnMessageReceived(const IPC::Message& message) { | 41 bool DaemonProcess::OnMessageReceived(const IPC::Message& message) { |
| 24 return true; | 42 DCHECK(main_task_runner()->BelongsToCurrentThread()); |
|
Wez
2012/08/21 00:18:57
nit: You don't need this check if all you're going
alexeypa (please no reviews)
2012/08/23 23:53:01
I'll add useful code to this method in the nearest
| |
| 43 | |
| 44 return false; | |
| 25 } | 45 } |
| 26 | 46 |
| 27 DaemonProcess::DaemonProcess( | 47 DaemonProcess::DaemonProcess( |
|
Wez
2012/08/21 00:18:57
nit: Although this ordering matches that in the cl
alexeypa (please no reviews)
2012/08/21 17:16:36
There was a long thread in chrome-dev about this a
| |
| 28 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 48 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 29 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 49 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 30 const base::Closure& stopped_callback) | 50 const base::Closure& stopped_callback) |
| 31 : Stoppable(main_task_runner, stopped_callback), | 51 : Stoppable(main_task_runner, stopped_callback), |
| 52 config_watcher_state_(kConfigWatcherRunning), | |
| 32 main_task_runner_(main_task_runner), | 53 main_task_runner_(main_task_runner), |
| 33 io_task_runner_(io_task_runner) { | 54 io_task_runner_(io_task_runner) { |
| 34 // Initialize on the same thread that will be used for shutting down. | 55 // Initialize on the same thread that will be used for shutting down. |
| 35 main_task_runner_->PostTask( | 56 main_task_runner_->PostTask( |
| 36 FROM_HERE, | 57 FROM_HERE, |
| 37 base::Bind(&DaemonProcess::Init, base::Unretained(this))); | 58 base::Bind(&DaemonProcess::Initialize, base::Unretained(this))); |
|
Wez
2012/08/21 00:18:57
Is there any stronger guarantee that DaemonProcess
alexeypa (please no reviews)
2012/08/23 23:53:01
Making the object ref-countable (or passing scoped
Wez
2012/08/30 20:36:04
For a follow-up CL, I wonder if we could add a hel
alexeypa (please no reviews)
2012/08/30 23:15:13
Maybe. I have a whole bunch of other important stu
| |
| 38 } | 59 } |
| 39 | 60 |
| 40 void DaemonProcess::Init() { | 61 void DaemonProcess::Initialize() { |
| 41 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 62 DCHECK(main_task_runner()->BelongsToCurrentThread()); |
| 42 | 63 |
| 43 if (!LaunchNetworkProcess()) { | 64 // Get the name of the host configuration file. |
| 44 LOG(ERROR) << "Failed to launch the networking process."; | 65 FilePath default_config_dir = remoting::GetConfigDir(); |
| 66 config_path_ = default_config_dir.Append(kDefaultHostConfigFile); | |
| 67 | |
|
Wez
2012/08/21 00:18:57
nit: No need for this blank line.
alexeypa (please no reviews)
2012/08/23 23:53:01
Done.
| |
| 68 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 69 if (command_line->HasSwitch(kHostConfigSwitchName)) { | |
| 70 config_path_ = command_line->GetSwitchValuePath(kHostConfigSwitchName); | |
| 71 } | |
| 72 | |
| 73 // Initialize the config file watcher on the I/O thread. | |
| 74 io_task_runner()->PostTask( | |
| 75 FROM_HERE, | |
| 76 base::Bind(&DaemonProcess::StartConfigWatcher, base::Unretained(this))); | |
|
Wez
2012/08/21 00:18:57
Similarly, what is the guarantee that DaemonProces
alexeypa (please no reviews)
2012/08/23 23:53:01
CHECK in the destructor seems to be it.
| |
| 77 | |
| 78 // Launch the process. | |
| 79 LaunchNetworkProcess(); | |
| 80 } | |
| 81 | |
| 82 void DaemonProcess::DoStop() { | |
| 83 DCHECK(main_task_runner()->BelongsToCurrentThread()); | |
| 84 | |
| 85 if (config_watcher_state_ == kConfigWatcherRunning) { | |
| 86 config_watcher_state_ = kConfigWatcherStopping; | |
| 87 io_task_runner()->PostTask( | |
| 88 FROM_HERE, | |
| 89 base::Bind(&DaemonProcess::StopConfigWatcher, base::Unretained(this))); | |
| 90 } | |
|
Wez
2012/08/21 00:18:57
I think you need to clear the reload timer here, o
alexeypa (please no reviews)
2012/08/23 23:53:01
It is done in DaemonProcess::StopConfigWatcher.
| |
| 91 | |
| 92 // Wait until the configuration file watcher has been fully stopped. | |
| 93 if (config_watcher_state_ == kConfigWatcherStopped) | |
| 94 CompleteStopping(); | |
| 95 } | |
| 96 | |
| 97 void DaemonProcess::ConfigUpdated(const FilePath& path, bool error) { | |
| 98 DCHECK(io_task_runner()->BelongsToCurrentThread()); | |
| 99 | |
| 100 // Call ReloadConfig() after a short delay, so that we will not try to read | |
| 101 // the updated configuration file before it has been completely written. | |
| 102 // If the writer moves the new configuration file into place atomically, | |
| 103 // this delay may not be necessary. | |
| 104 if (!error && config_path_ == path) | |
| 105 config_updated_timer_->Reset(); | |
| 106 } | |
| 107 | |
| 108 void DaemonProcess::ReloadConfig() { | |
| 109 DCHECK(io_task_runner()->BelongsToCurrentThread()); | |
| 110 | |
| 111 std::string config; | |
| 112 if (!file_util::ReadFileToString(config_path_, &config)) { | |
| 113 LOG(ERROR) << "Failed to read '" << config_path_.value() << "'"; | |
| 45 Stop(); | 114 Stop(); |
| 46 return; | 115 return; |
| 47 } | 116 } |
| 117 | |
| 118 main_task_runner()->PostTask( | |
| 119 FROM_HERE, | |
| 120 base::Bind(&DaemonProcess::SendConfig, base::Unretained(this), config)); | |
| 48 } | 121 } |
| 49 | 122 |
| 50 void DaemonProcess::DoStop() { | 123 void DaemonProcess::SendConfig(const std::string& config) { |
| 51 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 124 DCHECK(main_task_runner()->BelongsToCurrentThread()); |
| 52 | 125 |
| 53 CompleteStopping(); | 126 if (config_ != config) { |
| 127 config_ = config; | |
| 128 Send(new ChromotingDaemonNetworkMsg_Configuration(config_)); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 void DaemonProcess::StartConfigWatcher() { | |
| 133 DCHECK(io_task_runner()->BelongsToCurrentThread()); | |
| 134 DCHECK(config_updated_timer_.get() == NULL); | |
| 135 DCHECK(config_watcher_.get() == NULL); | |
| 136 | |
| 137 // Create the timer that will be used for dealyed-reading the configuration | |
|
Wez
2012/08/21 00:18:57
typo: delayed
alexeypa (please no reviews)
2012/08/23 23:53:01
Done.
| |
| 138 // file. | |
| 139 config_updated_timer_.reset(new base::DelayTimer<DaemonProcess>( | |
| 140 FROM_HERE, base::TimeDelta::FromSeconds(2), this, | |
| 141 &DaemonProcess::ReloadConfig)); | |
| 142 | |
| 143 // Start watching the configuration file. | |
| 144 config_watcher_.reset(new base::files::FilePathWatcher()); | |
| 145 if (!config_watcher_->Watch(config_path_, | |
| 146 base::Bind(&DaemonProcess::ConfigUpdated, | |
| 147 base::Unretained(this)))) { | |
| 148 LOG(ERROR) << "Couldn't watch file '" << config_path_.value() << "'"; | |
| 149 Stop(); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 // Force reloading of the configuration file at least once. | |
| 154 ReloadConfig(); | |
| 155 } | |
| 156 | |
| 157 void DaemonProcess::StopConfigWatcher() { | |
| 158 DCHECK(io_task_runner()->BelongsToCurrentThread()); | |
| 159 DCHECK(config_updated_timer_.get() != NULL); | |
| 160 DCHECK(config_watcher_.get() != NULL); | |
| 161 | |
| 162 config_updated_timer_.reset(NULL); | |
| 163 config_watcher_.reset(NULL); | |
| 164 | |
| 165 main_task_runner()->PostTask( | |
| 166 FROM_HERE, | |
| 167 base::Bind(&DaemonProcess::StopConfigWatcherCompleted, | |
| 168 base::Unretained(this))); | |
| 169 } | |
| 170 | |
| 171 void DaemonProcess::StopConfigWatcherCompleted() { | |
| 172 DCHECK(main_task_runner()->BelongsToCurrentThread()); | |
| 173 DCHECK(config_watcher_state_ == kConfigWatcherStopping); | |
| 174 | |
| 175 config_watcher_state_ = kConfigWatcherStopped; | |
| 176 Stop(); | |
| 54 } | 177 } |
| 55 | 178 |
| 56 } // namespace remoting | 179 } // namespace remoting |
| OLD | NEW |