- Add CefBrowser::ExecuteJavaScript() method.
tests/cefclient:
- Add test for ExecuteJavaScript method.

Issue 1, Suggested implementation by: vridosh


git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@12 5089003a-bbd8-11dd-ad1f-f1f9622dbc98
This commit is contained in:
Marshall Greenblatt 2009-01-29 17:48:16 +00:00
parent 729f9279bc
commit 9d3cd36b4b
6 changed files with 58 additions and 7 deletions

View File

@ -220,7 +220,16 @@ public:
virtual void LoadStream(CefRefPtr<CefStreamReader> stream, virtual void LoadStream(CefRefPtr<CefStreamReader> stream,
const std::wstring& url) =0; const std::wstring& url) =0;
// Register a new handler tied to the specified JS object |name|. Returns // Execute a string of JavaScript code in the specified target frame. The
// |script_url| parameter is the URL where the script in question can be
// found, if any. The renderer may request this URL to show the developer the
// source of the error. The |start_line| parameter is the base line number
// to use for error reporting.
virtual void ExecuteJavaScript(const std::wstring& js_code,
const std::wstring& script_url,
int start_line, TargetFrame targetFrame) =0;
// Register a new handler tied to the specified JS object |name|. Returns
// true if the handler is registered successfully. // true if the handler is registered successfully.
// A JS handler will be accessible to JavaScript as window.<classname>. // A JS handler will be accessible to JavaScript as window.<classname>.
virtual bool AddJSHandler(const std::wstring& classname, virtual bool AddJSHandler(const std::wstring& classname,

View File

@ -154,6 +154,16 @@ void CefBrowserImpl::LoadStream(CefRefPtr<CefStreamReader> stream,
&CefBrowserImpl::UIT_LoadHTMLForStreamRef, stream.get(), url)); &CefBrowserImpl::UIT_LoadHTMLForStreamRef, stream.get(), url));
} }
void CefBrowserImpl::ExecuteJavaScript(const std::wstring& js_code,
const std::wstring& script_url,
int start_line,
TargetFrame targetFrame)
{
PostTask(FROM_HERE, NewRunnableMethod(this,
&CefBrowserImpl::UIT_ExecuteJavaScript, js_code, script_url, start_line,
targetFrame));
}
bool CefBrowserImpl::AddJSHandler(const std::wstring& classname, bool CefBrowserImpl::AddJSHandler(const std::wstring& classname,
CefRefPtr<CefJSHandler> handler) CefRefPtr<CefJSHandler> handler)
{ {
@ -310,6 +320,22 @@ void CefBrowserImpl::UIT_LoadURLForRequestRef(CefRequest* request)
request->Release(); request->Release();
} }
void CefBrowserImpl::UIT_ExecuteJavaScript(const std::wstring& js_code,
const std::wstring& script_url,
int start_line,
TargetFrame targetFrame)
{
REQUIRE_UIT();
WebFrame* frame;
if(targetFrame == TF_FOCUSED)
frame = UIT_GetWebView()->GetFocusedFrame();
else
frame = UIT_GetWebView()->GetMainFrame();
frame->ExecuteJavaScript(WideToUTF8(js_code), GURL(script_url), start_line);
}
void CefBrowserImpl::UIT_GoBackOrForward(int offset) void CefBrowserImpl::UIT_GoBackOrForward(int offset)
{ {
REQUIRE_UIT(); REQUIRE_UIT();

View File

@ -58,6 +58,9 @@ public:
const std::wstring& url); const std::wstring& url);
virtual void LoadStream(CefRefPtr<CefStreamReader> stream, virtual void LoadStream(CefRefPtr<CefStreamReader> stream,
const std::wstring& url); const std::wstring& url);
virtual void ExecuteJavaScript(const std::wstring& js_code,
const std::wstring& script_url,
int start_line, TargetFrame targetFrame);
virtual bool AddJSHandler(const std::wstring& classname, virtual bool AddJSHandler(const std::wstring& classname,
CefRefPtr<CefJSHandler> handler); CefRefPtr<CefJSHandler> handler);
virtual bool HasJSHandler(const std::wstring& classname); virtual bool HasJSHandler(const std::wstring& classname);
@ -137,13 +140,16 @@ public:
void UIT_LoadURLForRequest(const std::wstring& url, void UIT_LoadURLForRequest(const std::wstring& url,
const std::wstring& frame_name, const std::wstring& frame_name,
const std::wstring& method, const std::wstring& method,
net::UploadData *upload_data, net::UploadData *upload_data,
const WebRequest::HeaderMap& headers); const WebRequest::HeaderMap& headers);
void UIT_LoadURLForRequestRef(CefRequest* request); void UIT_LoadURLForRequestRef(CefRequest* request);
void UIT_LoadHTML(const std::wstring& html, void UIT_LoadHTML(const std::wstring& html,
const std::wstring& url); const std::wstring& url);
void UIT_LoadHTMLForStreamRef(CefStreamReader* stream, void UIT_LoadHTMLForStreamRef(CefStreamReader* stream,
const std::wstring& url); const std::wstring& url);
void UIT_ExecuteJavaScript(const std::wstring& js_code,
const std::wstring& script_url,
int start_line, TargetFrame targetFrame);
void UIT_GoBackOrForward(int offset); void UIT_GoBackOrForward(int offset);
void UIT_Reload(); void UIT_Reload();
bool UIT_Navigate(const BrowserNavigationEntry& entry, bool reload); bool UIT_Navigate(const BrowserNavigationEntry& entry, bool reload);

