// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/chrome_constrained_window_views_client.h"

#include "base/memory/ptr_util.h"
#include "build/build_config.h"
#include "chrome/browser/platform_util.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/browser_window/public/global_browser_collection.h"
#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
#include "components/constrained_window/modal_dialog_host_property.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"

#if defined(USE_AURA)
#include "ui/aura/window.h"
#endif

namespace {

class ChromeConstrainedWindowViewsClient
    : public constrained_window::ConstrainedWindowViewsClient {
 public:
  ChromeConstrainedWindowViewsClient() = default;

  ChromeConstrainedWindowViewsClient(
      const ChromeConstrainedWindowViewsClient&) = delete;
  ChromeConstrainedWindowViewsClient& operator=(
      const ChromeConstrainedWindowViewsClient&) = delete;

  ~ChromeConstrainedWindowViewsClient() override = default;

 private:
  // ConstrainedWindowViewsClient:
  web_modal::ModalDialogHost* GetModalDialogHost(
      gfx::NativeWindow parent) override {
#if defined(USE_AURA)
    if (parent) {
      web_modal::ModalDialogHost* host =
          parent->GetProperty(constrained_window::kModalDialogHostKey);
      if (host) {
        return host;
      }
    }
#endif
    // Get the browser dialog management and hosting components from |parent|.
    BrowserWindowInterface* const browser =
        GlobalBrowserCollection::GetInstance()->FindBrowserWithWindow(parent);
    if (browser) {
      return browser->GetWebContentsModalDialogHostForWindow();
    }
    return nullptr;
  }

  gfx::NativeView GetDialogHostView(gfx::NativeWindow parent) override {
    return platform_util::GetViewForWindow(parent);
  }
};

}  // namespace

std::unique_ptr<constrained_window::ConstrainedWindowViewsClient>
CreateChromeConstrainedWindowViewsClient() {
  return std::make_unique<ChromeConstrainedWindowViewsClient>();
}
