Skip to content

Commit

Permalink
Add new options: log_file, log_severity, cache_path, ... (#221)
Browse files Browse the repository at this point in the history
remote_debugging_port, external_drag, command_line_switches,
context_menu.

WebRTC and Flash are enabled by default from now on.
  • Loading branch information
cztomczak committed Feb 3, 2019
1 parent 648ac50 commit f30b4a0
Show file tree
Hide file tree
Showing 12 changed files with 276 additions and 17 deletions.
4 changes: 2 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ if [[ ${rc} = 0 ]]; then
fi
cp -r src/www/ build/bin/
./build/bin/phpdesktop
rm -r blob_storage/
rm -r GPUCache/
#rm -r build/bin/blob_storage/
#rm -r build/bin/GPUCache/
else
echo "ERROR";
exit ${rc};
Expand Down
52 changes: 52 additions & 0 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,62 @@
#include "app.h"
#include "include/wrapper/cef_helpers.h"
#include "print_handler_gtk.h"
#include "settings.h"

App::App() {
}

// CefApp

void App::OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) {
// command_line_switches option
if (process_type.empty()) {
// Browser process type is an empty string
json_value* app_settings = get_app_settings();
json_value switches =
(*app_settings)["chrome"]["command_line_switches"];
if (switches.type == json_object) {
int length = switches.u.object.length;
for (int i = 0; i < length; i++) {
std::string name = switches.u.object.values[i].name;
std::string value = static_cast<const char*>(
*switches.u.object.values[i].value);
if (name.find("-") == 0) {
LOG(WARNING) << "Invalid command line switch: " << name;
continue;
}
if (value.empty()) {
LOG(INFO) << "Processing switch: " << name;
} else {
LOG(INFO) << "Processing switch: " << name << "=" << value;
}
if (command_line->HasSwitch(name)) {
if (value.empty()) {
// Switch already set, do nothing
} else {
std::string old_value =
command_line->GetSwitchValue(name);
if (old_value != value) {
// Overwrite the switch with a new value
command_line->AppendSwitchWithValue(name, value);
}
}
} else {
if (value.empty()) {
command_line->AppendSwitch(name);
} else {
command_line->AppendSwitchWithValue(name, value);
}
}
}
}
}
}

// CefBrowserProcessHandler

void App::OnContextInitialized() {
CEF_REQUIRE_UI_THREAD();
print_handler_ = new ClientPrintHandlerGtk();
Expand Down
7 changes: 5 additions & 2 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ class App : public CefApp,
public:
App();

// CefApp methods:
// CefApp
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler()
override {
return this;
}
virtual void OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) override;

// CefBrowserProcessHandler methods:
// CefBrowserProcessHandler
virtual void OnContextInitialized() override;
virtual CefRefPtr<CefPrintHandler> GetPrintHandler() override;

Expand Down
92 changes: 92 additions & 0 deletions src/client_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,76 @@ CefRefPtr<CefBrowser> ClientHandler::FindBrowserByXid(::Window xid) {
return NULL;
}

// CefContextMenuHandler

#define _MENU_ID_DEVTOOLS MENU_ID_USER_FIRST + 1
#define _MENU_ID_RELOAD_PAGE MENU_ID_USER_FIRST + 2

void ClientHandler::OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) {
CEF_REQUIRE_UI_THREAD();

json_value* settings = get_app_settings();
bool enable_menu = (*settings)["chrome"]["context_menu"]["enable_menu"];
bool navigation = (*settings)["chrome"]["context_menu"]["navigation"];
bool print = (*settings)["chrome"]["context_menu"]["print"];
bool view_source = (*settings)["chrome"]["context_menu"]["view_source"];
bool devtools = (*settings)["chrome"]["context_menu"]["devtools"];

if (!enable_menu) {
model->Clear();
return;
}

if (!navigation) {
model->Remove(MENU_ID_BACK);
model->Remove(MENU_ID_FORWARD);
// Remote separator.
model->RemoveAt(0);
}
if (!print) {
model->Remove(MENU_ID_PRINT);
}
if (!view_source) {
model->Remove(MENU_ID_VIEW_SOURCE);
}

if (!params->IsEditable() && params->GetSelectionText().empty()
&& (params->GetPageUrl().length()
|| params->GetFrameUrl().length())) {
if (navigation) {
model->InsertItemAt(2, _MENU_ID_RELOAD_PAGE, "Reload");
}
if (devtools) {
model->AddSeparator();
model->AddItem(_MENU_ID_DEVTOOLS, "Show Developer Tools");
}
}
}

