mirror of
https://bitbucket.org/chromiumembedded/cef
synced 2025-06-05 21:39:12 +02:00
- Add example to cefclient of launching DevTools in an external browser window and process using new CefGetPath and CefLaunchProcess functions (issue #639).
git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@713 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
@@ -384,9 +384,8 @@ int main(int argc, char* argv[]) {
|
||||
window_info.SetAsChild(vbox);
|
||||
|
||||
CefBrowserHost::CreateBrowserSync(
|
||||
window_info,
|
||||
static_cast<CefRefPtr<CefClient> >(g_handler),
|
||||
"http://www.google.com", browserSettings);
|
||||
window_info, g_handler.get(),
|
||||
g_handler->GetStartupURL(), browserSettings);
|
||||
|
||||
gtk_container_add(GTK_CONTAINER(window), vbox);
|
||||
gtk_widget_show_all(GTK_WIDGET(window));
|
||||
|
@@ -360,7 +360,7 @@ NSButton* MakeButton(NSRect* rect, NSString* title, NSView* parent) {
|
||||
|
||||
window_info.SetAsChild(contentView, 0, 0, kWindowWidth, kWindowHeight);
|
||||
CefBrowserHost::CreateBrowser(window_info, g_handler.get(),
|
||||
"http://www.google.com", settings);
|
||||
g_handler->GetStartupURL(), settings);
|
||||
|
||||
// Show the window.
|
||||
[mainWnd makeKeyAndOrderFront: nil];
|
||||
|
@@ -300,9 +300,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam,
|
||||
info.SetAsChild(hWnd, rect);
|
||||
|
||||
// Creat the new child browser window
|
||||
CefBrowserHost::CreateBrowser(info,
|
||||
static_cast<CefRefPtr<CefClient> >(g_handler),
|
||||
"http://www.google.com", settings);
|
||||
CefBrowserHost::CreateBrowser(info, g_handler.get(),
|
||||
g_handler->GetStartupURL(), settings);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@@ -9,10 +9,14 @@
|
||||
#include <string>
|
||||
#include "include/cef_browser.h"
|
||||
#include "include/cef_frame.h"
|
||||
#include "include/cef_path_util.h"
|
||||
#include "include/cef_process_util.h"
|
||||
#include "include/cef_runnable.h"
|
||||
#include "include/wrapper/cef_stream_resource_handler.h"
|
||||
#include "cefclient/binding_test.h"
|
||||
#include "cefclient/cefclient.h"
|
||||
#include "cefclient/client_renderer.h"
|
||||
#include "cefclient/client_switches.h"
|
||||
#include "cefclient/dom_test.h"
|
||||
#include "cefclient/resource_util.h"
|
||||
#include "cefclient/string_util.h"
|
||||
@@ -39,6 +43,17 @@ ClientHandler::ClientHandler()
|
||||
m_bFocusOnEditableField(false) {
|
||||
CreateProcessMessageDelegates(process_message_delegates_);
|
||||
CreateRequestDelegates(request_delegates_);
|
||||
|
||||
// Read command line settings.
|
||||
CefRefPtr<CefCommandLine> command_line =
|
||||
CefCommandLine::GetGlobalCommandLine();
|
||||
|
||||
if (command_line->HasSwitch(cefclient::kUrl))
|
||||
m_StartupURL = command_line->GetSwitchValue(cefclient::kUrl);
|
||||
if (m_StartupURL.empty())
|
||||
m_StartupURL = "http://www.google.com/";
|
||||
|
||||
m_bExternalDevTools = command_line->HasSwitch(cefclient::kExternalDevTools);
|
||||
}
|
||||
|
||||
ClientHandler::~ClientHandler() {
|
||||
@@ -293,14 +308,14 @@ void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
|
||||
|
||||
void ClientHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
|
||||
TerminationStatus status) {
|
||||
// Load google.com if that's not the website that we terminated on.
|
||||
// Load the startup URL if that's not the website that we terminated on.
|
||||
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
|
||||
std::string url = frame->GetURL();
|
||||
std::transform(url.begin(), url.end(), url.begin(), tolower);
|
||||
|
||||
const char kLoadURL[] = "http://www.google.com";
|
||||
if (url.find(kLoadURL) != 0)
|
||||
frame->LoadURL(kLoadURL);
|
||||
std::string startupURL = GetStartupURL();
|
||||
if (url.find(startupURL) != 0)
|
||||
frame->LoadURL(startupURL);
|
||||
}
|
||||
|
||||
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
|
||||
@@ -402,11 +417,40 @@ std::string ClientHandler::GetLastDownloadFile() {
|
||||
|
||||
void ClientHandler::ShowDevTools(CefRefPtr<CefBrowser> browser) {
|
||||
std::string devtools_url = browser->GetHost()->GetDevToolsURL(true);
|
||||
if (!devtools_url.empty() &&
|
||||
m_OpenDevToolsURLs.find(devtools_url) == m_OpenDevToolsURLs.end()) {
|
||||
m_OpenDevToolsURLs.insert(devtools_url);
|
||||
browser->GetMainFrame()->ExecuteJavaScript(
|
||||
"window.open('" + devtools_url + "');", "about:blank", 0);
|
||||
if (!devtools_url.empty()) {
|
||||
if (m_bExternalDevTools) {
|
||||
// Open DevTools in an external browser window.
|
||||
LaunchExternalBrowser(devtools_url);
|
||||
} else if (m_OpenDevToolsURLs.find(devtools_url) ==
|
||||
m_OpenDevToolsURLs.end()) {
|
||||
// Open DevTools in a popup window.
|
||||
m_OpenDevToolsURLs.insert(devtools_url);
|
||||
browser->GetMainFrame()->ExecuteJavaScript(
|
||||
"window.open('" + devtools_url + "');", "about:blank", 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
void ClientHandler::LaunchExternalBrowser(const std::string& url) {
|
||||
if (CefCurrentlyOn(TID_PROCESS_LAUNCHER)) {
|
||||
// Retrieve the current executable path.
|
||||
CefString file_exe;
|
||||
if (!CefGetPath(PK_FILE_EXE, file_exe))
|
||||
return;
|
||||
|
||||
// Create the command line.
|
||||
CefRefPtr<CefCommandLine> command_line =
|
||||
CefCommandLine::CreateCommandLine();
|
||||
command_line->SetProgram(file_exe);
|
||||
command_line->AppendSwitchWithValue(cefclient::kUrl, url);
|
||||
|
||||
// Launch the process.
|
||||
CefLaunchProcess(command_line);
|
||||
} else {
|
||||
// Execute on the PROCESS_LAUNCHER thread.
|
||||
CefPostTask(TID_PROCESS_LAUNCHER,
|
||||
NewCefRunnableFunction(&ClientHandler::LaunchExternalBrowser, url));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -189,6 +189,12 @@ class ClientHandler : public CefClient,
|
||||
|
||||
void ShowDevTools(CefRefPtr<CefBrowser> browser);
|
||||
|
||||
// Returns the startup URL.
|
||||
std::string GetStartupURL() { return m_StartupURL; }
|
||||
|
||||
// Create an external browser window that loads the specified URL.
|
||||
static void LaunchExternalBrowser(const std::string& url);
|
||||
|
||||
protected:
|
||||
void SetLoading(bool isLoading);
|
||||
void SetNavState(bool canGoBack, bool canGoForward);
|
||||
@@ -240,8 +246,15 @@ class ClientHandler : public CefClient,
|
||||
ProcessMessageDelegateSet process_message_delegates_;
|
||||
RequestDelegateSet request_delegates_;
|
||||
|
||||
// If true DevTools will be opened in an external browser window.
|
||||
bool m_bExternalDevTools;
|
||||
|
||||
// List of open DevTools URLs if not using an external browser window.
|
||||
std::set<std::string> m_OpenDevToolsURLs;
|
||||
|
||||
// The startup URL.
|
||||
std::string m_StartupURL;
|
||||
|
||||
// Include the default reference counting implementation.
|
||||
IMPLEMENT_REFCOUNTING(ClientHandler);
|
||||
// Include the default locking implementation.
|
||||
|
@@ -8,6 +8,9 @@
|
||||
|
||||
namespace cefclient {
|
||||
|
||||
const char kUrl[] = "url";
|
||||
const char kExternalDevTools[] = "external-devtools";
|
||||
|
||||
// CefSettings attributes.
|
||||
const char kMultiThreadedMessageLoop[] = "multi-threaded-message-loop";
|
||||
const char kCachePath[] = "cache-path";
|
||||
|
@@ -10,6 +10,9 @@
|
||||
|
||||
namespace cefclient {
|
||||
|
||||
extern const char kUrl[];
|
||||
extern const char kExternalDevTools[];
|
||||
|
||||
// CefSettings attributes.
|
||||
extern const char kMultiThreadedMessageLoop[];
|
||||
extern const char kCachePath[];
|
||||
|
Reference in New Issue
Block a user