Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / media / wv_test_license_server_config.cc
blobe9db6111f32dd4423485096e33c2f63498cb3178
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.
5 #include "chrome/browser/media/wv_test_license_server_config.h"
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/files/file_util.h"
10 #include "base/path_service.h"
11 #include "base/rand_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "net/base/net_errors.h"
14 #include "net/socket/tcp_server_socket.h"
15 #include "net/test/python_utils.h"
18 const uint16 kMinPort = 17000;
19 const uint16 kPortRangeSize = 1000;
21 // Widevine license server configuration files.
22 const base::FilePath::CharType kKeysFileName[] =
23 FILE_PATH_LITERAL("keys.dat");
24 const base::FilePath::CharType kPoliciesFileName[] =
25 FILE_PATH_LITERAL("policies.dat");
26 const base::FilePath::CharType kProfilesFileName[] =
27 FILE_PATH_LITERAL("profiles.dat");
29 // License server configuration files directory name relative to root.
30 const base::FilePath::CharType kLicenseServerConfigDirName[] =
31 FILE_PATH_LITERAL("config");
33 WVTestLicenseServerConfig::WVTestLicenseServerConfig() {
36 WVTestLicenseServerConfig::~WVTestLicenseServerConfig() {
39 bool WVTestLicenseServerConfig::GetServerCommandLine(
40 base::CommandLine* command_line) {
41 if (!GetPythonCommand(command_line)) {
42 LOG(ERROR) << "Could not get Python runtime command.";
43 return false;
46 // Add the Python protocol buffers files directory to Python path.
47 base::FilePath pyproto_dir;
48 if (!GetPyProtoPath(&pyproto_dir)) {
49 DVLOG(0) << "Cannot find pyproto directory required by license server.";
50 return false;
52 AppendToPythonPath(pyproto_dir);
54 base::FilePath license_server_path;
55 GetLicenseServerPath(&license_server_path);
56 if (!base::PathExists(license_server_path)) {
57 DVLOG(0) << "Missing license server file at "
58 << license_server_path.value();
59 return false;
62 base::FilePath server_root;
63 GetLicenseServerRootPath(&server_root);
64 base::FilePath config_path = server_root.Append(kLicenseServerConfigDirName);
66 if (!base::PathExists(config_path.Append(kKeysFileName)) ||
67 !base::PathExists(config_path.Append(kPoliciesFileName)) ||
68 !base::PathExists(config_path.Append(kProfilesFileName))) {
69 DVLOG(0) << "Missing license server configuration files.";
70 return false;
73 if (!SelectServerPort())
74 return false;
76 // Needed to dynamically load .so libraries used by license server.
77 // TODO(shadi): Remove need to set env variable once b/12932983 is fixed.
78 #if defined(OS_LINUX)
79 scoped_ptr<base::Environment> env(base::Environment::Create());
80 const char kLibraryPathEnvVarName[] = "LD_LIBRARY_PATH";
81 std::string library_paths(license_server_path.DirName().value());
82 std::string old_path;
83 if (env->GetVar(kLibraryPathEnvVarName, &old_path))
84 library_paths.append(":").append(old_path);
85 env->SetVar(kLibraryPathEnvVarName, library_paths);
86 #endif // defined(OS_LINUX)
88 // Since it is a Python command line, we need to AppendArg instead of
89 // AppendSwitch so that the arguments are passed to the Python server instead
90 // of Python engine.
91 command_line->AppendArgPath(license_server_path);
92 command_line->AppendArg("-k");
93 command_line->AppendArgPath(config_path.Append(kKeysFileName));
94 command_line->AppendArg("-o");
95 command_line->AppendArgPath(config_path.Append(kPoliciesFileName));
96 command_line->AppendArg("-r");
97 command_line->AppendArgPath(config_path.Append(kProfilesFileName));
98 command_line->AppendArg(base::StringPrintf("--port=%u", port_));
99 return true;
102 bool WVTestLicenseServerConfig::SelectServerPort() {
103 // Try all ports within the range of kMinPort to (kMinPort + kPortRangeSize)
104 // Instead of starting from kMinPort, use a random port within that range.
105 net::IPAddressNumber address;
106 net::ParseIPLiteralToNumber("127.0.0.1", &address);
107 uint16 start_seed = base::RandInt(0, kPortRangeSize);
108 uint16 try_port = 0;
109 for (uint16 i = 0; i < kPortRangeSize; ++i) {
110 try_port = kMinPort + (start_seed + i) % kPortRangeSize;
111 net::NetLog::Source source;
112 net::TCPServerSocket sock(NULL, source);
113 if (sock.Listen(net::IPEndPoint(address, try_port), 1) == net::OK) {
114 port_ = try_port;
115 return true;
118 DVLOG(0) << "Could not find an open port in the range of " << kMinPort <<
119 " to " << kMinPort + kPortRangeSize;
120 return false;
123 bool WVTestLicenseServerConfig::IsPlatformSupported() {
124 #if defined(OS_LINUX) && defined(ARCH_CPU_X86_64)
125 return true;
126 #else
127 return false;
128 #endif // defined(OS_LINUX)
131 std::string WVTestLicenseServerConfig::GetServerURL() {
132 return base::StringPrintf("http://localhost:%u/license_server", port_);
135 void WVTestLicenseServerConfig::GetLicenseServerPath(base::FilePath *path) {
136 base::FilePath server_root;
137 GetLicenseServerRootPath(&server_root);
138 // Platform-specific license server binary path relative to root.
139 *path =
140 #if defined(OS_LINUX)
141 server_root.Append(FILE_PATH_LITERAL("linux"))
142 .Append(FILE_PATH_LITERAL("license_server.py"));
143 #else
144 server_root.Append(FILE_PATH_LITERAL("unsupported_platform"));
145 #endif // defined(OS_LINUX)
148 void WVTestLicenseServerConfig::GetLicenseServerRootPath(
149 base::FilePath* path) {
150 base::FilePath source_root;
151 PathService::Get(base::DIR_SOURCE_ROOT, &source_root);
152 *path = source_root.Append(FILE_PATH_LITERAL("third_party"))
153 .Append(FILE_PATH_LITERAL("widevine"))
154 .Append(FILE_PATH_LITERAL("test"))
155 .Append(FILE_PATH_LITERAL("license_server"));