bool ClientHandler::OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags) {
CEF_REQUIRE_UI_THREAD();
if (command_id == _MENU_ID_RELOAD_PAGE) {
browser->ReloadIgnoreCache();
return true;
} else if (command_id == _MENU_ID_DEVTOOLS) {
CefWindowInfo window_info;
CefBrowserSettings settings;
CefPoint inspect;
browser->GetHost()->ShowDevTools(window_info, NULL, settings, inspect);
return true;
}
return false;
}

// CefDownloadHandler

void ClientHandler::OnBeforeDownload(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
Expand All @@ -61,6 +131,21 @@ void ClientHandler::OnBeforeDownload(
}
}

// CefDragHandler

bool ClientHandler::OnDragEnter(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDragData> dragData,
DragOperationsMask mask) {
bool external_drag = (*get_app_settings())["chrome"]["external_drag"];
if (external_drag) {
return false;
} else {
return true;
}
}

// CefLifeSpanHandler

void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();
// Add to the list of existing browsers.
Expand All @@ -77,6 +162,11 @@ void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
CEF_REQUIRE_UI_THREAD();

// Cookies are not flushed to disk when closing app immediately.
// Need to call FlushStore manually when browser is closing.
browser->GetHost()->GetRequestContext()->GetDefaultCookieManager(NULL)
->FlushStore(NULL);

// Remove from the list of existing browsers.
BrowserList::iterator bit = browser_list_.begin();
for (; bit != browser_list_.end(); ++bit) {
Expand All @@ -92,6 +182,8 @@ void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
}
}

// CefRequestHandler

bool ClientHandler::OnQuotaRequest(CefRefPtr<CefBrowser> browser,
const CefString& origin_url,
int64 new_size,
Expand Down
24 changes: 24 additions & 0 deletions src/client_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
#include "dialog_handler_gtk.h"

class ClientHandler : public CefClient,
public CefContextMenuHandler,
public CefDisplayHandler,
public CefDownloadHandler,
public CefDragHandler,
public CefLifeSpanHandler,
public CefLoadHandler,
public CefRequestHandler
Expand All @@ -26,6 +28,9 @@ class ClientHandler : public CefClient,
virtual CefRefPtr<CefBrowser> FindBrowserByXid(::Window xid);

// CefClient methods:
CefRefPtr<CefContextMenuHandler> GetContextMenuHandler() override {
return this;
}
CefRefPtr<CefDialogHandler> GetDialogHandler() override {
return dialog_handler_;
}
Expand All @@ -35,6 +40,9 @@ class ClientHandler : public CefClient,
CefRefPtr<CefDownloadHandler> GetDownloadHandler() override {
return this;
}
CefRefPtr<CefDragHandler> GetDragHandler() override {
return this;
}
CefRefPtr<CefJSDialogHandler> GetJSDialogHandler() override {
return dialog_handler_;
}
Expand All @@ -48,13 +56,29 @@ class ClientHandler : public CefClient,
return this;
}

// CefContextMenuHandler
virtual void OnBeforeContextMenu(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) override;
virtual bool OnContextMenuCommand(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags) override;

// CefDownloadHandler
virtual void OnBeforeDownload(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback) override;

// CefDragHandler
virtual bool OnDragEnter(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDragData> dragData,
DragOperationsMask mask) override;

