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 "base/values.h"
6 #include "chrome/test/chromedriver/chrome/device_metrics.h"
7 #include "chrome/test/chromedriver/chrome/devtools_client.h"
8 #include "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h"
9 #include "chrome/test/chromedriver/chrome/status.h"
10 #include "chrome/test/chromedriver/chrome/version.h"
12 MobileEmulationOverrideManager::MobileEmulationOverrideManager(
13 DevToolsClient* client,
14 const DeviceMetrics* device_metrics,
15 const BrowserInfo* browser_info)
17 overridden_device_metrics_(device_metrics),
18 browser_info_(browser_info) {
19 if (overridden_device_metrics_)
20 client_->AddListener(this);
23 MobileEmulationOverrideManager::~MobileEmulationOverrideManager() {
26 Status MobileEmulationOverrideManager::OnConnected(DevToolsClient* client) {
27 return ApplyOverrideIfNeeded();
30 Status MobileEmulationOverrideManager::OnEvent(
31 DevToolsClient* client,
32 const std::string& method,
33 const base::DictionaryValue& params) {
34 if (method == "Page.frameNavigated") {
35 const base::Value* unused_value;
36 if (!params.Get("frame.parentId", &unused_value))
37 return ApplyOverrideIfNeeded();
42 Status MobileEmulationOverrideManager::ApplyOverrideIfNeeded() {
43 if (overridden_device_metrics_ == NULL)
46 // Old revisions of Blink expect a parameter named |emulateViewport| but in
47 // Blink revision 177367 (Chromium revision 281046, build number 2081) this
48 // was renamed to |mobile|.
49 std::string mobile_param_name = "mobile";
50 if (browser_info_->browser_name == "chrome") {
51 if (browser_info_->build_no <= 2081)
52 mobile_param_name = "emulateViewport";
54 if (browser_info_->blink_revision < 177367)
55 mobile_param_name = "emulateViewport";
58 base::DictionaryValue params;
59 params.SetInteger("width", overridden_device_metrics_->width);
60 params.SetInteger("height", overridden_device_metrics_->height);
61 params.SetDouble("deviceScaleFactor",
62 overridden_device_metrics_->device_scale_factor);
63 params.SetBoolean(mobile_param_name, overridden_device_metrics_->mobile);
64 params.SetBoolean("fitWindow", overridden_device_metrics_->fit_window);
65 params.SetBoolean("textAutosizing",
66 overridden_device_metrics_->text_autosizing);
67 params.SetDouble("fontScaleFactor",
68 overridden_device_metrics_->font_scale_factor);
69 return client_->SendCommand("Page.setDeviceMetricsOverride", params);