summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/blocked_content/app_modal_dialog_helper.cc
blob: fea3ec37b5699dedf8f3e2af672772334c2cb5e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/blocked_content/app_modal_dialog_helper.h"

#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"

AppModalDialogHelper::AppModalDialogHelper(content::WebContents* dialog_host)
    : popup_(nullptr) {
  // If the WebContents that triggered this dialog is not currently focused, we
  // want to store a potential popup here to restore it after the dialog was
  // closed.
  chrome::HostDesktopType desktop_type =
      chrome::GetHostDesktopTypeForNativeView(dialog_host->GetNativeView());
  Browser* active_browser =
      BrowserList::GetInstance(desktop_type)->GetLastActive();
  if (active_browser) {
    content::WebContents* active_web_contents =
        active_browser->tab_strip_model()->GetActiveWebContents();
    if (active_browser->is_type_popup() && active_web_contents &&
        active_web_contents->GetOpener() == dialog_host) {
      // It's indeed a popup from the dialog opening WebContents. Store it, so
      // we can focus it later.
      popup_ = active_web_contents;
      Observe(popup_);
    }
  }
}

AppModalDialogHelper::~AppModalDialogHelper() {
  if (popup_)
    popup_->GetDelegate()->ActivateContents(popup_);
}

void AppModalDialogHelper::WebContentsDestroyed() {
  popup_ = nullptr;
}