Update session module (except for webRequest)

This commit is contained in:
Milan Burda
2016-03-26 02:11:20 +01:00
parent 80d4209dd4
commit 4cede46324
2 changed files with 349 additions and 6 deletions
@@ -18,6 +18,7 @@ import {
nativeImage,
screen,
shell,
session,
hideInternalModules
} from 'electron';
@@ -675,3 +676,69 @@ shell.openExternal('https://github.com', {
});
shell.beep();
// session
// https://github.com/atom/electron/blob/master/docs/api/session.md
session.defaultSession.on('will-download', (event, item, webContents) => {
event.preventDefault();
require('request')(item.getURL(), (data: any) => {
require('fs').writeFileSync('/somewhere', data);
});
});
// Query all cookies.
session.defaultSession.cookies.get({}, (error, cookies) => {
console.log(cookies);
});
// Query all cookies associated with a specific url.
session.defaultSession.cookies.get({ url : "http://www.github.com" }, (error, cookies) => {
console.log(cookies);
});
// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
var cookie = { url : "http://www.github.com", name : "dummy_name", value : "dummy" };
session.defaultSession.cookies.set(cookie, (error) => {
if (error) {
console.error(error);
}
});
// In the main process.
session.defaultSession.on('will-download', (event, item, webContents) => {
// Set the save path, making Electron not to prompt a save dialog.
item.setSavePath('/tmp/save.pdf');
console.log(item.getMimeType());
console.log(item.getFilename());
console.log(item.getTotalBytes());
item.on('updated', function() {
console.log('Received bytes: ' + item.getReceivedBytes());
});
item.on('done', function(e, state) {
if (state == "completed") {
console.log("Download successfully");
} else {
console.log("Download is cancelled or interrupted that can't be resumed");
}
});
});
// To emulate a GPRS connection with 50kbps throughput and 500 ms latency.
session.defaultSession.enableNetworkEmulation({
latency: 500,
downloadThroughput: 6400,
uploadThroughput: 6400
});
// To emulate a network outage.
session.defaultSession.enableNetworkEmulation({
offline: true
});
session.defaultSession.setCertificateVerifyProc((hostname, cert, callback) => {
callback((hostname === 'github.com') ? true : false);
});
+282 -6
View File
@@ -3,40 +3,316 @@
// Definitions by: jedmao <https://github.com/jedmao/>, rhysd <https://rhysd.github.io>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
/// <reference path="github-electron.event-emitter.d.ts" />
/// <reference path="github-electron.web-contents.d.ts" />
declare namespace Electron {
class Session {
/**
* The session module can be used to create new Session objects.
* You can also access the session of existing pages by using
* the session property of webContents which is a property of BrowserWindow.
*/
class Session extends EventEmitter {
/**
* @returns a new Session instance from partition string.
*/
static fromPartition(partition: string): Session;
/**
* @returns the default session object of the app.
*/
static defaultSession: Session;
cookies: any;
/**
* Emitted when Electron is about to download item in webContents.
* Calling event.preventDefault() will cancel the download
* and item will not be available from next tick of the process.
*/
on(event: 'will-download', listener: (event: Event, item: DownloadItem, webContents: WebContents) => void): this;
on(event: string, listener: Function): this;
/**
* The cookies gives you ability to query and modify cookies.
*/
cookies: SessionCookies;
/**
* @returns the sessions current cache size.
*/
getCacheSize(callback: (size: number) => void): void;
/**
* Clears the sessions HTTP cache.
*/
clearCache(callback: Function): void;
/**
* Clears the data of web storages.
*/
clearStorageData(callback: Function): void;
/**
* Clears the data of web storages.
*/
clearStorageData(options: ClearStorageDataOptions, callback: Function): void;
/**
* Writes any unwritten DOMStorage data to disk.
*/
flushStorageData(): void;
/**
* Sets the proxy settings.
*/
setProxy(config: string, callback: Function): void;
/**
* Resolves the proxy information for url.
*/
resolveProxy(url: URL, callback: (proxy: any) => any): void;
/**
* Sets download saving directory.
* By default, the download directory will be the Downloads under the respective app folder.
*/
setDownloadPath(path: string): void;
/**
* Emulates network with the given configuration for the session.
*/
enableNetworkEmulation(options: NetworkEmulationOptions): void;
/**
* Disables any network emulation already active for the session.
* Resets to the original network configuration.
*/
disableNetworkEmulation(): void;
/**
* Sets the certificate verify proc for session.
*/
setCertificateVerifyProc(proc: CertificateVerifyProc): void;
/**
* Clears the host resolver cache.
*/
clearHostResolverCache(callback: Function): void;
/**
* The webRequest API set allows to intercept and modify contents of a request at various stages of its lifetime.
*/
webRequest: any;
}
interface ClearStorageDataOptions {
/**
* Should follow window.location.origins representation scheme://host:port.
*/
origin?: string;
storages?: string[];
quotas?: string[];
/**
* The types of storages to clear.
*/
storages?: ('appcache' | 'cookies' | 'filesystem' | 'indexdb' | 'localstorage' | 'shadercache' | 'websql' | 'serviceworkers')[];
/**
* The types of quotas to clear.
*/
quotas?: ('temporary' | 'persistent' | 'syncable')[];
}
interface NetworkEmulationOptions {
/**
* Whether to emulate network outage.
*/
offline?: boolean;
/**
* RTT in ms.
*/
latency?: number;
/**
* Download rate in Bps.
*/
downloadThroughput?: number;
/**
* Upload rate in Bps.
*/
uploadThroughput?: number;
}
interface CertificateVerifyProc {
(hostname: string, cert: any, callback: (accepted: boolean) => any): any;
}
/**
* DownloadItem represents a download item in Electron.
*/
class DownloadItem extends EventEmitter {
/**
* Emits when the downloadItem gets updated.
*/
on(event: 'updated', listener: Function): this;
/**
* Emits when the download is in a terminal state. This includes a completed download,
* a cancelled download (via downloadItem.cancel()), and interrupted download that cant be resumed.
*/
on(event: 'done', listener: (event: Event, state: 'completed' | 'cancelled' | 'interrupted') => void): this;
on(event: string, listener: Function): this;
/**
* Set the save file path of the download item.
* Note: The API is only available in sessions will-download callback function.
* If user doesnt set the save path via the API, Electron will use the original
* routine to determine the save path (Usually prompts a save dialog).
*/
setSavePath(path: string): void;
/**
* Pauses the download.
*/
pause(): void;
/**
* Resumes the download that has been paused.
*/
resume(): void;
/**
* Cancels the download operation.
*/
cancel(): void;
/**
* @returns The origin url where the item is downloaded from.
*/
getURL(): string;
/**
* @returns The mime type.
*/
getMimeType(): string;
/**
* @returns Whether the download has user gesture.
*/
hasUserGesture(): boolean;
/**
* @returns The file name of the download item.
* Note: The file name is not always the same as the actual one saved in local disk.
* If user changes the file name in a prompted download saving dialog,
* the actual name of saved file will be different.
*/
getFilename(): string;
/**
* @returns The total size in bytes of the download item. If the size is unknown, it returns 0.
*/
getTotalBytes(): number;
/**
* @returns The received bytes of the download item.
*/
getReceivedBytes(): number;
/**
* @returns The Content-Disposition field from the response header.
*/
getContentDisposition(): string;
}
interface CookieFilter {
/**
* Retrieves cookies which are associated with url. Empty implies retrieving cookies of all urls.
*/
url?: string;
/**
* Filters cookies by name.
*/
name?: string;
/**
* Retrieves cookies whose domains match or are subdomains of domains.
*/
domain?: string;
/**
* Retrieves cookies whose path matches path.
*/
path?: string;
/**
* Filters cookies by their Secure property.
*/
secure?: boolean;
/**
* Filters out session or persistent cookies.
*/
session?: boolean;
}
interface Cookie {
/**
* The name of the cookie.
*/
name: string;
/**
* The value of the cookie.
*/
value: string;
/**
* The domain of the cookie.
*/
domain: string;
/**
* Whether the cookie is a host-only cookie.
*/
hostOnly: string;
/**
* The path of the cookie.
*/
path: string;
/**
* Whether the cookie is marked as secure.
*/
secure: boolean;
/**
* Whether the cookie is marked as HTTP only.
*/
httpOnly: boolean;
/**
* Whether the cookie is a session cookie or a persistent cookie with an expiration date.
*/
session: boolean;
/**
* The expiration date of the cookie as the number of seconds since the UNIX epoch.
* Not provided for session cookies.
*/
expirationDate?: number;
}
interface CookieDetails {
/**
* The URL associated with the cookie.
*/
url: string;
/**
* The name of the cookie.
* Default: empty.
*/
name?: string;
/**
* The value of the cookie.
* Default: empty.
*/
value?: string;
/**
* The domain of the cookie.
* Default: empty.
*/
domain?: string;
/**
* The path of the cookie.
* Default: empty.
*/
path?: string;
/**
* Whether the cookie should be marked as secure.
* Default: false.
*/
secure?: boolean;
/**
* Whether the cookie should be marked as HTTP only.
* Default: false.
*/
httpOnly?: boolean;
/**
* The expiration date of the cookie as the number of seconds since the UNIX epoch.
* If omitted, the cookie becomes a session cookie.
*/
expirationDate?: number;
}
interface SessionCookies {
/**
* Sends a request to get all cookies matching filter.
*/
get(filter: CookieFilter, callback: (error: Error, cookies: Cookie[]) => void): void;
/**
* Sets the cookie with details.
*/
set(details: CookieDetails, callback: (error: Error) => void): void;
/**
* Removes the cookies matching url and name.
*/
remove(url: string, name: string, callback: Function): void;
}
}