// CefLifeSpanHandler
virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;
virtual void OnBeforeClose(CefRefPtr<CefBrowser> browser) override;
Expand Down
54 changes: 45 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,51 @@ int main(int argc, char **argv) {
// Start Mongoose server
mongoose_start();

// Specify CEF global settings here.
CefSettings settings;
settings.no_sandbox = true;
CefString( &settings.log_file ) = "debug.log"; // @TODO from settings.json
settings.log_severity = LOGSEVERITY_INFO; // @TODO from settings.json
// @TODO cache_path settings.json option
// Specify CEF global settings here
CefSettings cef_settings;
cef_settings.no_sandbox = true;

// log_file
std::string log_file((*app_settings)["chrome"]["log_file"]);
log_file = get_full_path(log_file);
CefString(&cef_settings.log_file) = log_file;

// log_severity
std::string log_severity((*app_settings)["chrome"]["log_severity"]);
if (log_severity == "verbose") {
cef_settings.log_severity = LOGSEVERITY_VERBOSE;
} else if (log_severity == "info") {
cef_settings.log_severity = LOGSEVERITY_INFO;
} else if (log_severity == "warning") {
cef_settings.log_severity = LOGSEVERITY_WARNING;
} else if (log_severity == "error") {
cef_settings.log_severity = LOGSEVERITY_ERROR;
} else if (log_severity == "disable") {
cef_settings.log_severity = LOGSEVERITY_DISABLE;
} else {
cef_settings.log_severity = LOGSEVERITY_DEFAULT;
}

// Remote debugging port
// @todo from settings.json
// cache_path
std::string cache_path((*app_settings)["chrome"]["cache_path"]);
cache_path = get_full_path(cache_path);
CefString(&cef_settings.cache_path) = cache_path;

// remote_debugging_port
int remote_debugging_port(
(*app_settings)["chrome"]["remote_debugging_port"]);
if (remote_debugging_port == 0) {
remote_debugging_port = random(49152, 65535+1);
int i = 100;
while (((i--) > 0)
&& remote_debugging_port == mongoose_get_port_int()) {
remote_debugging_port = random(49152, 65535+1);
}
}
if (remote_debugging_port > 0) {
LOG(INFO) << "remote_debugging_port = " << remote_debugging_port;
cef_settings.remote_debugging_port = remote_debugging_port;
}

// App implements application-level callbacks for the browser
// process.
Expand All @@ -188,7 +224,7 @@ int main(int argc, char **argv) {
// file only after CEF was initialized. Before CEF is initialized
// all logs are only printed to console.
LOG(INFO) << "Initialize CEF";
CefInitialize(main_args, settings, app.get(), NULL);
CefInitialize(main_args, cef_settings, app.get(), NULL);

// The Chromium sandbox requires that there only be a single thread during
// initialization. Therefore initialize GTK after CEF.
Expand Down
8 changes: 7 additions & 1 deletion src/mongoose_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
#include "version.h"
#include "include/base/cef_logging.h"

std::string g_mongoose_port = "0"; // @TODO from settings.json
std::string g_mongoose_port = "0";
int g_mongoose_port_int = 0;
std::string g_mongoose_ip_address = "127.0.0.1"; // @TODO from settings.json
std::string g_mongoose_url = "";

Expand Down Expand Up @@ -123,6 +124,7 @@ bool mongoose_start() {
// When port was set to 0 then a random free port was assigned
// by OS.
int port = mg_get_listening_port(g_mongoose_context);
g_mongoose_port_int = port;
std::stringstream port_ss;
port_ss << port;
g_mongoose_port = port_ss.str();
Expand All @@ -149,6 +151,10 @@ std::string mongoose_get_port() {
return g_mongoose_port;
}

int mongoose_get_port_int() {
return g_mongoose_port_int;
}

std::string mongoose_get_ip_address() {
return g_mongoose_ip_address;
}
Expand Down
1 change: 1 addition & 0 deletions src/mongoose_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
bool mongoose_start();
void mongoose_stop();
std::string mongoose_get_port();
int mongoose_get_port_int();
std::string mongoose_get_ip_address();
std::string mongoose_get_url();
6 changes: 4 additions & 2 deletions src/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
"cache_path": "webcache",
"external_drag": true,
"remote_debugging_port": 0,
"command_line_switches": {},
"command_line_switches": {
"enable-media-stream": "",
"enable-system-flash": ""
},
"enable_downloads": true,
"context_menu": {
"enable_menu": true,
"navigation": true,
"print": true,
"view_source": true,
"open_in_external_browser": true,
"devtools": true
}
}
Expand Down
Loading

0 comments on commit f30b4a0

Please sign in to comment.