Add attribute to turn on node integration in <webview>

This commit is contained in:
Cheng Zhao 2014-10-25 20:52:42 +08:00
parent d596a7427e
commit 22e3b9df44
11 changed files with 154 additions and 13 deletions

View file

@ -181,6 +181,8 @@
'atom/browser/ui/x/x_window_utils.h',
'atom/browser/web_view/web_view_manager.cc',
'atom/browser/web_view/web_view_manager.h',
'atom/browser/web_view/web_view_renderer_state.cc',
'atom/browser/web_view/web_view_renderer_state.h',
'atom/browser/window_list.cc',
'atom/browser/window_list.h',
'atom/browser/window_list_observer.h',

View file

@ -9,13 +9,14 @@
#include "atom/browser/atom_browser_main_parts.h"
#include "atom/browser/atom_speech_recognition_manager_delegate.h"
#include "atom/browser/native_window.h"
#include "atom/browser/web_view/web_view_renderer_state.h"
#include "atom/browser/window_list.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "chrome/browser/printing/printing_message_filter.h"
#include "chrome/browser/speech/tts_message_filter.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_dispatcher_host.h"
#include "content/public/browser/site_instance.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/web_preferences.h"
@ -140,11 +141,17 @@ void AtomBrowserClient::AppendExtraCommandLineSwitches(
window = *iter;
}
if (window != NULL)
if (window != NULL) {
window->AppendExtraCommandLineSwitches(command_line, child_process_id);
else
// If we can not find a owner window then it is a guest web view.
command_line->AppendSwitch("guest");
} else {
// Append commnad line arguments for guest web view.
WebViewRendererState::WebViewInfo info;
if (WebViewRendererState::GetInstance()->GetInfo(child_process_id, &info)) {
command_line->AppendSwitch("guest");
command_line->AppendSwitchASCII(switches::kNodeIntegration,
info.node_integration ? "true" : "false");
}
}
dying_render_process_ = NULL;
}

View file

