Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / chrome / test / chromedriver / chrome_launcher.cc
blob90ddedead93637c9053e92559cbe6f62e7edcec6
1 // Copyright (c) 2013 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/test/chromedriver/chrome_launcher.h"
7 #include <algorithm>
8 #include <vector>
10 #include "base/base64.h"
11 #include "base/basictypes.h"
12 #include "base/bind.h"
13 #include "base/command_line.h"
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_file.h"
17 #include "base/format_macros.h"
18 #include "base/json/json_reader.h"
19 #include "base/json/json_writer.h"
20 #include "base/logging.h"
21 #include "base/process/kill.h"
22 #include "base/process/launch.h"
23 #include "base/process/process.h"
24 #include "base/strings/string_number_conversions.h"
25 #include "base/strings/string_util.h"
26 #include "base/strings/stringprintf.h"
27 #include "base/strings/utf_string_conversions.h"
28 #include "base/threading/platform_thread.h"
29 #include "base/time/time.h"
30 #include "base/values.h"
31 #include "chrome/common/chrome_constants.h"
32 #include "chrome/test/chromedriver/chrome/chrome_android_impl.h"
33 #include "chrome/test/chromedriver/chrome/chrome_desktop_impl.h"
34 #include "chrome/test/chromedriver/chrome/chrome_finder.h"
35 #include "chrome/test/chromedriver/chrome/chrome_remote_impl.h"
36 #include "chrome/test/chromedriver/chrome/device_manager.h"
37 #include "chrome/test/chromedriver/chrome/devtools_client_impl.h"
38 #include "chrome/test/chromedriver/chrome/devtools_event_listener.h"
39 #include "chrome/test/chromedriver/chrome/devtools_http_client.h"
40 #include "chrome/test/chromedriver/chrome/embedded_automation_extension.h"
41 #include "chrome/test/chromedriver/chrome/status.h"
42 #include "chrome/test/chromedriver/chrome/user_data_dir.h"
43 #include "chrome/test/chromedriver/chrome/version.h"
44 #include "chrome/test/chromedriver/chrome/web_view.h"
45 #include "chrome/test/chromedriver/net/port_server.h"
46 #include "chrome/test/chromedriver/net/url_request_context_getter.h"
47 #include "crypto/rsa_private_key.h"
48 #include "crypto/sha2.h"
49 #include "third_party/zlib/google/zip.h"
51 #if defined(OS_POSIX)
52 #include <fcntl.h>
53 #include <sys/stat.h>
54 #include <sys/types.h>
55 #elif defined(OS_WIN)
56 #include "chrome/test/chromedriver/keycode_text_conversion.h"
57 #endif
59 namespace {
61 const char* const kCommonSwitches[] = {
62 "disable-popup-blocking",
63 "ignore-certificate-errors",
64 "metrics-recording-only"
67 const char* const kDesktopSwitches[] = {
68 "disable-hang-monitor",
69 "disable-prompt-on-repost",
70 "disable-sync",
71 "no-first-run",
72 "disable-background-networking",
73 "disable-web-resources",
74 "safebrowsing-disable-auto-update",
75 "safebrowsing-disable-download-protection",
76 "disable-client-side-phishing-detection",
77 "disable-component-update",
78 "disable-default-apps",
79 "enable-logging",
80 "log-level=0",
81 "password-store=basic",
82 "use-mock-keychain",
83 "test-type=webdriver"
86 const char* const kAndroidSwitches[] = {
87 "disable-fre",
88 "enable-remote-debugging"
91 #if defined(OS_LINUX)
92 const char kEnableCrashReport[] = "enable-crash-reporter-for-testing";
93 #endif
95 Status UnpackAutomationExtension(const base::FilePath& temp_dir,
96 base::FilePath* automation_extension) {
97 std::string decoded_extension;
98 if (!base::Base64Decode(kAutomationExtension, &decoded_extension))
99 return Status(kUnknownError, "failed to base64decode automation extension");
101 base::FilePath extension_zip = temp_dir.AppendASCII("internal.zip");
102 int size = static_cast<int>(decoded_extension.length());
103 if (base::WriteFile(extension_zip, decoded_extension.c_str(), size)
104 != size) {
105 return Status(kUnknownError, "failed to write automation extension zip");
108 base::FilePath extension_dir = temp_dir.AppendASCII("internal");
109 if (!zip::Unzip(extension_zip, extension_dir))
110 return Status(kUnknownError, "failed to unzip automation extension");
112 *automation_extension = extension_dir;
113 return Status(kOk);
116 Status PrepareCommandLine(uint16 port,
117 const Capabilities& capabilities,
118 base::CommandLine* prepared_command,
119 base::ScopedTempDir* user_data_dir,
120 base::ScopedTempDir* extension_dir,
121 std::vector<std::string>* extension_bg_pages) {
122 base::FilePath program = capabilities.binary;
123 if (program.empty()) {
124 if (!FindChrome(&program))
125 return Status(kUnknownError, "cannot find Chrome binary");
126 } else if (!base::PathExists(program)) {
127 return Status(kUnknownError,
128 base::StringPrintf("no chrome binary at %" PRFilePath,
129 program.value().c_str()));
131 base::CommandLine command(program);
132 Switches switches;
134 for (const auto& common_switch : kCommonSwitches)
135 switches.SetUnparsedSwitch(common_switch);
136 for (const auto& desktop_switch : kDesktopSwitches)
137 switches.SetUnparsedSwitch(desktop_switch);
138 switches.SetSwitch("remote-debugging-port", base::IntToString(port));
139 for (const auto& excluded_switch : capabilities.exclude_switches) {
140 switches.RemoveSwitch(excluded_switch);
142 switches.SetFromSwitches(capabilities.switches);
144 base::FilePath user_data_dir_path;
145 if (switches.HasSwitch("user-data-dir")) {
146 user_data_dir_path = base::FilePath(
147 switches.GetSwitchValueNative("user-data-dir"));
148 } else {
149 command.AppendArg("data:,");
150 if (!user_data_dir->CreateUniqueTempDir())
151 return Status(kUnknownError, "cannot create temp dir for user data dir");
152 switches.SetSwitch("user-data-dir", user_data_dir->path().value());
153 user_data_dir_path = user_data_dir->path();
156 Status status = internal::PrepareUserDataDir(user_data_dir_path,
157 capabilities.prefs.get(),
158 capabilities.local_state.get());
159 if (status.IsError())
160 return status;
162 if (!extension_dir->CreateUniqueTempDir()) {
163 return Status(kUnknownError,
164 "cannot create temp dir for unpacking extensions");
166 status = internal::ProcessExtensions(capabilities.extensions,
167 extension_dir->path(),
168 true,
169 &switches,
170 extension_bg_pages);
171 if (status.IsError())
172 return status;
173 switches.AppendToCommandLine(&command);
174 *prepared_command = command;
175 return Status(kOk);
178 Status WaitForDevToolsAndCheckVersion(
179 const NetAddress& address,
180 URLRequestContextGetter* context_getter,
181 const SyncWebSocketFactory& socket_factory,
182 const Capabilities* capabilities,
183 scoped_ptr<DevToolsHttpClient>* user_client) {
184 scoped_ptr<DeviceMetrics> device_metrics;
185 if (capabilities && capabilities->device_metrics)
186 device_metrics.reset(new DeviceMetrics(*capabilities->device_metrics));
188 scoped_ptr<DevToolsHttpClient> client(new DevToolsHttpClient(
189 address, context_getter, socket_factory, device_metrics.Pass()));
190 base::TimeTicks deadline =
191 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(60);
192 Status status = client->Init(deadline - base::TimeTicks::Now());
193 if (status.IsError())
194 return status;
196 base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
197 if (cmd_line->HasSwitch("disable-build-check")) {
198 LOG(WARNING) << "You are using an unsupported command-line switch: "
199 "--disable-build-check. Please don't report bugs that "
200 "cannot be reproduced with this switch removed.";
201 } else if (client->browser_info()->build_no <
202 kMinimumSupportedChromeBuildNo) {
203 return Status(kUnknownError, "Chrome version must be >= " +
204 GetMinimumSupportedChromeVersion());
207 while (base::TimeTicks::Now() < deadline) {
208 WebViewsInfo views_info;
209 client->GetWebViewsInfo(&views_info);
210 for (size_t i = 0; i < views_info.GetSize(); ++i) {
211 if (views_info.Get(i).type == WebViewInfo::kPage) {
212 *user_client = client.Pass();
213 return Status(kOk);
216 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(50));
218 return Status(kUnknownError, "unable to discover open pages");
221 Status CreateBrowserwideDevToolsClientAndConnect(
222 const NetAddress& address,
223 const PerfLoggingPrefs& perf_logging_prefs,
224 const SyncWebSocketFactory& socket_factory,
225 const ScopedVector<DevToolsEventListener>& devtools_event_listeners,
226 scoped_ptr<DevToolsClient>* browser_client) {
227 scoped_ptr<DevToolsClient> client(new DevToolsClientImpl(
228 socket_factory, base::StringPrintf("ws://%s/devtools/browser/",
229 address.ToString().c_str()),
230 DevToolsClientImpl::kBrowserwideDevToolsClientId));
231 for (ScopedVector<DevToolsEventListener>::const_iterator it =
232 devtools_event_listeners.begin();
233 it != devtools_event_listeners.end();
234 ++it) {
235 // Only add listeners that subscribe to the browser-wide |DevToolsClient|.
236 // Otherwise, listeners will think this client is associated with a webview,
237 // and will send unrecognized commands to it.
238 if ((*it)->subscribes_to_browser())
239 client->AddListener(*it);
241 // Provide the client regardless of whether it connects, so that Chrome always
242 // has a valid |devtools_websocket_client_|. If not connected, no listeners
243 // will be notified, and client will just return kDisconnected errors if used.
244 *browser_client = client.Pass();
245 // To avoid unnecessary overhead, only connect if tracing is enabled, since
246 // the browser-wide client is currently only used for tracing.
247 if (!perf_logging_prefs.trace_categories.empty()) {
248 Status status = (*browser_client)->ConnectIfNecessary();
249 if (status.IsError())
250 return status;
252 return Status(kOk);
255 Status LaunchRemoteChromeSession(
256 URLRequestContextGetter* context_getter,
257 const SyncWebSocketFactory& socket_factory,
258 const Capabilities& capabilities,
259 ScopedVector<DevToolsEventListener>* devtools_event_listeners,
260 scoped_ptr<Chrome>* chrome) {
261 Status status(kOk);
262 scoped_ptr<DevToolsHttpClient> devtools_http_client;
263 status = WaitForDevToolsAndCheckVersion(
264 capabilities.debugger_address, context_getter, socket_factory,
265 NULL, &devtools_http_client);
266 if (status.IsError()) {
267 return Status(kUnknownError, "cannot connect to chrome at " +
268 capabilities.debugger_address.ToString(),
269 status);
272 scoped_ptr<DevToolsClient> devtools_websocket_client;
273 status = CreateBrowserwideDevToolsClientAndConnect(
274 capabilities.debugger_address, capabilities.perf_logging_prefs,
275 socket_factory, *devtools_event_listeners, &devtools_websocket_client);
276 if (status.IsError()) {
277 LOG(WARNING) << "Browser-wide DevTools client failed to connect: "
278 << status.message();
281 chrome->reset(new ChromeRemoteImpl(devtools_http_client.Pass(),
282 devtools_websocket_client.Pass(),
283 *devtools_event_listeners));
284 return Status(kOk);
287 Status LaunchDesktopChrome(
288 URLRequestContextGetter* context_getter,
289 uint16 port,
290 scoped_ptr<PortReservation> port_reservation,
291 const SyncWebSocketFactory& socket_factory,
292 const Capabilities& capabilities,
293 ScopedVector<DevToolsEventListener>* devtools_event_listeners,
294 scoped_ptr<Chrome>* chrome) {
295 base::CommandLine command(base::CommandLine::NO_PROGRAM);
296 base::ScopedTempDir user_data_dir;
297 base::ScopedTempDir extension_dir;
298 std::vector<std::string> extension_bg_pages;
299 Status status = PrepareCommandLine(port,
300 capabilities,
301 &command,
302 &user_data_dir,
303 &extension_dir,
304 &extension_bg_pages);
305 if (status.IsError())
306 return status;
308 base::LaunchOptions options;
310 #if defined(OS_LINUX)
311 // If minidump path is set in the capability, enable minidump for crashes.
312 if (!capabilities.minidump_path.empty()) {
313 VLOG(0) << "Minidump generation specified. Will save dumps to: "
314 << capabilities.minidump_path;
316 options.environ["CHROME_HEADLESS"] = 1;
317 options.environ["BREAKPAD_DUMP_LOCATION"] = capabilities.minidump_path;
319 if (!command.HasSwitch(kEnableCrashReport))
320 command.AppendSwitch(kEnableCrashReport);
323 // We need to allow new privileges so that chrome's setuid sandbox can run.
324 options.allow_new_privs = true;
325 #endif
327 #if !defined(OS_WIN)
328 if (!capabilities.log_path.empty())
329 options.environ["CHROME_LOG_FILE"] = capabilities.log_path;
330 if (capabilities.detach)
331 options.new_process_group = true;
332 #endif
334 #if defined(OS_POSIX)
335 base::FileHandleMappingVector no_stderr;
336 base::ScopedFD devnull;
337 if (!base::CommandLine::ForCurrentProcess()->HasSwitch("verbose")) {
338 // Redirect stderr to /dev/null, so that Chrome log spew doesn't confuse
339 // users.
340 devnull.reset(HANDLE_EINTR(open("/dev/null", O_WRONLY)));
341 if (!devnull.is_valid())
342 return Status(kUnknownError, "couldn't open /dev/null");
343 no_stderr.push_back(std::make_pair(devnull.get(), STDERR_FILENO));
344 options.fds_to_remap = &no_stderr;
346 #elif defined(OS_WIN)
347 if (!SwitchToUSKeyboardLayout())
348 VLOG(0) << "Can not set to US keyboard layout - Some keycodes may be"
349 "interpreted incorrectly";
350 #endif
352 #if defined(OS_WIN)
353 std::string command_string = base::WideToUTF8(command.GetCommandLineString());
354 #else
355 std::string command_string = command.GetCommandLineString();
356 #endif
357 VLOG(0) << "Launching chrome: " << command_string;
358 base::Process process = base::LaunchProcess(command, options);
359 if (!process.IsValid())
360 return Status(kUnknownError, "chrome failed to start");
362 scoped_ptr<DevToolsHttpClient> devtools_http_client;
363 status = WaitForDevToolsAndCheckVersion(
364 NetAddress(port), context_getter, socket_factory, &capabilities,
365 &devtools_http_client);
367 if (status.IsError()) {
368 int exit_code;
369 base::TerminationStatus chrome_status =
370 base::GetTerminationStatus(process.Handle(), &exit_code);
371 if (chrome_status != base::TERMINATION_STATUS_STILL_RUNNING) {
372 std::string termination_reason;
373 switch (chrome_status) {
374 case base::TERMINATION_STATUS_NORMAL_TERMINATION:
375 termination_reason = "exited normally";
376 break;
377 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION:
378 termination_reason = "exited abnormally";
379 break;
380 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED:
381 #if defined(OS_CHROMEOS)
382 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED_BY_OOM:
383 #endif
384 termination_reason = "was killed";
385 break;
386 case base::TERMINATION_STATUS_PROCESS_CRASHED:
387 termination_reason = "crashed";
388 break;
389 default:
390 termination_reason = "unknown";
391 break;
393 return Status(kUnknownError,
394 "Chrome failed to start: " + termination_reason);
396 if (!process.Terminate(0, true)) {
397 int exit_code;
398 if (base::GetTerminationStatus(process.Handle(), &exit_code) ==
399 base::TERMINATION_STATUS_STILL_RUNNING)
400 return Status(kUnknownError, "cannot kill Chrome", status);
402 return status;
405 scoped_ptr<DevToolsClient> devtools_websocket_client;
406 status = CreateBrowserwideDevToolsClientAndConnect(
407 NetAddress(port), capabilities.perf_logging_prefs, socket_factory,
408 *devtools_event_listeners, &devtools_websocket_client);
409 if (status.IsError()) {
410 LOG(WARNING) << "Browser-wide DevTools client failed to connect: "
411 << status.message();
414 scoped_ptr<ChromeDesktopImpl> chrome_desktop(
415 new ChromeDesktopImpl(devtools_http_client.Pass(),
416 devtools_websocket_client.Pass(),
417 *devtools_event_listeners,
418 port_reservation.Pass(),
419 process.Pass(),
420 command,
421 &user_data_dir,
422 &extension_dir));
423 for (size_t i = 0; i < extension_bg_pages.size(); ++i) {
424 VLOG(0) << "Waiting for extension bg page load: " << extension_bg_pages[i];
425 scoped_ptr<WebView> web_view;
426 Status status = chrome_desktop->WaitForPageToLoad(
427 extension_bg_pages[i], base::TimeDelta::FromSeconds(10), &web_view);
428 if (status.IsError()) {
429 return Status(kUnknownError,
430 "failed to wait for extension background page to load: " +
431 extension_bg_pages[i],
432 status);
435 *chrome = chrome_desktop.Pass();
436 return Status(kOk);
439 Status LaunchAndroidChrome(
440 URLRequestContextGetter* context_getter,
441 uint16 port,
442 scoped_ptr<PortReservation> port_reservation,
443 const SyncWebSocketFactory& socket_factory,
444 const Capabilities& capabilities,
445 ScopedVector<DevToolsEventListener>* devtools_event_listeners,
446 DeviceManager* device_manager,
447 scoped_ptr<Chrome>* chrome) {
448 Status status(kOk);
449 scoped_ptr<Device> device;
450 if (capabilities.android_device_serial.empty()) {
451 status = device_manager->AcquireDevice(&device);
452 } else {
453 status = device_manager->AcquireSpecificDevice(
454 capabilities.android_device_serial, &device);
456 if (status.IsError())
457 return status;
459 Switches switches(capabilities.switches);
460 for (auto common_switch : kCommonSwitches)
461 switches.SetUnparsedSwitch(common_switch);
462 for (auto android_switch : kAndroidSwitches)
463 switches.SetUnparsedSwitch(android_switch);
464 status = device->SetUp(capabilities.android_package,
465 capabilities.android_activity,
466 capabilities.android_process,
467 switches.ToString(),
468 capabilities.android_use_running_app,
469 port);
470 if (status.IsError()) {
471 device->TearDown();
472 return status;
475 scoped_ptr<DevToolsHttpClient> devtools_http_client;
476 status = WaitForDevToolsAndCheckVersion(NetAddress(port),
477 context_getter,
478 socket_factory,
479 &capabilities,
480 &devtools_http_client);
481 if (status.IsError()) {
482 device->TearDown();
483 return status;
486 scoped_ptr<DevToolsClient> devtools_websocket_client;
487 status = CreateBrowserwideDevToolsClientAndConnect(
488 NetAddress(port), capabilities.perf_logging_prefs, socket_factory,
489 *devtools_event_listeners, &devtools_websocket_client);
490 if (status.IsError()) {
491 LOG(WARNING) << "Browser-wide DevTools client failed to connect: "
492 << status.message();
495 chrome->reset(new ChromeAndroidImpl(devtools_http_client.Pass(),
496 devtools_websocket_client.Pass(),
497 *devtools_event_listeners,
498 port_reservation.Pass(),
499 device.Pass()));
500 return Status(kOk);
503 } // namespace
505 Status LaunchChrome(
506 URLRequestContextGetter* context_getter,
507 const SyncWebSocketFactory& socket_factory,
508 DeviceManager* device_manager,
509 PortServer* port_server,
510 PortManager* port_manager,
511 const Capabilities& capabilities,
512 ScopedVector<DevToolsEventListener>* devtools_event_listeners,
513 scoped_ptr<Chrome>* chrome) {
514 if (capabilities.IsRemoteBrowser()) {
515 return LaunchRemoteChromeSession(
516 context_getter, socket_factory,
517 capabilities, devtools_event_listeners, chrome);
520 uint16 port = 0;
521 scoped_ptr<PortReservation> port_reservation;
522 Status port_status(kOk);
524 if (capabilities.IsAndroid()) {
525 if (port_server)
526 port_status = port_server->ReservePort(&port, &port_reservation);
527 else
528 port_status = port_manager->ReservePortFromPool(&port, &port_reservation);
529 if (port_status.IsError())
530 return Status(kUnknownError, "cannot reserve port for Chrome",
531 port_status);
532 return LaunchAndroidChrome(context_getter,
533 port,
534 port_reservation.Pass(),
535 socket_factory,
536 capabilities,
537 devtools_event_listeners,
538 device_manager,
539 chrome);
540 } else {
541 if (port_server)
542 port_status = port_server->ReservePort(&port, &port_reservation);
543 else
544 port_status = port_manager->ReservePort(&port, &port_reservation);
545 if (port_status.IsError())
546 return Status(kUnknownError, "cannot reserve port for Chrome",
547 port_status);
548 return LaunchDesktopChrome(context_getter,
549 port,
550 port_reservation.Pass(),
551 socket_factory,
552 capabilities,
553 devtools_event_listeners,
554 chrome);
558 namespace internal {
560 void ConvertHexadecimalToIDAlphabet(std::string* id) {
561 for (size_t i = 0; i < id->size(); ++i) {
562 int val;
563 if (base::HexStringToInt(base::StringPiece(id->begin() + i,
564 id->begin() + i + 1),
565 &val)) {
566 (*id)[i] = val + 'a';
567 } else {
568 (*id)[i] = 'a';
573 std::string GenerateExtensionId(const std::string& input) {
574 uint8 hash[16];
575 crypto::SHA256HashString(input, hash, sizeof(hash));
576 std::string output = base::ToLowerASCII(base::HexEncode(hash, sizeof(hash)));
577 ConvertHexadecimalToIDAlphabet(&output);
578 return output;
581 Status GetExtensionBackgroundPage(const base::DictionaryValue* manifest,
582 const std::string& id,
583 std::string* bg_page) {
584 std::string bg_page_name;
585 bool persistent = true;
586 manifest->GetBoolean("background.persistent", &persistent);
587 const base::Value* unused_value;
588 if (manifest->Get("background.scripts", &unused_value))
589 bg_page_name = "_generated_background_page.html";
590 manifest->GetString("background.page", &bg_page_name);
591 manifest->GetString("background_page", &bg_page_name);
592 if (bg_page_name.empty() || !persistent)
593 return Status(kOk);
594 *bg_page = "chrome-extension://" + id + "/" + bg_page_name;
595 return Status(kOk);
598 Status ProcessExtension(const std::string& extension,
599 const base::FilePath& temp_dir,
600 base::FilePath* path,
601 std::string* bg_page) {
602 // Decodes extension string.
603 // Some WebDriver client base64 encoders follow RFC 1521, which require that
604 // 'encoded lines be no more than 76 characters long'. Just remove any
605 // newlines.
606 std::string extension_base64;
607 base::RemoveChars(extension, "\n", &extension_base64);
608 std::string decoded_extension;
609 if (!base::Base64Decode(extension_base64, &decoded_extension))
610 return Status(kUnknownError, "cannot base64 decode");
612 // If the file is a crx file, extract the extension's ID from its public key.
613 // Otherwise generate a random public key and use its derived extension ID.
614 std::string public_key;
615 std::string magic_header = decoded_extension.substr(0, 4);
616 if (magic_header.size() != 4)
617 return Status(kUnknownError, "cannot extract magic number");
619 const bool is_crx_file = magic_header == "Cr24";
621 if (is_crx_file) {
622 // Assume a CRX v2 file - see https://developer.chrome.com/extensions/crx.
623 std::string key_len_str = decoded_extension.substr(8, 4);
624 if (key_len_str.size() != 4)
625 return Status(kUnknownError, "cannot extract public key length");
626 uint32 key_len = *reinterpret_cast<const uint32*>(key_len_str.c_str());
627 public_key = decoded_extension.substr(16, key_len);
628 if (key_len != public_key.size())
629 return Status(kUnknownError, "invalid public key length");
630 } else {
631 // Not a CRX file. Generate RSA keypair to get a valid extension id.
632 scoped_ptr<crypto::RSAPrivateKey> key_pair(
633 crypto::RSAPrivateKey::Create(2048));
634 if (!key_pair)
635 return Status(kUnknownError, "cannot generate RSA key pair");
636 std::vector<uint8> public_key_vector;
637 if (!key_pair->ExportPublicKey(&public_key_vector))
638 return Status(kUnknownError, "cannot extract public key");
639 public_key =
640 std::string(reinterpret_cast<char*>(&public_key_vector.front()),
641 public_key_vector.size());
643 std::string public_key_base64;
644 base::Base64Encode(public_key, &public_key_base64);
645 std::string id = GenerateExtensionId(public_key);
647 // Unzip the crx file.
648 base::ScopedTempDir temp_crx_dir;
649 if (!temp_crx_dir.CreateUniqueTempDir())
650 return Status(kUnknownError, "cannot create temp dir");
651 base::FilePath extension_crx = temp_crx_dir.path().AppendASCII("temp.crx");
652 int size = static_cast<int>(decoded_extension.length());
653 if (base::WriteFile(extension_crx, decoded_extension.c_str(), size) !=
654 size) {
655 return Status(kUnknownError, "cannot write file");
657 base::FilePath extension_dir = temp_dir.AppendASCII("extension_" + id);
658 if (!zip::Unzip(extension_crx, extension_dir))
659 return Status(kUnknownError, "cannot unzip");
661 // Parse the manifest and set the 'key' if not already present.
662 base::FilePath manifest_path(extension_dir.AppendASCII("manifest.json"));
663 std::string manifest_data;
664 if (!base::ReadFileToString(manifest_path, &manifest_data))
665 return Status(kUnknownError, "cannot read manifest");
666 scoped_ptr<base::Value> manifest_value =
667 base::JSONReader::Read(manifest_data);
668 base::DictionaryValue* manifest;
669 if (!manifest_value || !manifest_value->GetAsDictionary(&manifest))
670 return Status(kUnknownError, "invalid manifest");
672 std::string manifest_key_base64;
673 if (manifest->GetString("key", &manifest_key_base64)) {
674 // If there is a key in both the header and the manifest, use the key in the
675 // manifest. This allows chromedriver users users who generate dummy crxs
676 // to set the manifest key and have a consistent ID.
677 std::string manifest_key;
678 if (!base::Base64Decode(manifest_key_base64, &manifest_key))
679 return Status(kUnknownError, "'key' in manifest is not base64 encoded");
680 std::string manifest_id = GenerateExtensionId(manifest_key);
681 if (id != manifest_id) {
682 if (is_crx_file) {
683 LOG(WARNING)
684 << "Public key in crx header is different from key in manifest"
685 << std::endl << "key from header: " << public_key_base64
686 << std::endl << "key from manifest: " << manifest_key_base64
687 << std::endl << "generated extension id from header key: " << id
688 << std::endl << "generated extension id from manifest key: "
689 << manifest_id;
691 id = manifest_id;
693 } else {
694 manifest->SetString("key", public_key_base64);
695 base::JSONWriter::Write(*manifest, &manifest_data);
696 if (base::WriteFile(
697 manifest_path, manifest_data.c_str(), manifest_data.size()) !=
698 static_cast<int>(manifest_data.size())) {
699 return Status(kUnknownError, "cannot add 'key' to manifest");
703 // Get extension's background page URL, if there is one.
704 std::string bg_page_tmp;
705 Status status = GetExtensionBackgroundPage(manifest, id, &bg_page_tmp);
706 if (status.IsError())
707 return status;
709 *path = extension_dir;
710 if (bg_page_tmp.size())
711 *bg_page = bg_page_tmp;
712 return Status(kOk);
715 void UpdateExtensionSwitch(Switches* switches,
716 const char name[],
717 const base::FilePath::StringType& extension) {
718 base::FilePath::StringType value = switches->GetSwitchValueNative(name);
719 if (value.length())
720 value += FILE_PATH_LITERAL(",");
721 value += extension;
722 switches->SetSwitch(name, value);
725 Status ProcessExtensions(const std::vector<std::string>& extensions,
726 const base::FilePath& temp_dir,
727 bool include_automation_extension,
728 Switches* switches,
729 std::vector<std::string>* bg_pages) {
730 std::vector<std::string> bg_pages_tmp;
731 std::vector<base::FilePath::StringType> extension_paths;
732 for (size_t i = 0; i < extensions.size(); ++i) {
733 base::FilePath path;
734 std::string bg_page;
735 Status status = ProcessExtension(extensions[i], temp_dir, &path, &bg_page);
736 if (status.IsError()) {
737 return Status(
738 kUnknownError,
739 base::StringPrintf("cannot process extension #%" PRIuS, i + 1),
740 status);
742 extension_paths.push_back(path.value());
743 if (bg_page.length())
744 bg_pages_tmp.push_back(bg_page);
747 if (include_automation_extension) {
748 base::FilePath automation_extension;
749 Status status = UnpackAutomationExtension(temp_dir, &automation_extension);
750 if (status.IsError())
751 return status;
752 if (switches->HasSwitch("disable-extensions")) {
753 UpdateExtensionSwitch(switches, "load-component-extension",
754 automation_extension.value());
755 } else {
756 extension_paths.push_back(automation_extension.value());
760 if (extension_paths.size()) {
761 base::FilePath::StringType extension_paths_value = base::JoinString(
762 extension_paths, base::FilePath::StringType(1, ','));
763 UpdateExtensionSwitch(switches, "load-extension", extension_paths_value);
765 bg_pages->swap(bg_pages_tmp);
766 return Status(kOk);
769 Status WritePrefsFile(
770 const std::string& template_string,
771 const base::DictionaryValue* custom_prefs,
772 const base::FilePath& path) {
773 int code;
774 std::string error_msg;
775 scoped_ptr<base::Value> template_value(
776 base::JSONReader::DeprecatedReadAndReturnError(template_string, 0, &code,
777 &error_msg));
778 base::DictionaryValue* prefs;
779 if (!template_value || !template_value->GetAsDictionary(&prefs)) {
780 return Status(kUnknownError,
781 "cannot parse internal JSON template: " + error_msg);
784 if (custom_prefs) {
785 for (base::DictionaryValue::Iterator it(*custom_prefs); !it.IsAtEnd();
786 it.Advance()) {
787 prefs->Set(it.key(), it.value().DeepCopy());
791 std::string prefs_str;
792 base::JSONWriter::Write(*prefs, &prefs_str);
793 VLOG(0) << "Populating " << path.BaseName().value()
794 << " file: " << PrettyPrintValue(*prefs);
795 if (static_cast<int>(prefs_str.length()) != base::WriteFile(
796 path, prefs_str.c_str(), prefs_str.length())) {
797 return Status(kUnknownError, "failed to write prefs file");
799 return Status(kOk);
802 Status PrepareUserDataDir(
803 const base::FilePath& user_data_dir,
804 const base::DictionaryValue* custom_prefs,
805 const base::DictionaryValue* custom_local_state) {
806 base::FilePath default_dir =
807 user_data_dir.AppendASCII(chrome::kInitialProfile);
808 if (!base::CreateDirectory(default_dir))
809 return Status(kUnknownError, "cannot create default profile directory");
811 std::string preferences;
812 base::FilePath preferences_path =
813 default_dir.Append(chrome::kPreferencesFilename);
815 if (base::PathExists(preferences_path))
816 base::ReadFileToString(preferences_path, &preferences);
817 else
818 preferences = kPreferences;
820 Status status =
821 WritePrefsFile(preferences,
822 custom_prefs,
823 default_dir.Append(chrome::kPreferencesFilename));
824 if (status.IsError())
825 return status;
827 std::string local_state;
828 base::FilePath local_state_path =
829 user_data_dir.Append(chrome::kLocalStateFilename);
831 if (base::PathExists(local_state_path))
832 base::ReadFileToString(local_state_path, &local_state);
833 else
834 local_state = kLocalState;
836 status = WritePrefsFile(local_state,
837 custom_local_state,
838 user_data_dir.Append(chrome::kLocalStateFilename));
839 if (status.IsError())
840 return status;
842 // Write empty "First Run" file, otherwise Chrome will wipe the default
843 // profile that was written.
844 if (base::WriteFile(
845 user_data_dir.Append(chrome::kFirstRunSentinel), "", 0) != 0) {
846 return Status(kUnknownError, "failed to write first run file");
848 return Status(kOk);
851 } // namespace internal