<commit_before>// Copyright (c) 2011 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 "client_handler.h"
#include <stdio.h>
#include <sstream>
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "cefclient.h"
#include "native_handler.h"

ClientHandler::ClientHandler() :
		m_MainHwnd(NULL), m_BrowserHwnd(NULL), m_bFormElementHasFocus(false) {
}

ClientHandler::~ClientHandler() {
}

void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	AutoLock lock_scope(this);
	if (!m_Browser.get()) {
		// We need to keep the main child window, but not popup windows
		m_Browser = browser;
		m_BrowserHwnd = browser->GetWindowHandle();
	}
}

bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle()) {
		// Since the main window contains the browser window, we need to close
		// the parent window instead of the browser window.
		CloseMainWindow();

		// Return true here so that we can skip closing the browser window
		// in this pass. (It will be destroyed due to the call to close
		// the parent above.)
		return true;
	}

	// A popup browser window is not contained in another window, so we can let
	// these windows close by themselves.
	return false;
}

void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle()) {
		// Free the browser pointer so that the browser can be destroyed
		m_Browser = NULL;
	}
}

void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
		CefRefPtr<CefV8Context> context = frame->GetV8Context();
		CefRefPtr<CefV8Value> global = context->GetGlobal();
		context->Enter();

		CefRefPtr<CefV8Value> windowNumber = CefV8Value::CreateInt(0);
		global->SetValue("$windowNumber", windowNumber,
				V8_PROPERTY_ATTRIBUTE_NONE);

		std::string path;
		if (m_nativeHandler)
			path = m_nativeHandler->path;
		else
			path.append(PathToOpen());

		CefRefPtr<CefV8Value> pathToOpen = CefV8Value::CreateString(path);
		global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<NativeHandler> nativeHandler = new NativeHandler();
		nativeHandler->window = window;
		nativeHandler->path = path;
		global->SetValue("$native", nativeHandler->object,
				V8_PROPERTY_ATTRIBUTE_NONE);
		m_nativeHandler = nativeHandler;

		CefRefPtr<CefV8Value> atom = CefV8Value::CreateObject(NULL, NULL);
		global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<CefV8Value> loadPath = CefV8Value::CreateString(
				AppPath() + "/..");
		atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString(
				"window-bootstrap");
		global->SetValue("$bootstrapScript", bootstrapScript,
				V8_PROPERTY_ATTRIBUTE_NONE);

		context->Exit();
	}
}

void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, int httpStatusCode) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
		CefRefPtr<CefDOMVisitor> visitor = GetDOMVisitor(frame->GetURL());
		if (visitor.get())
			frame->VisitDOM(visitor);
	}
}

bool ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, ErrorCode errorCode,
		const CefString& failedUrl, CefString& errorText) {
	REQUIRE_UI_THREAD();

	if (errorCode == ERR_CACHE_MISS) {
		// Usually caused by navigating to a page with POST data via back or
		// forward buttons.
		errorText = "<html><head><title>Expired Form Data</title></head>"
				"<body><h1>Expired Form Data</h1>"
				"<h2>Your form request has expired. "
				"Click reload to re-submit the form data.</h2></body>"
				"</html>";
	} else {
		// All other messages.
		std::stringstream ss;
		ss << "<html><head><title>Load Failed</title></head>"
				"<body><h1>Load Failed</h1>"
				"<h2>Load of URL " << std::string(failedUrl)
				<< " failed with error code " << static_cast<int>(errorCode)
				<< ".</h2></body>"
						"</html>";
		errorText = ss.str();
	}

	return false;
}

void ClientHandler::OnNavStateChange(CefRefPtr<CefBrowser> browser,
		bool canGoBack, bool canGoForward) {
	//Intentionally left blank
}

bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
		const CefString& message, const CefString& source, int line) {
	return false;
}

void ClientHandler::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, CefRefPtr<CefDOMNode> node) {
	REQUIRE_UI_THREAD();

	// Set to true if a form element has focus.
	m_bFormElementHasFocus = (node.get() && node->IsFormControlElement());
}

bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser, KeyEventType type,
		int code, int modifiers, bool isSystemKey, bool isAfterJavaScript) {<commit_msg>Log console message to standard out<commit_after>// Copyright (c) 2011 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 "client_handler.h"
#include <stdio.h>
#include <iostream>
#include <sstream>
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "cefclient.h"
#include "native_handler.h"

ClientHandler::ClientHandler() :
		m_MainHwnd(NULL), m_BrowserHwnd(NULL), m_bFormElementHasFocus(false) {
}

ClientHandler::~ClientHandler() {
}

void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	AutoLock lock_scope(this);
	if (!m_Browser.get()) {
		// We need to keep the main child window, but not popup windows
		m_Browser = browser;
		m_BrowserHwnd = browser->GetWindowHandle();
	}
}

bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle()) {
		// Since the main window contains the browser window, we need to close
		// the parent window instead of the browser window.
		CloseMainWindow();

		// Return true here so that we can skip closing the browser window
		// in this pass. (It will be destroyed due to the call to close
		// the parent above.)
		return true;
	}

	// A popup browser window is not contained in another window, so we can let
	// these windows close by themselves.
	return false;
}

void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle()) {
		// Free the browser pointer so that the browser can be destroyed
		m_Browser = NULL;
	}
}

void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
		CefRefPtr<CefV8Context> context = frame->GetV8Context();
		CefRefPtr<CefV8Value> global = context->GetGlobal();
		context->Enter();

		CefRefPtr<CefV8Value> windowNumber = CefV8Value::CreateInt(0);
		global->SetValue("$windowNumber", windowNumber,
				V8_PROPERTY_ATTRIBUTE_NONE);

		std::string path;
		if (m_nativeHandler)
			path = m_nativeHandler->path;
		else
			path.append(PathToOpen());

		CefRefPtr<CefV8Value> pathToOpen = CefV8Value::CreateString(path);
		global->SetValue("$pathToOpen", pathToOpen, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<NativeHandler> nativeHandler = new NativeHandler();
		nativeHandler->window = window;
		nativeHandler->path = path;
		global->SetValue("$native", nativeHandler->object,
				V8_PROPERTY_ATTRIBUTE_NONE);
		m_nativeHandler = nativeHandler;

		CefRefPtr<CefV8Value> atom = CefV8Value::CreateObject(NULL, NULL);
		global->SetValue("atom", atom, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<CefV8Value> loadPath = CefV8Value::CreateString(
				AppPath() + "/..");
		atom->SetValue("loadPath", loadPath, V8_PROPERTY_ATTRIBUTE_NONE);

		CefRefPtr<CefV8Value> bootstrapScript = CefV8Value::CreateString(
				"window-bootstrap");
		global->SetValue("$bootstrapScript", bootstrapScript,
				V8_PROPERTY_ATTRIBUTE_NONE);

		context->Exit();
	}
}

void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, int httpStatusCode) {
	REQUIRE_UI_THREAD();

	if (m_BrowserHwnd == browser->GetWindowHandle() && frame->IsMain()) {
		CefRefPtr<CefDOMVisitor> visitor = GetDOMVisitor(frame->GetURL());
		if (visitor.get())
			frame->VisitDOM(visitor);
	}
}

bool ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, ErrorCode errorCode,
		const CefString& failedUrl, CefString& errorText) {
	REQUIRE_UI_THREAD();

	if (errorCode == ERR_CACHE_MISS) {
		// Usually caused by navigating to a page with POST data via back or
		// forward buttons.
		errorText = "<html><head><title>Expired Form Data</title></head>"
				"<body><h1>Expired Form Data</h1>"
				"<h2>Your form request has expired. "
				"Click reload to re-submit the form data.</h2></body>"
				"</html>";
	} else {
		// All other messages.
		std::stringstream ss;
		ss << "<html><head><title>Load Failed</title></head>"
				"<body><h1>Load Failed</h1>"
				"<h2>Load of URL " << std::string(failedUrl)
				<< " failed with error code " << static_cast<int>(errorCode)
				<< ".</h2></body>"
						"</html>";
		errorText = ss.str();
	}

	return false;
}

void ClientHandler::OnNavStateChange(CefRefPtr<CefBrowser> browser,
		bool canGoBack, bool canGoForward) {
	//Intentionally left blank
}

bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
		const CefString& message, const CefString& source, int line) {
	REQUIRE_UI_THREAD();
	std::cout << std::string(message) << std::endl;
	return false;
}

void ClientHandler::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefFrame> frame, CefRefPtr<CefDOMNode> node) {
	REQUIRE_UI_THREAD();

	// Set to true if a form element has focus.
	m_bFormElementHasFocus = (node.get() && node->IsFormControlElement());
}

bool ClientHandler::OnKeyEvent(CefRefPtr<CefBrowser> browser, KeyEventType type,
		int code, int modifiers, bool isSystemKey, bool isAfterJavaScript) {
{'fraction_non_alphanumeric': 0.12758721212710136, 'fraction_numerical': 0.006219026333689631, 'mean_word_length': 5.207478890229192, 'pattern_counts': {'":': 0, '<': 132, '<?xml version=': 0, '>': 156, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7122963', 'n_tokens_mistral': 3695, 'n_tokens_neox': 3453, 'n_words': 807}