Skip to content

Commit f27c73c

Browse files
committed
inject-js in Window.open options
1 parent 08094e4 commit f27c73c

File tree

7 files changed

+48
-3
lines changed

7 files changed

+48
-3
lines changed

src/api/dispatcher_host.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@ void DispatcherHost::OnCreateShell(const std::string& url,
252252
WebContents::CreateParams create_params(browser_context,
253253
new_renderer ? NULL : base_web_contents->GetSiteInstance());
254254

255+
std::string filename;
256+
if (new_manifest->GetString(switches::kmInjectJS, &filename))
257+
create_params.nw_inject_js_fn = filename;
258+
if (new_manifest->GetString(switches::kmInjectCSS, &filename))
259+
create_params.nw_inject_css_fn = filename;
260+
255261
WebContents* web_contents = content::WebContentsImpl::CreateWithOpener(
256262
create_params,
257263
static_cast<content::WebContentsImpl*>(base_web_contents));

src/common/shell_switches.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const char kmUserAgent[] = "user-agent";
9898
const char kmRemotePages[] = "node-remote";
9999

100100
const char kmNewInstance[] = "new-instance";
101+
const char kmInjectJS[] = "inject-js";
102+
const char kmInjectCSS[] = "inject-css";
101103

102104
#if defined(OS_WIN)
103105
// Enable conversion from vector to raster for any page.

src/common/shell_switches.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ extern const char kmPageCache[];
5656
extern const char kmUserAgent[];
5757
extern const char kmRemotePages[];
5858
extern const char kmNewInstance[];
59+
extern const char kmInjectJS[];
60+
extern const char kmInjectCSS[];
5961

6062
#if defined(OS_WIN)
6163
extern const char kPrintRaster[];

src/renderer/nw_render_view_observer.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,27 @@
2020

2121
#include "content/nw/src/renderer/nw_render_view_observer.h"
2222

23+
#include <v8.h>
24+
25+
#include "base/file_util.h"
26+
#include "base/strings/utf_string_conversions.h"
2327
#include "content/nw/src/renderer/common/render_messages.h"
2428
#include "content/public/renderer/render_view.h"
29+
#include "content/renderer/render_view_impl.h"
2530
#include "skia/ext/platform_canvas.h"
2631
#include "third_party/WebKit/public/platform/WebRect.h"
2732
#include "third_party/WebKit/public/platform/WebSize.h"
2833
#include "third_party/WebKit/public/web/WebFrame.h"
34+
#include "third_party/WebKit/public/web/WebScriptSource.h"
2935
#include "third_party/WebKit/public/web/WebView.h"
3036
#include "webkit/glue/webkit_glue.h"
3137

38+
using content::RenderView;
39+
using content::RenderViewImpl;
40+
3241
using WebKit::WebFrame;
3342
using WebKit::WebRect;
43+
using WebKit::WebScriptSource;
3444
using WebKit::WebSize;
3545

3646
namespace nw {
@@ -70,6 +80,25 @@ void NwRenderViewObserver::OnCaptureSnapshot() {
7080
Send(new NwViewHostMsg_Snapshot(routing_id(), snapshot));
7181
}
7282

83+
void NwRenderViewObserver::DidFinishDocumentLoad(WebKit::WebFrame* frame) {
84+
RenderViewImpl* rv = RenderViewImpl::FromWebView(frame->view());
85+
if (!rv)
86+
return;
87+
std::string js_fn = rv->renderer_preferences_.nw_inject_js_fn;
88+
if (js_fn.empty())
89+
return;
90+
std::string content;
91+
base::FilePath path = rv->renderer_preferences_.nw_app_root_path.Append(js_fn);
92+
if (!base::ReadFileToString(path, &content)) {
93+
LOG(WARNING) << "Failed to load js script file: " << path.value();
94+
return;
95+
}
96+
base::string16 jscript = base::UTF8ToUTF16(content);
97+
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
98+
// v8::Handle<v8::Value> result;
99+
frame->executeScriptAndReturnValue(WebScriptSource(jscript));
100+
}
101+
73102
bool NwRenderViewObserver::CaptureSnapshot(WebKit::WebView* view,
74103
SkBitmap* snapshot) {
75104
view->layout();

src/renderer/nw_render_view_observer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,11 @@ class NwRenderViewObserver : public content::RenderViewObserver {
3636
NwRenderViewObserver(content::RenderView* render_view);
3737
virtual ~NwRenderViewObserver();
3838

39-
private:
4039
// RenderViewObserver implementation.
4140
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
41+
virtual void DidFinishDocumentLoad(WebKit::WebFrame* frame) OVERRIDE;
42+
43+
private:
4244

4345
void OnCaptureSnapshot();
4446

src/shell_content_browser_client.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
114114

115115
WebContentsViewPort* ShellContentBrowserClient::OverrideCreateWebContentsView(
116116
WebContents* web_contents,
117-
RenderViewHostDelegateView** render_view_host_delegate_view) {
117+
RenderViewHostDelegateView** render_view_host_delegate_view,
118+
const WebContents::CreateParams& params) {
118119
std::string user_agent, rules;
119120
nw::Package* package = shell_browser_main_parts()->package();
120121
content::RendererPreferences* prefs = web_contents->GetMutableRendererPrefs();
@@ -133,6 +134,8 @@ WebContentsViewPort* ShellContentBrowserClient::OverrideCreateWebContentsView(
133134
prefs->nw_remote_page_rules = rules;
134135

135136
prefs->nw_app_root_path = package->path();
137+
prefs->nw_inject_css_fn = params.nw_inject_css_fn;
138+
prefs->nw_inject_js_fn = params.nw_inject_js_fn;
136139
return NULL;
137140
}
138141

src/shell_content_browser_client.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class ShellContentBrowserClient : public ContentBrowserClient {
3434
const MainFunctionParams& parameters) OVERRIDE;
3535
virtual WebContentsViewPort* OverrideCreateWebContentsView(
3636
WebContents* web_contents,
37-
RenderViewHostDelegateView** render_view_host_delegate_view) OVERRIDE;
37+
RenderViewHostDelegateView** render_view_host_delegate_view,
38+
const WebContents::CreateParams& params) OVERRIDE;
3839
virtual std::string GetApplicationLocale() OVERRIDE;
3940
virtual void AppendExtraCommandLineSwitches(CommandLine* command_line,
4041
int child_process_id) OVERRIDE;

0 commit comments

Comments
 (0)