// Copyright 2015 The Chromium Embedded Framework 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 "libcef/browser/browser_platform_delegate.h"

#if defined(OS_WIN)
#include "libcef/browser/native/browser_platform_delegate_native_win.h"
#include "libcef/browser/osr/browser_platform_delegate_osr_win.h"
#elif defined(OS_MACOSX)
#include "libcef/browser/native/browser_platform_delegate_native_mac.h"
#include "libcef/browser/osr/browser_platform_delegate_osr_mac.h"
#elif defined(OS_LINUX)
#include "libcef/browser/native/browser_platform_delegate_native_linux.h"
#include "libcef/browser/osr/browser_platform_delegate_osr_linux.h"
#else
#error A delegate implementation is not available for your platform.
#endif

namespace {

scoped_ptr<CefBrowserPlatformDelegateNative> CreateNativeDelegate(
    const CefWindowInfo& window_info) {
#if defined(OS_WIN)
  return make_scoped_ptr(new CefBrowserPlatformDelegateNativeWin(window_info));
#elif defined(OS_MACOSX)
  return make_scoped_ptr(new CefBrowserPlatformDelegateNativeMac(window_info));
#elif defined(OS_LINUX)
  return make_scoped_ptr(
      new CefBrowserPlatformDelegateNativeLinux(window_info));
#endif
}

scoped_ptr<CefBrowserPlatformDelegateOsr> CreateOSRDelegate(
    scoped_ptr<CefBrowserPlatformDelegateNative> native_delegate) {
#if defined(OS_WIN)
  return make_scoped_ptr(
        new CefBrowserPlatformDelegateOsrWin(native_delegate.Pass()));
#elif defined(OS_MACOSX)
  return make_scoped_ptr(
        new CefBrowserPlatformDelegateOsrMac(native_delegate.Pass()));
#elif defined(OS_LINUX)
  return make_scoped_ptr(
        new CefBrowserPlatformDelegateOsrLinux(native_delegate.Pass()));
#endif
}

}  // namespace

// static
scoped_ptr<CefBrowserPlatformDelegate> CefBrowserPlatformDelegate::Create(
    const CefWindowInfo& window_info,
    const CefBrowserSettings& settings,
    CefRefPtr<CefClient> client) {
  scoped_ptr<CefBrowserPlatformDelegateNative> native_delegate =
      CreateNativeDelegate(window_info);
  if (window_info.windowless_rendering_enabled &&
      client->GetRenderHandler().get()) {
    return CreateOSRDelegate(native_delegate.Pass());
  }
  return native_delegate.Pass();
}