Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / media / test_license_server.cc
1 // Copyright 2014 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 #include "chrome/browser/media/test_license_server.h"
6
7 #include "base/command_line.h"
8 #include "base/file_util.h"
9 #include "base/process/kill.h"
10 #include "base/process/launch.h"
11 #include "chrome/browser/media/test_license_server_config.h"
12
13
14 TestLicenseServer::TestLicenseServer(
15   scoped_ptr<TestLicenseServerConfig> server_config)
16     : server_config_(server_config.Pass()),
17       license_server_process_(base::kNullProcessHandle) {
18 }
19
20 TestLicenseServer::~TestLicenseServer() {
21   Stop();
22 }
23
24 bool TestLicenseServer::Start() {
25   if (license_server_process_ != base::kNullProcessHandle)
26     return true;
27
28   if (!server_config_->IsPlatformSupported()) {
29     VLOG(0) << "License server is not supported on current platform.";
30     return false;
31   }
32
33   base::FilePath license_server_path;
34   server_config_->GetLicenseServerPath(&license_server_path);
35   if (!base::PathExists(license_server_path)) {
36     VLOG(0) << "Missing license server file at " << license_server_path.value();
37     return false;
38   }
39   CommandLine command_line(license_server_path);
40   server_config_->AppendCommandLineArgs(&command_line);
41
42   VLOG(0) << "Starting test license server " <<
43       command_line.GetCommandLineString();
44   if (!base::LaunchProcess(command_line, base::LaunchOptions(),
45                            &license_server_process_)) {
46     VLOG(0) << "Failed to start test license server!";
47     return false;
48   }
49   DCHECK_NE(license_server_process_, base::kNullProcessHandle);
50   return true;
51 }
52
53 bool TestLicenseServer::Stop() {
54   if (license_server_process_ == base::kNullProcessHandle)
55     return true;
56   VLOG(0) << "Killing license server.";
57   bool kill_succeeded = base::KillProcess(license_server_process_, 1, true);
58
59   if (kill_succeeded) {
60     base::CloseProcessHandle(license_server_process_);
61     license_server_process_ = base::kNullProcessHandle;
62   } else {
63     VLOG(1) << "Kill failed?!";
64   }
65   return kill_succeeded;
66 }
67
68 std::string TestLicenseServer::GetServerURL() {
69   return server_config_->GetServerURL();
70 }