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(bratell): By making this more generic we can possibly reused | |
Paweł Hajdan Jr.
2013/09/05 18:31:59
nit: TODO in this place is surprising. Please put
| |
89 // it at other places such as | |
90 // chrome/common/multi_process_lock_unittest.cc. | |
91 public: | |
92 // Constructor which sets up the environment to include the path to | |
Paweł Hajdan Jr.
2013/09/05 18:31:59
nit: "to include the path to path_to_add" -> "to a
| |
93 // path_to_add. | |
94 explicit ScopedPath(const base::FilePath& path_to_add); | |
95 | |
96 // Destructor that restores the path that were active when the | |
97 // object was constructed. | |
98 ~ScopedPath(); | |
99 | |
100 private: | |
101 // The PATH environment variable before it was changed or an empty | |
102 // string if there was no PATH environment variable. | |
103 std::string old_path_; | |
104 | |
105 // The helper object that allows us to read and set environment | |
106 // variables more easily. | |
107 scoped_ptr<base::Environment> environment_; | |
108 | |
109 // A flag saying if we have actually modified the environment. | |
110 bool path_modified_; | |
111 | |
112 DISALLOW_COPY_AND_ASSIGN(ScopedPath); | |
113 }; | |
114 | |
115 ScopedPath::ScopedPath(const base::FilePath& path_to_add) | |
116 : environment_(base::Environment::Create()), | |
117 path_modified_(false) { | |
118 environment_->GetVar("PATH", &old_path_); | |
119 | |
120 std::string new_value = old_path_; | |
121 if (!new_value.empty()) | |
122 new_value += ";"; | |
123 | |
124 new_value += WideToUTF8(path_to_add.value()); | |
125 | |
126 path_modified_ = environment_->SetVar("PATH", new_value); | |
127 } | |
128 | |
129 ScopedPath::~ScopedPath() { | |
130 if (!path_modified_) | |
131 return; | |
132 if (old_path_.empty()) | |
133 environment_->UnSetVar("PATH"); | |
134 else | |
135 environment_->SetVar("PATH", old_path_); | |
136 } | |
137 | |
84 } // namespace | 138 } // namespace |
85 | 139 |
86 namespace net { | 140 namespace net { |
87 | 141 |
88 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { | 142 bool LocalTestServer::LaunchPython(const base::FilePath& testserver_path) { |
89 CommandLine python_command(CommandLine::NO_PROGRAM); | 143 CommandLine python_command(CommandLine::NO_PROGRAM); |
90 if (!GetPythonCommand(&python_command)) | 144 if (!GetPythonCommand(&python_command)) |
91 return false; | 145 return false; |
92 | 146 |
93 python_command.AppendArgPath(testserver_path); | 147 python_command.AppendArgPath(testserver_path); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
127 if (!job_handle_.IsValid()) { | 181 if (!job_handle_.IsValid()) { |
128 LOG(ERROR) << "Could not create JobObject."; | 182 LOG(ERROR) << "Could not create JobObject."; |
129 return false; | 183 return false; |
130 } | 184 } |
131 | 185 |
132 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { | 186 if (!base::SetJobObjectAsKillOnJobClose(job_handle_.Get())) { |
133 LOG(ERROR) << "Could not SetInformationJobObject."; | 187 LOG(ERROR) << "Could not SetInformationJobObject."; |
134 return false; | 188 return false; |
135 } | 189 } |
136 | 190 |
191 // Add our internal python to the path so it can be used if there is | |
192 // no system python. | |
193 base::FilePath python_dir; | |
194 if (!PathService::Get(base::DIR_SOURCE_ROOT, &python_dir)) { | |
195 LOG(ERROR) << "Could not locate source root directory."; | |
196 return false; | |
197 } | |
198 python_dir = python_dir.AppendASCII("third_party").AppendASCII("python_26"); | |
199 ScopedPath python_path(python_dir); | |
137 base::LaunchOptions launch_options; | 200 base::LaunchOptions launch_options; |
138 launch_options.inherit_handles = true; | 201 launch_options.inherit_handles = true; |
139 launch_options.job_handle = job_handle_.Get(); | 202 launch_options.job_handle = job_handle_.Get(); |
140 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { | 203 if (!base::LaunchProcess(python_command, launch_options, &process_handle_)) { |
141 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); | 204 LOG(ERROR) << "Failed to launch " << python_command.GetCommandLineString(); |
142 return false; | 205 return false; |
143 } | 206 } |
144 | 207 |
145 return true; | 208 return true; |
146 } | 209 } |
(...skipping 19 matching lines...) Expand all Loading... | |
166 if (!ParseServerData(server_data)) { | 229 if (!ParseServerData(server_data)) { |
167 LOG(ERROR) << "Could not parse server_data: " << server_data; | 230 LOG(ERROR) << "Could not parse server_data: " << server_data; |
168 return false; | 231 return false; |
169 } | 232 } |
170 | 233 |
171 return true; | 234 return true; |
172 } | 235 } |
173 | 236 |
174 } // namespace net | 237 } // namespace net |
175 | 238 |
OLD | NEW |