-
Notifications
You must be signed in to change notification settings - Fork 15.1k
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
Add ability to set per-session preload scripts #10430
Conversation
atom/browser/api/atom_api_window.h
Outdated
@@ -54,6 +54,10 @@ class Window : public mate::TrackableObject<Window>, | |||
|
|||
int32_t ID() const; | |||
|
|||
static void AddGlobalPreload(const base::FilePath::StringType preloadPath); | |||
static void RemoveGlobalPreload(const base::FilePath::StringType preloadPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be const base::FilePath::StringType&
(and std::vector<base::FilePath::StringType>&
below) to avoid a copies?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious what's the use case for this? Also, should we generalize this so that it can be used for other web contents as well (e.g. BrowserView
)?
@poiru Two main use cases for me
|
I'm pretty sure this already works for |
Ah, I see! Since |
I initially thought about putting them there but then decided against it because existing preloads go onto the |
I would suggest adding it to the session module #2605 |
@deepak1556 At the moment this isn't session scoped though, it's for all sessions. That issue refers to preloads for iframes as well, not sure this PR quite hits that issue perfectly. |
Having it scoped to session gives better control, doesn't have to worry about implementation for
If the scripts are added to the global session, then it would address the use case you mentioned too. Any reason why this shouldn't be session specific ? |
I think it is better to be a session-wide API instead of a global API. For single-session apps setting the preload script for the global session is as easy as setting the global preload script. For multi-session apps, like browsers that use different sessions for UI and remote pages, they almost never use the same preload script for different sessions. So an API setting global preload script would be useless for those apps, while an API settings session-wide preload script can meet most needs. |
7a6c22a
to
7ab1e42
Compare
@zcbenz @deepak1556 Moved the logic to be session based 👍 |
01f1a70
to
b719fe7
Compare
atom/browser/api/atom_api_session.h
Outdated
@@ -81,6 +82,9 @@ class Session: public mate::TrackableObject<Session>, | |||
void GetBlobData(const std::string& uuid, | |||
const AtomBlobReader::CompletionCallback& callback); | |||
void CreateInterruptedDownload(const mate::Dictionary& options); | |||
void AddPreload(const base::FilePath::StringType& preloadPath); | |||
void RemovePreload(const base::FilePath::StringType& preloadPath); | |||
std::vector<base::FilePath::StringType> GetPreloads(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use const std::vector<base::FilePath::StringType>&
to avoid copying.
atom/browser/api/atom_api_session.h
Outdated
@@ -103,6 +107,7 @@ class Session: public mate::TrackableObject<Session>, | |||
std::string devtools_network_emulation_client_id_; | |||
|
|||
scoped_refptr<AtomBrowserContext> browser_context_; | |||
std::vector<base::FilePath::StringType> g_preloads; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
preload_paths_
is probably a better name.
atom/browser/api/atom_api_session.h
Outdated
@@ -81,6 +82,9 @@ class Session: public mate::TrackableObject<Session>, | |||
void GetBlobData(const std::string& uuid, | |||
const AtomBlobReader::CompletionCallback& callback); | |||
void CreateInterruptedDownload(const mate::Dictionary& options); | |||
void AddPreload(const base::FilePath::StringType& preloadPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/preloadPath
/preload_path
atom::api::WebContents::CreateFrom(isolate, web_contents); | ||
auto session = atom::api::Session::CreateFrom( | ||
isolate, api_web_contents.get()->GetBrowserContext()); | ||
for (auto preloadPath : session->GetPreloads()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const auto&
to avoid copy. Also preload_path
.
auto session = atom::api::Session::CreateFrom( | ||
isolate, api_web_contents.get()->GetBrowserContext()); | ||
for (auto preloadPath : session->GetPreloads()) { | ||
if (base::FilePath(preloadPath).IsAbsolute()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to do this check in AddPreload()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency with existing preload logic I think it's better to leave this here
I'm rebasing this branch on master. |
3ffbc42
to
eaa4b71
Compare
By design the BrowserClient should not be aware of the api:: classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 👍
@@ -367,8 +368,6 @@ class WebContents : public mate::TrackableObject<WebContents>, | |||
const std::vector<base::string16>& labels); | |||
|
|||
private: | |||
AtomBrowserContext* GetBrowserContext() const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want this method to be public ? Its not used in this PR.
} // namespace | ||
|
||
// static | ||
int SessionPreferences::kLocatorKey = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it be better to have the key as SessionPreferencesUserData
just so it doesn't collide with anything from content layer in the future ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The static member data has the class name in its symbol name, so we won't get symbol conflicts. The kLocatorKey
is a pattern also used in other Chromium classes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
What would the js api look like for this? |
How will I know when this feature is released? (I've scoured the website looking for an RSS feed or a mailing list. Closest thing I can find is Twitter) |
@iffy You can subscribe to an Atom feed of the releases or tags for any repository. Whether or not a particular PR is "released" is rather more complicated but hopefully that should get you started. |
This adds three new instance methods to
session
objects that allow you to set preload scripts, these scripts will be executed before normal preload scripts and you can have as many as you want.API: