AboutSupportDeveloper GuideVersion 41.131.100.46

This class handles Platform actions. It does not need to be used directly by developers. However, its methods can be overriden by passing an overrideCallback to Platform.init in order to implement custom Platform behavior. (See Platform.init)

For an overview of Provider customization, see https://developers.openfin.co/docs/platform-customization#section-customizing-platform-behavior.

interface PlatformProvider {
    applySnapshot(payload: ApplySnapshotPayload, identity?: Identity): Promise<void>;
    checkViewsForPreventUnload(views: View[]): Promise<ViewStatuses>;
    closeView(payload: CloseViewPayload, identity?: Identity): Promise<any>;
    closeWindow(payload: CloseWindowPayload, callerIdentity: Identity): Promise<void>;
    createView(payload: CreateViewPayload, identity: Identity): Promise<View>;
    createWindow(options: PlatformWindowCreationOptions, identity?: Identity): Promise<Window>;
    fetchManifest(payload: FetchManifestPayload, callerIdentity: Identity): Promise<any>;
    getSnapshot(payload: undefined, identity: Identity): Promise<Snapshot>;
    getUserDecisionForBeforeUnload(payload: ViewsPreventingUnloadPayload): Promise<BeforeUnloadUserDecision>;
    getViewsForWindowClose(windowId: Identity): Promise<View[]>;
    getWindowContext(payload: GetWindowContextPayload, identity?: Identity): Promise<any>;
    getWindowSnapshot(identity: Identity, callerIdentity: Identity): Promise<WindowCreationOptions>;
    handleFailedViewCreation(viewIdentity: Identity, error: Error): Promise<void | ViewCreationSuccess>;
    handleRunRequested(payload: RunRequestedEvent): Promise<void>;
    handleViewsAndWindowClose(windowId: Identity, userDecision: BeforeUnloadUserDecision): Promise<void>;
    onWindowContextUpdated(payload: OptionsChangedEvent): Promise<undefined | HostContextChangedPayload>;
    positionOutOfBoundsWindows(snapshot: Snapshot, outOfBoundsWindows: WindowCreationOptions[]): Promise<WindowCreationOptions[]>;
    quit(payload: undefined, identity: Identity): Promise<void>;
    replaceLayout(payload: ReplaceLayoutPayload, identity?: Identity): Promise<void>;
    replaceView(payload: ReplaceViewPayload, identity?: Identity): Promise<void>;
    setWindowContext(payload: SetWindowContextPayload, identity?: Identity): Promise<any>;
}

