OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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. | |
4 // | |
5 // This file implements a standalone host process for Me2Me, which is currently | |
6 // used for the Linux-only Virtual Me2Me build. | |
7 | |
8 #include <stdlib.h> | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/at_exit.h" | |
13 #include "base/bind.h" | |
14 #include "base/callback.h" | |
15 #include "base/command_line.h" | |
16 #include "base/file_path.h" | |
17 #include "base/file_util.h" | |
18 #include "base/logging.h" | |
19 #include "base/message_loop.h" | |
20 #include "base/threading/thread.h" | |
21 #include "build/build_config.h" | |
22 #include "crypto/nss_util.h" | |
23 #include "remoting/base/constants.h" | |
24 #include "remoting/host/chromoting_host.h" | |
25 #include "remoting/host/chromoting_host_context.h" | |
26 #include "remoting/host/desktop_environment.h" | |
27 #include "remoting/host/event_executor.h" | |
28 #include "remoting/host/heartbeat_sender.h" | |
29 #include "remoting/host/host_config.h" | |
30 #include "remoting/host/json_host_config.h" | |
31 #include "remoting/host/self_access_verifier.h" | |
32 | |
33 #if defined(TOOLKIT_USES_GTK) | |
34 #include "ui/gfx/gtk_util.h" | |
35 #endif | |
36 | |
37 namespace { | |
38 // These are used for parsing the config-file locations from the command line, | |
39 // and for defining the default locations if the switches are not present. | |
40 const char kAuthConfigSwitchName[] = "auth-config"; | |
41 const char kHostConfigSwitchName[] = "host-config"; | |
42 const FilePath::CharType kDefaultConfigDir[] = | |
43 FILE_PATH_LITERAL(".config/chrome-remote-desktop"); | |
44 const FilePath::CharType kDefaultAuthConfigFile[] = | |
45 FILE_PATH_LITERAL("auth.json"); | |
46 const FilePath::CharType kDefaultHostConfigFile[] = | |
47 FILE_PATH_LITERAL("host.json"); | |
48 } | |
49 | |
50 namespace remoting { | |
51 | |
52 class HostProcess { | |
53 public: | |
54 HostProcess() {} | |
55 | |
56 void ProcessCommandLine(const CommandLine* cmd_line) { | |
57 FilePath default_config_dir = | |
58 file_util::GetHomeDir().Append(kDefaultConfigDir); | |
59 | |
60 if (cmd_line->HasSwitch(kAuthConfigSwitchName)) { | |
61 set_auth_config_path(cmd_line->GetSwitchValuePath(kAuthConfigSwitchName)); | |
62 } else { | |
63 set_auth_config_path(default_config_dir.Append(kDefaultAuthConfigFile)); | |
64 } | |
65 | |
66 if (cmd_line->HasSwitch(kHostConfigSwitchName)) { | |
67 set_host_config_path(cmd_line->GetSwitchValuePath(kHostConfigSwitchName)); | |
68 } else { | |
69 set_host_config_path(default_config_dir.Append(kDefaultHostConfigFile)); | |
70 } | |
Wez
2011/11/16 22:26:14
With this code moved to ProcessCommandLine(), if a
Lambros
2011/11/17 01:31:11
Done. I like both these suggestions, but the form
| |
71 } | |
72 | |
73 // Read Host config from disk, returning true if successful. | |
74 bool LoadConfig(base::MessageLoopProxy* message_loop_proxy) { | |
Wez
2011/11/16 22:26:14
Doesn't need to be public any more?
Lambros
2011/11/17 01:31:11
Moved to private section.
| |
75 host_config_ = | |
76 new remoting::JsonHostConfig(host_config_path_, message_loop_proxy); | |
77 scoped_refptr<remoting::JsonHostConfig> auth_config = | |
78 new remoting::JsonHostConfig(auth_config_path_, message_loop_proxy); | |
79 | |
80 std::string failed_path; | |
81 if (!host_config_->Read()) { | |
82 failed_path = host_config_path_.value(); | |
83 } else if (!auth_config->Read()) { | |
84 failed_path = auth_config_path_.value(); | |
85 } | |
86 if (!failed_path.empty()) { | |
87 LOG(ERROR) << "Failed to read configuration file " << failed_path; | |
88 return false; | |
89 } | |
90 | |
91 // Copy the needed keys from |auth_config| into |host_config|. | |
92 std::string value; | |
93 auth_config->GetString(kXmppAuthTokenConfigPath, &value); | |
94 host_config_->SetString(kXmppAuthTokenConfigPath, value); | |
95 auth_config->GetString(kXmppLoginConfigPath, &value); | |
96 host_config_->SetString(kXmppLoginConfigPath, value); | |
97 | |
98 // For the Me2Me host, we assume we always use the ClientLogin token for | |
99 // chromiumsync because we do not have an HTTP stack with which we can | |
100 // easily request an OAuth2 access token even if we had a RefreshToken for | |
101 // the account. | |
102 host_config_->SetString(kXmppAuthServiceConfigPath, | |
103 kChromotingTokenDefaultServiceName); | |
104 return true; | |
105 } | |
106 | |
107 int Run() { | |
108 // |message_loop| is declared early so that any code we call into which | |
109 // requires a current message-loop won't complain. | |
110 // It needs to be a UI message loop to keep runloops spinning on the Mac. | |
111 MessageLoop message_loop(MessageLoop::TYPE_UI); | |
112 | |
113 remoting::ChromotingHostContext context( | |
114 base::MessageLoopProxy::current()); | |
115 context.Start(); | |
116 | |
117 base::Thread file_io_thread("FileIO"); | |
118 file_io_thread.Start(); | |
119 | |
120 if (!LoadConfig(file_io_thread.message_loop_proxy())) { | |
121 context.Stop(); | |
122 return 1; | |
123 } | |
124 | |
125 // Initialize AccessVerifier. | |
126 scoped_ptr<remoting::SelfAccessVerifier> self_access_verifier( | |
127 new remoting::SelfAccessVerifier()); | |
128 if (!self_access_verifier->Init(host_config_)) { | |
129 context.Stop(); | |
130 return 1; | |
131 } | |
132 | |
133 // Create the DesktopEnvironment and ChromotingHost. | |
134 scoped_ptr<DesktopEnvironment> desktop_environment( | |
135 DesktopEnvironment::Create(&context)); | |
136 | |
137 host_ = ChromotingHost::Create(&context, host_config_, | |
138 desktop_environment.get(), | |
139 self_access_verifier.release(), false); | |
140 | |
141 // Initialize HeartbeatSender. | |
142 scoped_ptr<remoting::HeartbeatSender> heartbeat_sender( | |
143 new remoting::HeartbeatSender(context.network_message_loop(), | |
144 host_config_)); | |
145 if (!heartbeat_sender->Init()) { | |
146 context.Stop(); | |
147 return 1; | |
148 } | |
149 host_->AddStatusObserver(heartbeat_sender.get()); | |
150 | |
151 // Run the ChromotingHost until the shutdown task is executed. | |
152 host_->Start(); | |
153 message_loop.MessageLoop::Run(); | |
154 | |
155 // And then stop the chromoting context. | |
156 context.Stop(); | |
157 file_io_thread.Stop(); | |
158 | |
159 host_ = NULL; | |
160 | |
161 return 0; | |
162 } | |
163 | |
164 void set_auth_config_path(const FilePath& config_path) { | |
165 auth_config_path_ = config_path; | |
166 } | |
167 | |
168 void set_host_config_path(const FilePath& config_path) { | |
169 host_config_path_ = config_path; | |
170 } | |
Wez
2011/11/16 22:26:14
Since the only thing that sets these is ProcessCom
Lambros
2011/11/17 01:31:11
Done.
| |
171 | |
172 private: | |
173 FilePath auth_config_path_; | |
174 FilePath host_config_path_; | |
175 | |
176 scoped_refptr<remoting::JsonHostConfig> host_config_; | |
177 | |
178 scoped_refptr<ChromotingHost> host_; | |
179 }; | |
180 | |
181 } // namespace remoting | |
182 | |
183 int main(int argc, char** argv) { | |
184 CommandLine::Init(argc, argv); | |
185 | |
186 // This object instance is required by Chrome code (for example, | |
187 // LazyInstance, MessageLoop). | |
188 base::AtExitManager exit_manager; | |
189 | |
190 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
191 | |
192 #if defined(TOOLKIT_USES_GTK) | |
193 // Required for any calls into GTK functions, such as the Disconnect and | |
194 // Continue windows, though these should not be used for the Me2Me case | |
195 // (crbug.com/104377). | |
196 gfx::GtkInitFromCommandLine(*cmd_line); | |
197 #endif // TOOLKIT_USES_GTK | |
198 | |
199 remoting::HostProcess me2me_host; | |
200 | |
201 me2me_host.ProcessCommandLine(cmd_line); | |
202 | |
203 return me2me_host.Run(); | |
204 } | |
OLD | NEW |