View File

@ -788,7 +788,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
if(browser.get()) if(browser.get())
browser->StopLoad(); browser->StopLoad();
return 0; return 0;
case ID_TESTS_JAVASCRIPT: // Test our javascript handler case ID_TESTS_JAVASCRIPT_HANDLER: // Test our javascript handler
if(browser.get()) if(browser.get())
{ {
std::wstring html = std::wstring html =
@ -801,6 +801,13 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
browser->LoadString(html, L"about:blank"); browser->LoadString(html, L"about:blank");
} }
return 0; return 0;
case ID_TESTS_JAVASCRIPT_EXECUTE: // Test execution of javascript
if(browser.get())
{
browser->ExecuteJavaScript(L"alert('JavaScript execute works!');",
L"about:blank", 0, CefBrowser::TF_MAIN);
}
return 0;
case ID_TESTS_PLUGIN: // Test our custom plugin case ID_TESTS_PLUGIN: // Test our custom plugin
if(browser.get()) if(browser.get())
{ {
@ -811,6 +818,7 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
L"</body></html>"; L"</body></html>";
browser->LoadString(html, L"about:blank"); browser->LoadString(html, L"about:blank");
} }
return 0;
} }
} }
break; break;

View File

@ -57,7 +57,8 @@ BEGIN
END END
POPUP "Tests" POPUP "Tests"
BEGIN BEGIN
MENUITEM "JavaScript", ID_TESTS_JAVASCRIPT MENUITEM "JavaScript Handler", ID_TESTS_JAVASCRIPT_HANDLER
MENUITEM "JavaScript Execute", ID_TESTS_JAVASCRIPT_EXECUTE
MENUITEM "Plugin", ID_TESTS_PLUGIN MENUITEM "Plugin", ID_TESTS_PLUGIN
END END
END END

View File

@ -21,8 +21,9 @@
#define IDC_NAV_FORWARD 201 #define IDC_NAV_FORWARD 201
#define IDC_NAV_RELOAD 202 #define IDC_NAV_RELOAD 202
#define IDC_NAV_STOP 203 #define IDC_NAV_STOP 203
#define ID_TESTS_JAVASCRIPT 32771 #define ID_TESTS_JAVASCRIPT_HANDLER 32771
#define ID_TESTS_PLUGIN 32772 #define ID_TESTS_JAVASCRIPT_EXECUTE 32772
#define ID_TESTS_PLUGIN 32773
#define IDC_STATIC -1 #define IDC_STATIC -1
#define IDS_LOGO 1000 #define IDS_LOGO 1000