1cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// found in the LICENSE file.
4cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
5ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch#include "chrome/test/chromedriver/chrome/mobile_emulation_override_manager.h"
6ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/values.h"
8ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch#include "chrome/test/chromedriver/chrome/browser_info.h"
9cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/chromedriver/chrome/device_metrics.h"
10cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/chromedriver/chrome/devtools_client.h"
11cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/test/chromedriver/chrome/status.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MobileEmulationOverrideManager::MobileEmulationOverrideManager(
14cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DevToolsClient* client,
15116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const DeviceMetrics* device_metrics,
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const BrowserInfo* browser_info)
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    : client_(client),
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      overridden_device_metrics_(device_metrics),
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      browser_info_(browser_info) {
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (overridden_device_metrics_)
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    client_->AddListener(this);
22cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
24cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)MobileEmulationOverrideManager::~MobileEmulationOverrideManager() {
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
27cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)Status MobileEmulationOverrideManager::OnConnected(DevToolsClient* client) {
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return ApplyOverrideIfNeeded();
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)Status MobileEmulationOverrideManager::OnEvent(
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    DevToolsClient* client,
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const std::string& method,
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::DictionaryValue& params) {
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (method == "Page.frameNavigated") {
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    const base::Value* unused_value;
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (!params.Get("frame.parentId", &unused_value))
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      return ApplyOverrideIfNeeded();
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return Status(kOk);
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)Status MobileEmulationOverrideManager::ApplyOverrideIfNeeded() {
44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (overridden_device_metrics_ == NULL)
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return Status(kOk);
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Old revisions of Blink expect a parameter named |emulateViewport| but in
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Blink revision 177367 (Chromium revision 281046, build number 2081) this
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // was renamed to |mobile|.
505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string mobile_param_name = "mobile";
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (browser_info_->browser_name == "chrome") {
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (browser_info_->build_no <= 2081)
535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      mobile_param_name = "emulateViewport";
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  } else {
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (browser_info_->blink_revision < 177367)
565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      mobile_param_name = "emulateViewport";
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
58116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  base::DictionaryValue params;
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetInteger("width", overridden_device_metrics_->width);
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetInteger("height", overridden_device_metrics_->height);
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetDouble("deviceScaleFactor",
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   overridden_device_metrics_->device_scale_factor);
645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  params.SetBoolean(mobile_param_name, overridden_device_metrics_->mobile);
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetBoolean("fitWindow", overridden_device_metrics_->fit_window);
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetBoolean("textAutosizing",
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                    overridden_device_metrics_->text_autosizing);
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  params.SetDouble("fontScaleFactor",
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                   overridden_device_metrics_->font_scale_factor);
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return client_->SendCommand("Page.setDeviceMetricsOverride", params);
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
72