blob: 4dabbbeb7d14ac8d8a219604bada2c9a371010a9 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2022 The Chromium Authors
Randolf Jung7fbab06e2022-02-23 20:03:452// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <memory>
6
7#include "chrome/browser/devtools/protocol/emulation_handler.h"
8#include "chrome/browser/infobars/confirm_infobar_creator.h"
9#include "chrome/browser/ui/startup/automation_infobar_delegate.h"
10
11EmulationHandler::EmulationHandler(content::DevToolsAgentHost* agent_host,
12 protocol::UberDispatcher* dispatcher)
13 : agent_host_(agent_host) {
14 protocol::Emulation::Dispatcher::wire(dispatcher, this);
15}
Randolf Jungffa0adb2022-03-04 12:25:3016EmulationHandler::~EmulationHandler() {
17 SetAutomationOverride(false);
18}
Randolf Jung7fbab06e2022-02-23 20:03:4519
20protocol::Response EmulationHandler::Disable() {
21 SetAutomationOverride(false);
22 return protocol::Response::FallThrough();
23}
24
25protocol::Response EmulationHandler::SetAutomationOverride(bool enabled) {
Randolf Jungffa0adb2022-03-04 12:25:3026 if (!enabled) {
27 if (automation_info_bar_) {
28 automation_info_bar_->RemoveSelf();
29 }
Randolf Jung4896cf612022-02-28 10:40:4330 return protocol::Response::FallThrough();
Randolf Jung7fbab06e2022-02-23 20:03:4531 }
Randolf Jungffa0adb2022-03-04 12:25:3032 if (automation_info_bar_) {
33 return protocol::Response::FallThrough();
34 }
35
36 infobars::ContentInfoBarManager* info_bar_manager =
37 GetContentInfoBarManager();
Randolf Jung7fbab06e2022-02-23 20:03:4538 if (!info_bar_manager) {
39 // Implies the web content cannot have an info bar attached. A priori, the
40 // automation override doesn't matter on the chrome layer.
41 return protocol::Response::FallThrough();
42 }
43
Randolf Jungffa0adb2022-03-04 12:25:3044 // Note since the observer removes itself when the info bar is removed, the
Randolf Jung8699ec62022-03-10 19:27:5345 // observer is added at most once because of the info bar nullity check
46 // above.
Randolf Jung7fbab06e2022-02-23 20:03:4547 automation_info_bar_ = AutomationInfoBarDelegate::Create(info_bar_manager);
Randolf Jung8699ec62022-03-10 19:27:5348 if (automation_info_bar_) {
49 info_bar_manager->AddObserver(this);
50 }
Randolf Jung7fbab06e2022-02-23 20:03:4551 return protocol::Response::FallThrough();
52}
Randolf Jungffa0adb2022-03-04 12:25:3053
54infobars::ContentInfoBarManager* EmulationHandler::GetContentInfoBarManager() {
55 content::WebContents* web_contents = agent_host_->GetWebContents();
56 if (!web_contents) {
57 return nullptr;
58 }
59 return infobars::ContentInfoBarManager::FromWebContents(
60 web_contents->GetOutermostWebContents());
61}
62
63void EmulationHandler::OnInfoBarRemoved(infobars::InfoBar* infobar,
64 bool animate) {
65 if (automation_info_bar_ == infobar) {
66 infobar->owner()->RemoveObserver(this);
67 automation_info_bar_ = nullptr;
68 }
69}