Add listener in the renderer process for deeplink

Summary:
- Set userPrefferedPlugin for external deeplink
- Listen for deeplink in renderer process and navigate accordingly

Reviewed By: passy

Differential Revision: D10339035

fbshipit-source-id: 4de6249a0672f9ce02b0dfb78a4563302c308578
This commit is contained in:
Pritesh Nandgaonkar
2018-10-17 06:17:55 -07:00
committed by Facebook Github Bot
parent 4f914a655e
commit ce996ba8af
5 changed files with 91 additions and 7 deletions

View File

@@ -45,8 +45,8 @@ export class App extends React.Component<Props> {
); );
}); });
ipcRenderer.send('getLaunchTime'); ipcRenderer.send('getLaunchTime');
ipcRenderer.send('componentDidMount');
} }
render() { render() {
return ( return (
<FlexColumn fill={true}> <FlexColumn fill={true}>

View File

@@ -0,0 +1,32 @@
/**
* Copyright 2018-present Facebook.
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
* @format
*/
import {uriComponents} from '../application.js';
test('test parsing of deeplink URL', () => {
const url = 'flipper://app/plugin/meta/data';
let components = uriComponents(url);
expect(components).toEqual(['app', 'plugin', 'meta/data']);
});
test('test parsing of deeplink URL when arguments are less', () => {
const url = 'flipper://app/';
let components = uriComponents(url);
expect(components).toEqual(['app']);
});
test('test parsing of deeplink URL when url is null', () => {
// $FlowFixMe
let components = uriComponents(null);
expect(components).toEqual([]);
});
test('test parsing of deeplink URL when pattern does not match', () => {
const url = 'Some random string';
let components = uriComponents(url);
expect(components).toEqual([]);
});

View File

@@ -5,10 +5,27 @@
* @format * @format
*/ */
import {remote} from 'electron'; import {remote, ipcRenderer} from 'electron';
import type {Store} from '../reducers/index.js'; import type {Store} from '../reducers/index.js';
import type Logger from '../fb-stubs/Logger.js'; import type Logger from '../fb-stubs/Logger.js';
import {selectPlugin, userPreferredPlugin} from '../reducers/connections';
export const uriComponents = (url: string) => {
if (!url) {
return [];
}
const match: ?Array<string> = url.match(
/^flipper:\/\/([^\/]*)\/([^\/]*)\/?(.*)$/,
);
if (match) {
return (match
.map(decodeURIComponent)
.slice(1)
.filter(Boolean): Array<string>);
}
return [];
};
export default (store: Store, logger: Logger) => { export default (store: Store, logger: Logger) => {
const currentWindow = remote.getCurrentWindow(); const currentWindow = remote.getCurrentWindow();
currentWindow.on('focus', () => { currentWindow.on('focus', () => {
@@ -23,4 +40,25 @@ export default (store: Store, logger: Logger) => {
payload: false, payload: false,
}); });
}); });
ipcRenderer.on('flipper-deeplink', (event, url) => {
// flipper://<client>/<pluginId>/<payload>
const match = uriComponents(url);
if (match.length > 1) {
store.dispatch(
selectPlugin({
selectedApp: match[0],
selectedPlugin: match[1],
deepLinkPayload: match[2],
}),
);
}
});
ipcRenderer.on('flipper-deeplink-preferred-plugin', (event, url) => {
// flipper://<client>/<pluginId>/<payload>
const match = uriComponents(url);
if (match.length > 1) {
store.dispatch(userPreferredPlugin(match[1]));
}
});
}; };

View File

@@ -45,6 +45,10 @@ export type Action =
selectedApp: ?string, selectedApp: ?string,
}, },
} }
| {
type: 'SELECT_USER_PREFERRED_PLUGIN',
payload: string,
}
| { | {
type: 'SERVER_ERROR', type: 'SERVER_ERROR',
payload: ?string, payload: ?string,
@@ -178,7 +182,10 @@ export default function reducer(
userPreferredPlugin: selectedPlugin, userPreferredPlugin: selectedPlugin,
}; };
} }
case 'SELECT_USER_PREFERRED_PLUGIN': {
const {payload} = action;
return {...state, userPreferredPlugin: payload};
}
case 'NEW_CLIENT': { case 'NEW_CLIENT': {
const {payload} = action; const {payload} = action;
const {userPreferredApp, userPreferredPlugin} = state; const {userPreferredApp, userPreferredPlugin} = state;
@@ -249,3 +256,7 @@ export const selectPlugin = (payload: {
type: 'SELECT_PLUGIN', type: 'SELECT_PLUGIN',
payload, payload,
}); });
export const userPreferredPlugin = (payload: string): Action => ({
type: 'SELECT_USER_PREFERRED_PLUGIN',
payload,
});

View File

@@ -153,6 +153,13 @@ app.on('ready', function() {
} }
}); });
ipcMain.on('componentDidMount', event => {
if (deeplinkURL) {
win.webContents.send('flipper-deeplink-preferred-plugin', deeplinkURL);
deeplinkURL = null;
}
});
ipcMain.on('getLaunchTime', event => { ipcMain.on('getLaunchTime', event => {
if (launchStartTime) { if (launchStartTime) {
event.sender.send('getLaunchTime', launchStartTime); event.sender.send('getLaunchTime', launchStartTime);
@@ -161,7 +168,6 @@ ipcMain.on('getLaunchTime', event => {
launchStartTime = null; launchStartTime = null;
} }
}); });
// Define custom protocol handler. Deep linking works on packaged versions of the application! // Define custom protocol handler. Deep linking works on packaged versions of the application!
app.setAsDefaultProtocolClient('flipper'); app.setAsDefaultProtocolClient('flipper');
@@ -219,8 +225,5 @@ function tryCreateWindow() {
slashes: true, slashes: true,
}); });
win.loadURL(entryUrl); win.loadURL(entryUrl);
if (deeplinkURL) {
win.webContents.send('flipper-deeplink', deeplinkURL);
}
} }
} }