Methods

  • Handles requests to apply a snapshot to the current Platform.

    Parameters

    • payload: ApplySnapshotPayload

      Payload containing the snapshot to be applied, as well as any options.

    • Optionalidentity: Identity

      Identity of the entity that called Platform.applySnapshot. Undefined if called internally (e.g. when opening the initial snapshot).

    Returns Promise<void>

    Allows overriding a platform's default snapshot behavior. All calls to Platform.applySnapshot will pass through the override.

    const overrideCallback = (Provider) => {
    class Override extends Provider {
    async applySnapshot(payload, callerIdentity) {
    const { snapshot, options } = payload;
    // Perhaps in our platform, we wish to always use the closeExistingWindows option
    return super.applySnapshot(
    { snapshot, options: { ...options, closeExistingWindows: true }},
    callerIdentity
    );
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • It takes in an array of Views and returns an object specifying which of them are trying to prevent an unload and which are not.

    Parameters

    • views: View[]

      Array of Views

    Returns Promise<ViewStatuses>

  • Closes a view

    Parameters

    Returns Promise<any>

    Allows overriding a platform's default view closing behavior. All calls to Platform.closeView will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async closeView(payload, callerIdentity) {
    const { view } = payload;
    // Perhaps in our platform, we want to take a snapshot and save it somewhere before closing certain views.
    if (view.name === 'my-special-view') {
    const snapshot = await fin.Platform.getCurrentSync().getSnapshot();
    await saveSnapshot(snapshot);
    }
    return super.closeView(options, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });

    async function saveSnapshot(snapshot) {
    // Save the snapshot in localstorage, send to a server, etc.
    }
  • Closes a Window. By default it will fire any before unload handler set by a View in the Window. This can be disabled by setting skipBeforeUnload in the options object of the payload. This method is called by Platform.closeWindow.

    Parameters

    Returns Promise<void>

    This method calls a number of Platform Provider methods:

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async closeWindow(payload, callerIdentity) {
    const { windowId: { uuid, name }, options: { skipBeforeUnload } } = payload;
    console.log(`${uuid}/${name} is closing and skipBeforeUnload is set to: ${skipBeforeUnload}`);
    return super.closeWindow(payload, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Creates a new view and attaches it to a specified target window.

    Parameters

    Returns Promise<View>

  • Handles requests to create a window in the current platform.

    Parameters

    Returns Promise<Window>

    If createWindow was called as part of applying a snapshot or creating a view without a target window, identity will be undefined.

    Allows overriding a platform's default window creation behavior. All calls to Platform.createWindow will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async createWindow(options, callerIdentity) {
    if (options.reason === 'tearout') {
    // Perhaps in our platform, we want tearout windows to have a certain initial context value
    const customContext = {
    security: 'STOCK',
    currentView: 'detailed'
    }
    return super.createWindow({ ...options, customContext }, callerIdentity);
    }
    // default behavior for all other creation reasons
    return super.createWindow(options, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Handles requests to fetch manifests in the current platform.

    Parameters

    Returns Promise<any>

    If fetchManifest was called internally, callerIdentity will be the provider's identity.

    Allows overriding a platform's default behavior when fetching manifests. Overwriting this call will change how Platform.launchContentManifest and fins links fetch manifests. All calls to Platform.fetchManifest will also pass through the override.

    const overrideCallback = (Provider) => {
    class Override extends Provider {
    async fetchManifest(payload, callerIdentity) {
    const { manifestUrl } = payload;

    // I want to fetch certain URLs from the platform provider so that renderer-side cookies will be sent
    if (manifestUrl === 'https://www.my-internal-manifest.com') {
    const manifest = await fetch(manifestUrl);
    return manifest.json();
    }

    // Any requests that might be cross-origin should still be sent from the browser process
    return super.fetchManifest(payload);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Gets the current state of windows and their views and returns a snapshot object containing that info.

    Parameters

    • payload: undefined

      Undefined unless you've defined a custom getSnapshot protocol.

    • identity: Identity

      Identity of the entity that called Platform.getSnapshot.

    Returns Promise<Snapshot>

    Snapshot of current platform state.

    Allows overriding a platform's default snapshot behavior. All calls to Platform.getSnapshot will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    // Extend default behavior
    class MyOverride extends PlatformProvider {
    async getSnapshot(payload, callerIdentity) {
    // Call super to access vanilla platform behavior
    const snapshot = await super.getSnapshot();
    // Perform any additional logic needed
    const modifiedSnapshot = { ...snapshot, answer: 42 }
    await saveSnapshotToServer(modifiedSnapshot);
    return modifiedSnapshot;
    }
    }
    // Return instance with methods to be consumed by Platform
    return new MyOverride();
    };
    fin.Platform.init({ overrideCallback });

    async function saveSnapshotToServer(snapshot) {
    // Send a snapshot to the server, store it locally somewhere, etc.
    }
  • Handle the decision of whether a Window or specific View should close when trying to prevent an unload. This is meant to be overridden. Called in PlatformProvider.closeWindow. Normally you would use this method to show a dialog indicating that there are Views that are trying to prevent an unload. By default it will always return all Views passed into it as meaning to close.

    Parameters

    Returns Promise<BeforeUnloadUserDecision>

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async getUserDecisionForBeforeUnload(payload, callerIdentity) {
    const { windowShouldClose, viewsPreventingUnload, viewsNotPreventingUnload, windowId, closeType } = payload;

    // launch dialog and wait for user response
    const continueWithClose = await showDialog(viewsPreventingUnload, windowId, closeType);

    if (continueWithClose) {
    return { windowShouldClose, viewsToClose: [...viewsNotPreventingUnload, ...viewsPreventingUnload] };
    } else {
    return { windowShouldClose: false, viewsToClose: [] };
    }
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });

    async function showDialog(viewsPreventingUnload, windowId, closeType) {
    // Show a dialog and await for user response
    }
  • Gets all the Views attached to a Window that should close along with it. This is meant to be overridable in the case where you want to return other Views that may not be attached to the Window that is closing.

    Parameters

    Returns Promise<View[]>

  • Handles requests to get a window's context. target may be a window or a view. If it is a window, that window's customContext will be returned. If it is a view, the customContext of that view's current host window will be returned.

    Parameters

    • payload: GetWindowContextPayload

      Object containing the requested context update, the target's identity, and the target's entityType.

    • Optionalidentity: Identity

      Identity of the entity that called Platform.getWindowContext. Undefined when getWindowContext is called internally (e.g. when getting a window's context for the purpose of raising a "host-context-changed" event on a reparented view).

    Returns Promise<any>

    The new context.

    Allows overriding a platform's default getWindowContext behavior. All calls to Platform.getWindowContext will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async getWindowContext(payload, callerIdentity) {
    // Perhaps in our platform, we wish to make window context available only to views from a given domain.
    const { entityType } = callerIdentity;
    if (entityType === 'view') {
    const { url } = await fin.View.wrapSync(callerIdentity).getInfo();
    if (!url.startsWith('https://my.trusted-domain.com')) {
    throw new Error('Only apps from trusted domains may use window context in this platform.');
    }
    }
    return super.getWindowContext(payload, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Experimental

    This API is called during the () call. Gets the current state of a particular window and its views and returns an object that can be added to the OpenFin.Snapshot.windows property. Override this function if you wish to mutate each window snapshot during the () call

    Parameters

    Returns Promise<WindowCreationOptions>

  • Experimental

    This method is called to handle errors with view creation and initial navigation during layout creation. Overriding this method enables catching the error and correcting behavior (ie navigating to an error page).

    • To indicate that the error has been handled, view creation should be treated as successful return {identity: viewIdentity, success: true}.
    • Not returning anything will cause the layout-initialized event to include the view as failed with the original error.
    • Throwing an error will update the error on the event payload of layout-initialized to the thrown error.

    Parameters

    • viewIdentity: Identity

      Identity of the view

    • error: Error

      error thrown during a view creation

    Returns Promise<void | ViewCreationSuccess>

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async handleFailedViewCreation(viewIdentity, error) {
    const view = fin.View.wrapSync(viewId);
    try {
    // navigate to an error page
    await view.navigate('http://localhost:3000/errorPage.html');
    // resolve a success result after redirecting to a error page
    return {identity: viewIdentity, success: true};
    } catch(e) {
    // layout-initialized event will include this view with the error
    throw error;
    }
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Handles subsequent launch attempts of the current platform. Attempts to launch appManifestUrl passed as userAppConfigArgs. If no appManifestUrl is present will attempt to launch using the requesting manifest snapshot. If no appManifestUrl or snapshot is available nothing will be launched.

    Parameters

    Returns Promise<void>

  • Called when a window's customContext is updated. Responsible for raising the host-context-updated event on that window's child views.

    Parameters

    • payload: OptionsChangedEvent

      The event payload for the window whose context has changed. The new context will be contained as payload.diff.customContext.newVal.

    Returns Promise<undefined | HostContextChangedPayload>

    The event that it raised.

  • Called when a snapshot is being applied and some windows in that snapshot would be fully or partially off-screen. Returns an array of windows with modified positions, such that any off-screen windows are positioned in the top left corner of the main monitor.

    Parameters

    • snapshot: Snapshot

      The snapshot to be applied.

    • outOfBoundsWindows: WindowCreationOptions[]

      An array of WindowOptions for any windows that would be off-screen.

    Returns Promise<WindowCreationOptions[]>

    An array of WindowOptions with their position modified to fit on screen.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async positionOutOfBoundsWindows(
    snapshot, // The snapshot currently being applied
    outOfBoundsWindows // An array of options for all windows that would be fully or partially off-screen.
    ) {
    // By default, this function cascades out-of-bounds windows from the top left of the main monitor.
    // Perhaps we wish to instead position all such windows on the bottom right.

    // First, find the coordinates of the bottom right of the primary monitor
    const {
    primaryMonitor: {
    availableRect: { right, bottom }
    }
    } = await fin.System.getMonitorInfo();

    // Update the coordinates of each out-of-bounds window to be bottom right
    const newWindows = snapshot.windows.map((windowOptions) => {
    if (outOfBoundsWindows.find(w => w.name === windowOptions.name)) {
    // If the window is out of bounds, set a new initial top and left
    const { defaultHeight, defaultWidth } = windowOptions;
    return {
    ...windowOptions,
    defaultTop: bottom - defaultHeight,
    defaultLeft: right - defaultWidth
    }
    } else {
    return windowOptions;
    }
    })

    // Return all windows with desired bounds
    return newWindows;
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });
  • Closes the current Platform and all child windows and views.

    Parameters

    • payload: undefined

      Undefined unless you have implemented a custom quite protocol.

    • identity: Identity

      Identity of the entity that called Platform.quit.

    Returns Promise<void>

    Allows overriding a platform's default shutdown behavior. All calls to Platform.quit will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async quit(payload, callerIdentity) {
    // Perhaps in our platform, we wish to take a snapshot and save it somewhere before shutting down
    const snapshot = await this.getSnapshot();
    await saveSnapshot(snapshot);
    return super.quit(payload, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });

    async function saveSnapshot(snapshot) {
    // Save the snapshot in localstorage, send to a server, etc.
    }
  • Replaces a Platform window's layout with a new layout. Any views that were in the old layout but not the new layout will be destroyed.

    Parameters

    • payload: ReplaceLayoutPayload

      Contains the target window and an opts object with a layout property to apply.

    • Optionalidentity: Identity

      Identity of the entity that called Platform.replaceLayout. Undefined if replaceLayout is called internally (e.g. while applying a snapshot).

    Returns Promise<void>

    Allows overriding a platform's default replaceLayout behavior. All calls to Platform#replaceLayout Platform.replaceLayout will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async replaceLayout(payload, callerIdentity) {
    // Perhaps in our platform, we wish to save an updated snapshot each time a layout is replaced
    await super.replaceLayout(payload, callerIdentity);
    const updatedSnapshot = await fin.Platform.getCurrentSync().getSnapshot();
    await saveSnapshot(updatedSnapshot);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });

    async function saveSnapshot(snapshot) {
    // Save the snapshot in localstorage, send to a server, etc.
    }
  • Parameters

    Returns Promise<void>

  • Handles requests to set a window's context. target may be a window or a view. If it is a window, that window's customContext will be updated. If it is a view, the customContext of that view's current host window will be updated.

    Parameters

    • payload: SetWindowContextPayload

      Object containing the requested context update, the target's identity, and the target's entityType.

    • Optionalidentity: Identity

      Identity of the entity that called Platform.setWindowContext. Undefined if setWindowContext is called internally (e.g. while applying a snapshot).

    Returns Promise<any>

    The new context.

    Allows overriding a platform's default setWindowContext behavior. All calls to Platform.setWindowContext will pass through the override.

    const overrideCallback = (PlatformProvider) => {
    class Override extends PlatformProvider {
    async setWindowContext(payload, callerIdentity) {
    // Perhaps in our platform, we wish to only take context updates from windows
    // and ignore updates sent from views.
    if (payload.entityType === 'view') {
    throw new Error('Updating window context from a view is not permitted in this platform.');
    }
    return super.setWindowContext(payload, callerIdentity);
    }
    }
    return new Override();
    }

    fin.Platform.init({ overrideCallback });