/*
	This file is a part of Awesomium, a library that makes it easy for 
	developers to embed web-content in their applications.

	Copyright (C) 2009 Adam J. Simmons

	Project Website:
	<http://princeofcode.com/awesomium.php>

	This library 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.1 of the License, or (at your option) any later version.

	This library 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 library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
	02110-1301 USA
*/

#ifndef __WEBCOREPROXY_H__
#define __WEBCOREPROXY_H__

#include "base/ref_counted.h"
#include "base/scoped_temp_dir.h"
#include "base/timer.h"
#include "webkit/glue/webkit_glue.h"
#include "webkit/glue/webkitclient_impl.h"
#include "WebCString.h"
#include "WebKit.h"
#include "WebString.h"
#include "WebURL.h"
#include "webkit/glue/simple_webmimeregistry_impl.h"
#include "base/waitable_event.h"
#include <string>
#include <list>


namespace base { class Thread; }
namespace Awesomium { class WebCore; }

class WebCoreProxy : public base::RefCountedThreadSafe<WebCoreProxy>,
	public webkit_glue::WebKitClientImpl
{
public:
	WebCoreProxy(base::Thread* coreThread, bool pluginsEnabled);
	~WebCoreProxy();
	
	// Awesomium specific

	void startup();
	void pause();
	void resume();

  // WebKitClientImpl

		bool sandboxEnabled();
	
		WebKit::WebMimeRegistry* mimeRegistry();
	
		WebKit::WebSandboxSupport* sandboxSupport();
	
		WebKit::WebMessagePortChannel* createMessagePortChannel();

		//mgdesign
		//chrome v4
		WebKit::WebStorageNamespace* WebCoreProxy::createLocalStorageNamespace(	const WebKit::WebString& path, unsigned int);
		//chrome v5
		//WebKit::WebStorageNamespace* WebCoreProxy::createLocalStorageNamespace(const WebKit::WebString& path);

	  //mgdesign, to remove in chrome v5
	  WebKit::WebStorageNamespace* createSessionStorageNamespace();

		bool getFileSize(const WebKit::WebString& path, long long& result);

		unsigned long long visitedLinkHash(const char* canonicalURL, size_t length);
	
		bool isLinkVisited(unsigned long long linkHash);
	
		void setCookies(const WebKit::WebURL& url, const WebKit::WebURL& policy_url,const WebKit::WebString& value);
	
		WebKit::WebString cookies(const WebKit::WebURL& url, const WebKit::WebURL& policy_url);
	
		//mgdesign, to remove in chrome v5:
		void prefetchHostName(const WebKit::WebString&);

		WebKit::WebData loadResource(const char* name);
	
		WebKit::WebString defaultLocale();

		WebKit::WebClipboard *clipboard();

/*
  // mgdesign : not implemented from webkit_glue::WebKitClientImpl
	WebKit::WebKitClient::FileHandle databaseOpenFile(const WebKit::WebString& vfs_file_name, int desired_flags, WebKit::WebKitClient::FileHandle* dir_handle);
	int databaseDeleteFile(const WebKit::WebString& vfs_file_name,bool sync_dir);
  long databaseGetFileAttributes( const WebKit::WebString& vfs_file_name);
  long long databaseGetFileSize( const WebKit::WebString& vfs_file_name);
	void WebKit::WebApplicationCacheHost* createApplicationCacheHost(WebKit::WebApplicationCacheHostClient* client);
*/

	// mgdesign: to remove in chrome v5
	void dispatchStorageEvent(const WebKit::WebString& key,const WebKit::WebString& old_value, const WebKit::WebString& new_value,const WebKit::WebString& origin, const WebKit::WebURL& url,   bool is_local_storage);
	WebKit::WebSharedWorkerRepository* sharedWorkerRepository();

	double currentTime();

protected:
  void initWebKit();
	void asyncStartup();
	
	void asyncPause();

	void pumpPluginMessages();
	void pumpThrottledMessages();
	void purgePluginMessages();

	class Clipboard;
	Clipboard *webclipboard;

	base::Thread* coreThread;
	bool pluginsEnabled;
	base::RepeatingTimer<WebCoreProxy> pluginMessageTimer;
	webkit_glue::SimpleWebMimeRegistryImpl mime_registry_;
	base::WaitableEvent threadWaitEvent, pauseRequestEvent;

	ScopedTempDir appcache_dir_;
	double (*externCurrentTimeFunction_)();

#if defined(WIN32)
	std::list<MSG> throttledMessages;
#endif

	friend class Awesomium::WebCore;
};

#endif
