Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: remove awkward semi-documented preloadURL WebPreference #33228

Merged
merged 1 commit into from Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions docs/api/web-contents.md
Expand Up @@ -820,9 +820,6 @@ This event can be used to configure `webPreferences` for the `webContents`
of a `<webview>` before it's loaded, and provides the ability to set settings
that can't be set via `<webview>` attributes.

**Note:** The specified `preload` script option will appear as `preloadURL`
(not `preload`) in the `webPreferences` object emitted with this event.

#### Event: 'did-attach-webview'

Returns:
Expand Down
3 changes: 0 additions & 3 deletions docs/api/webview-tag.md
Expand Up @@ -158,9 +158,6 @@ When the guest page doesn't have node integration this script will still have
access to all Node APIs, but global objects injected by Node will be deleted
after this script has finished executing.

**Note:** This option will appear as `preloadURL` (not `preload`) in
the `webPreferences` specified to the `will-attach-webview` event.

### `httpreferrer`

```html
Expand Down
1 change: 0 additions & 1 deletion docs/tutorial/security.md
Expand Up @@ -562,7 +562,6 @@ app.on('web-contents-created', (event, contents) => {
contents.on('will-attach-webview', (event, webPreferences, params) => {
// Strip away preload scripts if unused or verify their location is legitimate
delete webPreferences.preload
delete webPreferences.preloadURL

// Disable Node.js integration
webPreferences.nodeIntegration = false
Expand Down
3 changes: 2 additions & 1 deletion lib/browser/guest-view-manager.ts
Expand Up @@ -15,6 +15,7 @@ interface GuestInstance {

const webViewManager = process._linkedBinding('electron_browser_web_view_manager');
const eventBinding = process._linkedBinding('electron_browser_event');
const netBinding = process._linkedBinding('electron_browser_net');

const supportedWebViewEvents = Object.keys(webViewEvents);

Expand Down Expand Up @@ -49,7 +50,7 @@ function makeWebPreferences (embedder: Electron.WebContents, params: Record<stri
};

if (params.preload) {
webPreferences.preloadURL = params.preload;
webPreferences.preload = netBinding.fileURLToFilePath(params.preload);
}

// Security options that guest will always inherit from embedder
Expand Down
13 changes: 13 additions & 0 deletions shell/browser/api/electron_api_net.cc
Expand Up @@ -5,11 +5,15 @@
#include <string>

#include "gin/handle.h"
#include "net/base/filename_util.h"
#include "net/base/network_change_notifier.h"
#include "net/http/http_util.h"
#include "services/network/public/cpp/features.h"
#include "shell/browser/api/electron_api_url_loader.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/object_template_builder.h"

#include "shell/common/node_includes.h"
Expand All @@ -28,6 +32,14 @@ bool IsValidHeaderValue(std::string header_value) {
return net::HttpUtil::IsValidHeaderValue(header_value);
}

base::FilePath FileURLToFilePath(v8::Isolate* isolate, const GURL& url) {
base::FilePath path;
if (!net::FileURLToFilePath(url, &path))
gin_helper::ErrorThrower(isolate).ThrowError(
"Failed to convert URL to file path");
return path;
}

using electron::api::SimpleURLLoaderWrapper;

void Initialize(v8::Local<v8::Object> exports,
Expand All @@ -41,6 +53,7 @@ void Initialize(v8::Local<v8::Object> exports,
dict.SetMethod("isValidHeaderName", &IsValidHeaderName);
dict.SetMethod("isValidHeaderValue", &IsValidHeaderValue);
dict.SetMethod("createURLLoader", &SimpleURLLoaderWrapper::Create);
dict.SetMethod("fileURLToFilePath", &FileURLToFilePath);
}

} // namespace
Expand Down
9 changes: 0 additions & 9 deletions shell/browser/web_contents_preferences.cc
Expand Up @@ -258,15 +258,6 @@ void WebContentsPreferences::Merge(
} else {
LOG(ERROR) << "preload script must have absolute path.";
}
} else if (web_preferences.Get(options::kPreloadURL, &preload_url_str)) {
// Translate to file path if there is "preload-url" option.
base::FilePath preload;
GURL preload_url(preload_url_str);
if (net::FileURLToFilePath(preload_url, &preload)) {
preload_path_ = preload;
} else {
LOG(ERROR) << "preload url must be file:// protocol.";
}
}

std::string type;
Expand Down
3 changes: 0 additions & 3 deletions shell/common/options_switches.cc
Expand Up @@ -120,9 +120,6 @@ const char kPreloadScript[] = "preload";

const char kPreloadScripts[] = "preloadScripts";

// Like --preload, but the passed argument is an URL.
const char kPreloadURL[] = "preloadURL";

// Enable the node integration.
const char kNodeIntegration[] = "nodeIntegration";

Expand Down
1 change: 0 additions & 1 deletion shell/common/options_switches.h
Expand Up @@ -66,7 +66,6 @@ extern const char kOverlayHeight[];
extern const char kZoomFactor[];
extern const char kPreloadScript[];
extern const char kPreloadScripts[];
extern const char kPreloadURL[];
extern const char kNodeIntegration[];
extern const char kContextIsolation[];
extern const char kExperimentalFeatures[];
Expand Down
1 change: 0 additions & 1 deletion spec/static/main.js
Expand Up @@ -155,7 +155,6 @@ ipcMain.on('disable-preload-on-next-will-attach-webview', (event, id) => {
event.sender.once('will-attach-webview', (event, webPreferences, params) => {
params.src = `file://${path.join(__dirname, '..', 'fixtures', 'pages', 'webview-stripped-preload.html')}`;
delete webPreferences.preload;
delete webPreferences.preloadURL;
});
});

Expand Down
1 change: 1 addition & 0 deletions typings/internal-ambient.d.ts
Expand Up @@ -222,6 +222,7 @@ declare namespace NodeJS {
isOnline(): boolean;
isValidHeaderName: (headerName: string) => boolean;
isValidHeaderValue: (headerValue: string) => boolean;
fileURLToFilePath: (url: string) => string;
Net: any;
net: any;
createURLLoader(options: CreateURLLoaderOptions): URLLoader;
Expand Down
1 change: 0 additions & 1 deletion typings/internal-electron.d.ts
Expand Up @@ -97,7 +97,6 @@ declare namespace Electron {

interface WebPreferences {
disablePopups?: boolean;
preloadURL?: string;
embedder?: Electron.WebContents;
type?: 'backgroundPage' | 'window' | 'browserView' | 'remote' | 'webview' | 'offscreen';
}
Expand Down