<commit_before>/// <reference path="../typings/firefox-content-script.d.ts" />

// the 'content script' for the extension's main panel.
//
// This is loaded before the front-end itself so that it can set up
// the browser integration interface (an implementation of page_access.PageAccess)
// for use by the app when it loads.

import collectionutil = require('../../../lib/base/collectionutil');
import forms = require('../../../webui/forms');
import stringutil = require('../../../lib/base/stringutil');
import page_access = require('../../../webui/page_access');
import rpc = require('../../../lib/net/rpc');
import site_info_service = require('../../../lib/siteinfo/service');

var selfWorker: ContentWorker = <any>self;

var OAUTH_REDIRECT_URL = 'http://localhost:8234';

if (stringutil.startsWith(window.location.href, OAUTH_REDIRECT_URL)) {
	selfWorker.port.emit('oauth-credentials-received', window.location.hash);
}

var pageAccess = createObjectIn<page_access.ExtensionConnector>(unsafeWindow, { defineAs: 'firefoxAddOn' });
var addonRpc = new rpc.RpcHandler(selfWorker.port);
var appRpc = new rpc.RpcHandler(new rpc.WindowMessagePort(document.defaultView, '*', 'extension-core', 'extension-app'));
appRpc.clone = (data) => {
	return cloneInto(data, unsafeWindow);
};

pageAccess.syncService = selfWorker.options.syncService;
pageAccess.oauthRedirectUrl = OAUTH_REDIRECT_URL;

appRpc.onAsync('find-fields', (done) => {
	addonRpc.call('find-fields', [], (err: any, fields: forms.InputField[]) => {
		done(err, fields);
	});
});

appRpc.onAsync('autofill', (done: (err: any, count: number) => void, fields: forms.AutoFillEntry[]) => {
	addonRpc.call('autofill', [fields], (err: any, count: number) => {
		done(err, count);
	});
});

appRpc.onAsync('fetch-url', (done: (err:any, info: site_info_service.UrlResponse) => void, url: string) => {
	addonRpc.call('fetch-url', [url], (err: any, info: site_info_service.UrlResponse) => {
		done(err, info);
	});
});

appRpc.on('hide-panel', () => {
	addonRpc.call('hide-panel', []);
});

appRpc.on<void>('copy', (mimeType: string, data: string) => {
	addonRpc.call('copy', [mimeType, data]);
});

addonRpc.on<void>('pagechanged', (url: string) => {
	pageAccess.currentUrl = url;
	appRpc.call('pagechanged', [url]);
});

addonRpc.on('show', () => {
	appRpc.call('show', []);
});

// notify the add-on that the panel content script is ready.
// Note that the panel content script is loaded _before_
// the front-end so this event does not indicate that
// the front-end itself is ready.
addonRpc.call('ready', []);
<commit_msg>Simplify and improve documentation of logic for forwarding messages from app UI to Firefox extension core<commit_after>/// <reference path="../typings/firefox-content-script.d.ts" />

// the 'content script' for the extension's main panel.
//
// This is loaded before the front-end itself so that it can set up
// the browser integration interface (an implementation of page_access.PageAccess)
// for use by the app when it loads.

import collectionutil = require('../../../lib/base/collectionutil');
import forms = require('../../../webui/forms');
import stringutil = require('../../../lib/base/stringutil');
import page_access = require('../../../webui/page_access');
import rpc = require('../../../lib/net/rpc');
import site_info_service = require('../../../lib/siteinfo/service');

var selfWorker: ContentWorker = <any>self;

var OAUTH_REDIRECT_URL = 'http://localhost:8234';

if (stringutil.startsWith(window.location.href, OAUTH_REDIRECT_URL)) {
	selfWorker.port.emit('oauth-credentials-received', window.location.hash);
}

var pageAccess = createObjectIn<page_access.ExtensionConnector>(unsafeWindow, { defineAs: 'firefoxAddOn' });

// setup handler that sends/receives messages from the priviledged add-on
// code (in src/main.ts)
var extensionRpc = new rpc.RpcHandler(selfWorker.port);

// setup handler that sends/receives messages from the unpriviledged UI
// for passcards
var appUiRpc = new rpc.RpcHandler(new rpc.WindowMessagePort(document.defaultView, '*', 'extension-core', 'extension-app'));
appUiRpc.clone = (data) => {
	return cloneInto(data, unsafeWindow);
};

pageAccess.syncService = selfWorker.options.syncService;
pageAccess.oauthRedirectUrl = OAUTH_REDIRECT_URL;

// forwards an RPC request from the app UI to the priviledged extension code,
// which in turn may forward the request to the active tab. When the main
// extension script returns a reply, that is forwarded back to the app UI
// via the `done` callback
var forwardAsyncRpcCall = (method: string, done: Function, ...args: any[]) => {
	extensionRpc.call(method, args, (err: any, ...result: any[]) => {
		done.apply(null, [err].concat(result));
	});
}

appUiRpc.onAsync('find-fields', forwardAsyncRpcCall.bind(null, 'find-fields'));
appUiRpc.onAsync('autofill', forwardAsyncRpcCall.bind(null, 'autofill'));
appUiRpc.onAsync('fetch-url', forwardAsyncRpcCall.bind(null, 'fetch-url'));

appUiRpc.on('hide-panel', () => {
	extensionRpc.call('hide-panel', []);
});

appUiRpc.on<void>('copy', (mimeType: string, data: string) => {
	extensionRpc.call('copy', [mimeType, data]);
});

extensionRpc.on<void>('pagechanged', (url: string) => {
	pageAccess.currentUrl = url;
	appUiRpc.call('pagechanged', [url]);
});

extensionRpc.on('show', () => {
	appUiRpc.call('show', []);
});

// notify the add-on that the panel content script is ready.
// Note that the panel content script is loaded _before_
// the front-end so this event does not indicate that
// the front-end itself is ready.
extensionRpc.call('ready', []);

{'fraction_non_alphanumeric': 0.17525588076854012, 'fraction_numerical': 0.0014365236128568864, 'mean_word_length': 4.706967213114754, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1767342', 'n_tokens_mistral': 1806, 'n_tokens_neox': 1789, 'n_words': 445}