Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 2 | // 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 | |
| 11 | EmulationHandler::EmulationHandler(content::DevToolsAgentHost* agent_host, |
| 12 | protocol::UberDispatcher* dispatcher) |
| 13 | : agent_host_(agent_host) { |
| 14 | protocol::Emulation::Dispatcher::wire(dispatcher, this); |
| 15 | } |
Randolf Jung | ffa0adb | 2022-03-04 12:25:30 | [diff] [blame] | 16 | EmulationHandler::~EmulationHandler() { |
| 17 | SetAutomationOverride(false); |
| 18 | } |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 19 | |
| 20 | protocol::Response EmulationHandler::Disable() { |
| 21 | SetAutomationOverride(false); |
| 22 | return protocol::Response::FallThrough(); |
| 23 | } |
| 24 | |
| 25 | protocol::Response EmulationHandler::SetAutomationOverride(bool enabled) { |
Randolf Jung | ffa0adb | 2022-03-04 12:25:30 | [diff] [blame] | 26 | if (!enabled) { |
| 27 | if (automation_info_bar_) { |
| 28 | automation_info_bar_->RemoveSelf(); |
| 29 | } |
Randolf Jung | 4896cf61 | 2022-02-28 10:40:43 | [diff] [blame] | 30 | return protocol::Response::FallThrough(); |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 31 | } |
Randolf Jung | ffa0adb | 2022-03-04 12:25:30 | [diff] [blame] | 32 | if (automation_info_bar_) { |
| 33 | return protocol::Response::FallThrough(); |
| 34 | } |
| 35 | |
| 36 | infobars::ContentInfoBarManager* info_bar_manager = |
| 37 | GetContentInfoBarManager(); |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 38 | 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 Jung | ffa0adb | 2022-03-04 12:25:30 | [diff] [blame] | 44 | // Note since the observer removes itself when the info bar is removed, the |
Randolf Jung | 8699ec6 | 2022-03-10 19:27:53 | [diff] [blame] | 45 | // observer is added at most once because of the info bar nullity check |
| 46 | // above. |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 47 | automation_info_bar_ = AutomationInfoBarDelegate::Create(info_bar_manager); |
Randolf Jung | 8699ec6 | 2022-03-10 19:27:53 | [diff] [blame] | 48 | if (automation_info_bar_) { |
| 49 | info_bar_manager->AddObserver(this); |
| 50 | } |
Randolf Jung | 7fbab06e | 2022-02-23 20:03:45 | [diff] [blame] | 51 | return protocol::Response::FallThrough(); |
| 52 | } |
Randolf Jung | ffa0adb | 2022-03-04 12:25:30 | [diff] [blame] | 53 | |
| 54 | infobars::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 | |
| 63 | void 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 | } |