@ -16,7 +16,6 @@ app.on('ready', function() {
width: 800,
height: 600,
resizable: false,
'node-integration': true,
'auto-hide-menu-bar': true,
'use-content-size': true,
'web-preferences': {

View file

@ -56,8 +56,6 @@
</style>
</head>
<body>
<webview id="foo" autosize src="http://www.baidu.com/" style="display:block; width:640px; height:100px"></webview>
<script>
var execPath = require('remote').process.execPath;
var command = execPath + ' path-to-your-app';

View file

@ -33,7 +33,7 @@ createGuest = (embedder, params) ->
guestInstanceId: id
storagePartitionId: params.storagePartitionId
guestInstances[id] = guest
webViewManager.addGuest id, embedder, guest
webViewManager.addGuest id, embedder, guest, params.nodeIntegration
# Destroy guest when the embedder is gone.
embedder.once 'render-view-deleted', ->

View file

@ -6,8 +6,11 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/web_view/web_view_renderer_state.h"
#include "base/bind.h"
#include "base/stl_util.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
@ -39,11 +42,29 @@ WebViewManager::~WebViewManager() {
void WebViewManager::AddGuest(int guest_instance_id,
content::WebContents* embedder,
content::WebContents* web_contents) {
web_contents_map_[guest_instance_id] = {web_contents, embedder};
content::WebContents* web_contents,
bool node_integration) {
web_contents_map_[guest_instance_id] = { web_contents, embedder };
WebViewRendererState::WebViewInfo web_view_info = { node_integration };
content::BrowserThread::PostTask(
content::BrowserThread::IO,
FROM_HERE,
base::Bind(&WebViewRendererState::AddGuest,
base::Unretained(WebViewRendererState::GetInstance()),
web_contents->GetRenderProcessHost()->GetID(),
web_view_info));
}
void WebViewManager::RemoveGuest(int guest_instance_id) {
auto web_contents = web_contents_map_[guest_instance_id].web_contents;
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(
&WebViewRendererState::RemoveGuest,
base::Unretained(WebViewRendererState::GetInstance()),
web_contents->GetRenderProcessHost()->GetID()));
web_contents_map_.erase(guest_instance_id);
}

View file

@ -22,7 +22,8 @@ class WebViewManager : public content::BrowserPluginGuestManager {
void AddGuest(int guest_instance_id,
content::WebContents* embedder,
content::WebContents* web_contents);
content::WebContents* web_contents,
bool node_integration);
void RemoveGuest(int guest_instance_id);
protected:

View file

@ -0,0 +1,46 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "atom/browser/web_view/web_view_renderer_state.h"
#include "content/public/browser/browser_thread.h"
namespace atom {
// static
WebViewRendererState* WebViewRendererState::GetInstance() {
return Singleton<WebViewRendererState>::get();
}
WebViewRendererState::WebViewRendererState() {
}
WebViewRendererState::~WebViewRendererState() {
}
bool WebViewRendererState::IsGuest(int render_process_id) {
return webview_info_map_.find(render_process_id) !=
webview_info_map_.end();
}
void WebViewRendererState::AddGuest(int guest_process_id,
const WebViewInfo& webview_info) {
webview_info_map_[guest_process_id] = webview_info;
}
void WebViewRendererState::RemoveGuest(int guest_process_id) {
webview_info_map_.erase(guest_process_id);
}
bool WebViewRendererState::GetInfo(int guest_process_id,
WebViewInfo* webview_info) {
WebViewInfoMap::iterator iter = webview_info_map_.find(guest_process_id);
if (iter != webview_info_map_.end()) {
*webview_info = iter->second;
return true;
}
return false;
}
} // namespace atom

View file

@ -0,0 +1,55 @@
// Copyright (c) 2014 GitHub, Inc. All rights reserved.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_BROWSER_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_
#define ATOM_BROWSER_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_
#include <map>
#include <string>
#include <utility>
#include "base/memory/singleton.h"
namespace atom {
class WebViewManager;
// This class keeps track of <webview> renderer state for use on the IO thread.
// All methods should be called on the IO thread.
class WebViewRendererState {
public:
struct WebViewInfo {
bool node_integration;
};
static WebViewRendererState* GetInstance();
// Looks up the information for the embedder <webview> for a given render
// view, if one exists. Called on the IO thread.
bool GetInfo(int guest_process_id, WebViewInfo* webview_info);
// Returns true if the given renderer is used by webviews.
bool IsGuest(int render_process_id);
private:
friend class WebViewManager;
friend struct DefaultSingletonTraits<WebViewRendererState>;
typedef std::map<int, WebViewInfo> WebViewInfoMap;
WebViewRendererState();
~WebViewRendererState();
// Adds or removes a <webview> guest render process from the set.
void AddGuest(int render_process_id, const WebViewInfo& webview_info);
void RemoveGuest(int render_process_id);
WebViewInfoMap webview_info_map_;
DISALLOW_COPY_AND_ASSIGN(WebViewRendererState);
};
} // namespace atom
#endif // ATOM_BROWSER_WEB_VIEW_WEB_VIEW_RENDERER_STATE_H_

View file

@ -19,6 +19,7 @@ WEB_VIEW_ATTRIBUTE_MAXWIDTH = 'maxwidth'
WEB_VIEW_ATTRIBUTE_MINHEIGHT = 'minheight'
WEB_VIEW_ATTRIBUTE_MINWIDTH = 'minwidth'
WEB_VIEW_ATTRIBUTE_PARTITION = 'partition'
WEB_VIEW_ATTRIBUTE_NODEINTEGRATION = 'nodeintegration'
AUTO_SIZE_ATTRIBUTES = [
WEB_VIEW_ATTRIBUTE_AUTOSIZE,
WEB_VIEW_ATTRIBUTE_MAXHEIGHT,
@ -373,7 +374,9 @@ class WebView
storagePartitionId =
@webviewNode.getAttribute(WEB_VIEW_ATTRIBUTE_PARTITION) or
@webviewNode[WEB_VIEW_ATTRIBUTE_PARTITION]
params = storagePartitionId: storagePartitionId
params =
storagePartitionId: storagePartitionId
nodeIntegration: @webviewNode.hasAttribute WEB_VIEW_ATTRIBUTE_NODEINTEGRATION
guestViewInternal.createGuest 'webview', params, (guestInstanceId) =>
@pendingGuestCreation = false
unless @elementAttached

View file

@ -73,6 +73,15 @@ bounds specified by the attributes `minwidth`, `minheight`, `maxwidth`, and
enabled. When `autosize` is enabled, the `webview` container size cannot be less
than the minimum values or greater than the maximum.
### nodeintegration
```html
<webview src="http://www.google.com/" nodeintegration></webview>
```
If "on", the guest page in `webview` will have node integration and can use node
APIs like `require` and `process` to access low level system resources.
## Methods
### `<webview>`.getUrl()