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