/*
-----------------------------------------------------------------------------
This source file is part of OpenSpace3D
For the latest info, see http://www.openspace3d.com

Copyright (c) 2011 I-maginer

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA 02111-1307, USA, or go to
http://www.gnu.org/copyleft/lesser.txt
-----------------------------------------------------------------------------
*/

#include "embeddedWebNavigatorV8ContextHandler.h"
#include "embeddedWebNavigatorClient.h"
#include "embeddedWebNavigatorScolExternalCallHandler.h"

namespace Scol
{
  namespace EmbeddedWebNavigator
  {

WebNavigatorV8ContextHandler::WebNavigatorV8ContextHandler(CefRefPtr<WebNavigatorClient>& parentWebNavigatorClientInstance, const ScolWindowHandle& scolMainWindow) : WebNavigatorHandler(parentWebNavigatorClientInstance, scolMainWindow)
{
}

WebNavigatorV8ContextHandler::WebNavigatorV8ContextHandler() : WebNavigatorHandler(CefRefPtr<WebNavigatorClient>(), 0)
{
  // Forbiden ctor.
}

WebNavigatorV8ContextHandler::~WebNavigatorV8ContextHandler()
{
}

void WebNavigatorV8ContextHandler::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
{
  // Global v8 object
  CefRefPtr<CefV8Value> object = context->GetGlobal();

  // Create an instance of ScolExternalCallHandler object. No need to delete, because it's handled with a CefRefPtr
  CefRefPtr<CefV8Handler> scolExternalCallHandler = new ScolExternalCallHandler(parentWebNavigatorClient, scolMainWindowHandle);

  // Create the "scolExternalCall" function.
  CefRefPtr<CefV8Value> scolExternalCallFunction = CefV8Value::CreateFunction("scolExternalCall", scolExternalCallHandler);

  // Add the "scolExternalCall" function to the "window" object.
  object->SetValue("scolExternalCall", scolExternalCallFunction, V8_PROPERTY_ATTRIBUTE_NONE);


  // Create an instance of ScolExternalCallHandler object. No need to delete, because it's handled with a CefRefPtr
  CefRefPtr<CefV8Handler> scolExternalCallHandlerStr = new ScolExternalCallHandler(parentWebNavigatorClient, scolMainWindowHandle);

  // Create the "scolExternalCallStr" function.
  CefRefPtr<CefV8Value> scolExternalCallFunctionStr = CefV8Value::CreateFunction("scolExternalCallStr", scolExternalCallHandlerStr);

  // Add the "scolExternalCallStr" function to the "window" object.
  object->SetValue("scolExternalCallStr", scolExternalCallFunctionStr, V8_PROPERTY_ATTRIBUTE_NONE);
}

void WebNavigatorV8ContextHandler::OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
{
  // TODO
}

  }
}