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 "net/test/spawned_test_server/local_test_server.h" | 5 #include "net/test/spawned_test_server/local_test_server.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <wincrypt.h> | 8 #include <wincrypt.h> |
9 | 9 |
10 #include "base/base_paths.h" | 10 #include "base/base_paths.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/environment.h" | |
13 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
14 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
15 #include "base/path_service.h" | 16 #include "base/path_service.h" |
16 #include "base/process/launch.h" | 17 #include "base/process/launch.h" |
17 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 #include "base/test/test_timeouts.h" | 21 #include "base/test/test_timeouts.h" |
21 #include "base/threading/thread.h" | 22 #include "base/threading/thread.h" |
22 #include "base/win/scoped_handle.h" | 23 #include "base/win/scoped_handle.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
74 thread.Stop(); | 75 thread.Stop(); |
75 // If the timeout kicked in, abort. | 76 // If the timeout kicked in, abort. |
76 if (unblocked) { | 77 if (unblocked) { |
77 LOG(ERROR) << "Timeout exceeded for ReadData"; | 78 LOG(ERROR) << "Timeout exceeded for ReadData"; |
78 return false; | 79 return false; |
79 } | 80 } |
80 | 81 |
81 return true; | 82 return true; |
82 } | 83 } |
83 | 84 |
85 // Class that sets up a temporary path that includes the supplied path | |
86 // at the end. | |
87 class ScopedPath { | |
88 // TODO: By making this more generic we can possibly reused it at | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: TODO should have a username in parentheses. I
| |
89 // other places such as chrome/common/multi_process_lock_unittest.cc. | |
90 public: | |
91 // Constructor which sets up the environment to include the path to | |
92 // third_party/python_26. | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: Don't mention python_26 here as it's supposed
| |
93 ScopedPath(const base::FilePath& path_to_add); | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: One-parameter ctros should be marked explicit
| |
94 | |
95 // Destructor that restores the path that were active when the | |
96 // object was constructed. | |
97 ~ScopedPath(); | |
98 | |
99 private: | |
100 // The PATH environment variable before it was changed or an empty | |
101 // string if there was no PATH environment variable. | |
102 std::string old_path_; | |
103 | |
104 // The helper object that allows us to read and set environment | |
105 // variables more easily. | |
106 scoped_ptr<base::Environment> environment_; | |
107 | |
108 // A flag saying if we have actually modified the environment. If we | |
109 // haven't then we should also not try to "restore" it in the | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: No need for the "If we haven't" part (up to y
| |
110 // destructor. | |
111 bool path_modified_; | |
112 | |
113 // This is a local stack based helper object. Copying it would make | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: DISALLOW_COPY_AND_ASSIGN is a common enough m
| |
114 // no sense and would cause bugs. | |
115 DISALLOW_COPY_AND_ASSIGN(ScopedPath); | |
116 }; | |
117 | |
118 ScopedPath::ScopedPath(const base::FilePath& path_to_add) | |
119 : environment_(base::Environment::Create()), | |
120 path_modified_(false) { | |
121 // Retrieves the old PATH, adds the new path to the end of it and | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: No need for this comment.
| |
122 // then restores the original PATH in the destructor. | |
123 environment_->GetVar("PATH", &old_path_); | |
124 | |
125 std::string new_value = old_path_; | |
126 if (!new_value.empty()) | |
127 new_value += ";"; | |
128 | |
129 new_value += WideToUTF8(path_to_add.value()); | |
130 | |
131 path_modified_ = environment_->SetVar("PATH", new_value); | |
132 } | |
133 | |
134 ScopedPath::~ScopedPath() { | |
135 if (path_modified_) { | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: I prefer an early return, like:
if (!path_mo
| |
136 if (old_path_.empty()) | |
137 environment_->UnSetVar("PATH"); | |
138 else | |
139 environment_->SetVar("PATH", old_path_); | |
140 } | |
141 } | |
142 | |
84 } // namespace | 143 } // namespace |
85 | 144 |
86 namespace net { | 145 namespace net { |
87 | 146 |
147 | |
Paweł Hajdan Jr.
2013/08/26 21:39:49
nit: No need for this empty line.
| |
88 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { | 148 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { |
89 CommandLine python_command(CommandLine::NO_PROGRAM); | 149 CommandLine python_command(CommandLine::NO_PROGRAM); |
90 if (!GetPythonCommand(&python_command)) | 150 if (!GetPythonCommand(&python_command)) |
91 return false; | 151 return false; |
92 | 152 |
93 python_command.AppendArgPath(testserver_path); | 153 python_command.AppendArgPath(testserver_path); |
94 if (!AddCommandLineArguments(&python_command)) | 154 if (!AddCommandLineArguments(&python_command)) |
95 return false; | 155 return false; |
96 | 156 |
97 HANDLE child_read = NULL; | 157 HANDLE child_read = NULL; |
(...skipping 29 matching lines...) Expand all Loading... | |
127 if (!job_handle_.IsValid()) { | 187 if (!job_handle_.IsValid()) { |
128 LOG(ERROR) << "Could not create JobObject."; | 188 LOG(ERROR) << "Could not create JobObject."; |
129 return false; | 189 return false; |
130 } | 190 } |
131 | 191 |
132 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { | 192 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { |
133 LOG(ERROR) << "Could not SetInformationJobObject."; | 193 LOG(ERROR) << "Could not SetInformationJobObject."; |
134 return false; | 194 return false; |
135 } | 195 } |
136 | 196 |
197 // Add our internal python to the path so it can be used if there is | |
198 // no system python. | |
199 base::FilePath python_dir; | |
200 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_dir)) { | |
201 LOG(ERROR) << "Could not locate source root directory."; | |
202 return false; | |
203 } | |
204 python_dir = python_dir.AppendASCII("third_party").AppendASCII("python_26"); | |
205 ScopedPath python_path(python_dir); | |
137 base::LaunchOptions launch_options; | 206 base::LaunchOptions launch_options; |
138 launch_options.inherit_handles = true; | 207 launch_options.inherit_handles = true; |
139 launch_options.job_handle = job_handle_.Get(); | 208 launch_options.job_handle = job_handle_.Get(); |
140 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { | 209 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { |
141 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); | 210 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); |
142 return false; | 211 return false; |
143 } | 212 } |
144 | 213 |
145 return true; | 214 return true; |
146 } | 215 } |
(...skipping 19 matching lines...) Expand all Loading... | |
166 if (!ParseServerData(server_data)) { | 235 if (!ParseServerData(server_data)) { |
167 LOG(ERROR) << "Could not parse server_data: " << server_data; | 236 LOG(ERROR) << "Could not parse server_data: " << server_data; |
168 return false; | 237 return false; |
169 } | 238 } |
170 | 239 |
171 return true; | 240 return true; |
172 } | 241 } |
173 | 242 |
174 } // namespace net | 243 } // namespace net |
175 | 244 |
OLD | NEW |