Skip to content
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

refactor(content): simplify MonetizationLinkManager #538

Merged
merged 44 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
a42ffce
add MonetizationLinkManager: basics of link available on page load
sidvishnoi Aug 23, 2024
659c831
stop/resume on visibilitychange
sidvishnoi Aug 23, 2024
fcee306
nit: rename param
sidvishnoi Aug 23, 2024
810cb7b
watch for add/remove of link tags
sidvishnoi Aug 23, 2024
dd9f59b
handle onmonetization attribute change
sidvishnoi Aug 23, 2024
ee0fad9
handle element attribute changes; move things around
sidvishnoi Aug 23, 2024
0b5707f
onAddedNode/onAddedLink; nits
sidvishnoi Aug 23, 2024
1fec4d8
stop first on href change
sidvishnoi Aug 23, 2024
04154a8
Merge branch 'main' into refactor-tag-manager
sidvishnoi Aug 26, 2024
2056b5f
handle toggleWM within background only
sidvishnoi Aug 26, 2024
2df5eb6
remove todo notes/comments
sidvishnoi Aug 26, 2024
038cfed
remove unused types
sidvishnoi Aug 26, 2024
bfa8d64
bring in iframe support with existing frameMananger
sidvishnoi Aug 26, 2024
c53b7e1
remove monetizationTagManager; use monetizationLinkManager
sidvishnoi Aug 26, 2024
b192f05
update payload types (extract *PayloadEntry out)
sidvishnoi Aug 26, 2024
5885b5f
nit: extract postMessage call to private method
sidvishnoi Aug 26, 2024
1a9920e
add types for Window.postMessage messages
sidvishnoi Aug 26, 2024
d1e837f
nit
sidvishnoi Aug 27, 2024
0bea75c
implement MonetizationLinkManager.end (unlisten everything)
sidvishnoi Aug 27, 2024
8bf32a2
don't define polyfill again on re-inject
sidvishnoi Aug 27, 2024
76f2e92
cleanup MonetizationLinkManager on disconnect
sidvishnoi Aug 27, 2024
525d35b
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 6, 2024
32662ae
rename CHECK_WALLET_ADDRESS_URL to GET_WALLET_ADDRESS_INFO
sidvishnoi Sep 6, 2024
729e14f
move checkWalletAddressUrlFormat into monetizationLinkManager
sidvishnoi Sep 6, 2024
5e47bfc
move isNotNull to shared/helpers
sidvishnoi Sep 6, 2024
1b84570
move isDocumentReady in
sidvishnoi Sep 6, 2024
d061527
don't use statics, keep helpers separately testable
sidvishnoi Sep 6, 2024
ce72dfb
move context-invalidation bug related code out of this PR
sidvishnoi Sep 6, 2024
0155835
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 9, 2024
d351645
nit: simplify toggleWM logic
sidvishnoi Sep 9, 2024
52e5f87
move getMonetizationLinkTags into MonetizationLinkManager
sidvishnoi Sep 9, 2024
0aec471
frameManager: move HANDLED_MESSAGES to top-level constant
sidvishnoi Sep 9, 2024
92d3122
style: import order
sidvishnoi Sep 9, 2024
c7babdb
remove unused types
sidvishnoi Sep 9, 2024
c9ab817
move checkHrefFormat into class also
sidvishnoi Sep 9, 2024
15937b1
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 10, 2024
3cad011
fix iframe integration (tried method reuse too much)
sidvishnoi Sep 10, 2024
f2543d9
add resume payload length check
sidvishnoi Sep 11, 2024
34157be
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 11, 2024
5e735ed
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 12, 2024
7ba33cc
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 13, 2024
4d61c2d
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 16, 2024
cdaafed
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 16, 2024
34e46d0
Merge branch 'main' into refactor-tag-manager
sidvishnoi Sep 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/background/services/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export class Background {
// endregion

// region Content
case 'CHECK_WALLET_ADDRESS_URL':
case 'GET_WALLET_ADDRESS_INFO':
return success(
await getWalletInformation(message.payload.walletAddressUrl),
);
Expand Down
15 changes: 10 additions & 5 deletions src/background/services/monetization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class MonetizationService {
}

async startPaymentSession(
payload: StartMonetizationPayload[],
payload: StartMonetizationPayload,
sender: Runtime.MessageSender,
) {
if (!payload.length) {
Expand Down Expand Up @@ -146,7 +146,7 @@ export class MonetizationService {
}

async stopPaymentSession(
payload: StopMonetizationPayload[],
payload: StopMonetizationPayload,
sender: Runtime.MessageSender,
) {
let needsAdjustAmount = false;
Expand Down Expand Up @@ -191,7 +191,7 @@ export class MonetizationService {
}

async resumePaymentSession(
payload: ResumeMonetizationPayload[],
payload: ResumeMonetizationPayload,
sender: Runtime.MessageSender,
) {
const tabId = getTabId(sender);
Expand Down Expand Up @@ -243,8 +243,13 @@ export class MonetizationService {

async toggleWM() {
const { enabled } = await this.storage.get(['enabled']);
await this.storage.set({ enabled: !enabled });
await this.message.sendToActiveTab('EMIT_TOGGLE_WM', { enabled: !enabled });
const nowEnabled = !enabled;
await this.storage.set({ enabled: nowEnabled });
if (nowEnabled) {
await this.resumePaymentSessionActiveTab();
} else {
this.stopAllSessions();
}
}

async pay(amount: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/content/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { asClass, asValue, createContainer, InjectionMode } from 'awilix';
import browser, { type Browser } from 'webextension-polyfill';
import { createLogger, Logger } from '@/shared/logger';
import { ContentScript } from './services/contentScript';
import { MonetizationTagManager } from './services/monetizationTagManager';
import { MonetizationLinkManager } from './services/monetizationLinkManager';
import { LOG_LEVEL } from '@/shared/defines';
import { FrameManager } from './services/frameManager';
import {
Expand All @@ -16,7 +16,7 @@ export interface Cradle {
document: Document;
window: Window;
message: MessageManager<ContentToBackgroundMessage>;
monetizationTagManager: MonetizationTagManager;
monetizationLinkManager: MonetizationLinkManager;
frameManager: FrameManager;
contentScript: ContentScript;
}
Expand All @@ -39,7 +39,7 @@ export const configureContainer = () => {
.inject(() => ({
logger: logger.getLogger('content-script:frameManager'),
})),
monetizationTagManager: asClass(MonetizationTagManager)
monetizationLinkManager: asClass(MonetizationLinkManager)
.singleton()
.inject(() => ({
logger: logger.getLogger('content-script:tagManager'),
Expand Down
30 changes: 22 additions & 8 deletions src/content/messages.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
export enum ContentToContentAction {
INITIALIZE_IFRAME = 'INITIALIZE_IFRAME',
IS_MONETIZATION_ALLOWED_ON_START = 'IS_MONETIZATION_ALLOWED_ON_START',
IS_MONETIZATION_ALLOWED_ON_RESUME = 'IS_MONETIZATION_ALLOWED_ON_RESUME',
IS_MONETIZATION_ALLOWED_ON_STOP = 'IS_MONETIZATION_ALLOWED_ON_STOP',
START_MONETIZATION = 'START_MONETIZATION',
STOP_MONETIZATION = 'STOP_MONETIZATION',
RESUME_MONETIZATION = 'RESUME_MONETIZATION',
import type {
ResumeMonetizationPayload,
StartMonetizationPayload,
StopMonetizationPayload,
} from '@/shared/messages';

export interface ContentToContentMessageMap {
INITIALIZE_IFRAME: void;
IS_MONETIZATION_ALLOWED_ON_START: StartMonetizationPayload;
IS_MONETIZATION_ALLOWED_ON_RESUME: ResumeMonetizationPayload;
IS_MONETIZATION_ALLOWED_ON_STOP: StopMonetizationPayload;
START_MONETIZATION: StartMonetizationPayload;
STOP_MONETIZATION: StopMonetizationPayload;
RESUME_MONETIZATION: ResumeMonetizationPayload;
}

export type ContentToContentMessage = {
[K in keyof ContentToContentMessageMap]: {
message: K;
id: string;
payload: ContentToContentMessageMap[K];
};
}[keyof ContentToContentMessageMap];
18 changes: 6 additions & 12 deletions src/content/services/contentScript.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ToContentMessage } from '@/shared/messages';
import { failure } from '@/shared/helpers';
import type { Cradle } from '@/content/container';
import { failure } from '@/shared/helpers';

export class ContentScript {
private browser: Cradle['browser'];
private window: Cradle['window'];
private logger: Cradle['logger'];
private monetizationTagManager: Cradle['monetizationTagManager'];
private monetizationLinkManager: Cradle['monetizationLinkManager'];
private frameManager: Cradle['frameManager'];

private isFirstLevelFrame: boolean;
Expand All @@ -16,14 +16,14 @@ export class ContentScript {
browser,
window,
logger,
monetizationTagManager,
monetizationLinkManager,
frameManager,
}: Cradle) {
Object.assign(this, {
browser,
window,
logger,
monetizationTagManager,
monetizationLinkManager,
frameManager,
});

Expand All @@ -40,7 +40,7 @@ export class ContentScript {

if (this.isTopFrame) this.frameManager.start();

this.monetizationTagManager.start();
this.monetizationLinkManager.start();
}
}

Expand All @@ -50,16 +50,10 @@ export class ContentScript {
try {
switch (message.action) {
case 'MONETIZATION_EVENT':
this.monetizationTagManager.dispatchMonetizationEvent(
this.monetizationLinkManager.dispatchMonetizationEvent(
message.payload,
);
return;

case 'EMIT_TOGGLE_WM':
this.monetizationTagManager.toggleWM(message.payload);

return;

default:
return;
}
Expand Down
53 changes: 23 additions & 30 deletions src/content/services/frameManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
import { ContentToContentAction } from '../messages';
import type { ContentToContentMessage } from '../messages';
import type {
ResumeMonetizationPayload,
StartMonetizationPayload,
ResumeMonetizationPayloadEntry,
StartMonetizationPayloadEntry,
StopMonetizationPayload,
} from '@/shared/messages';
import type { Cradle } from '@/content/container';

const HANDLED_MESSAGES: ContentToContentMessage['message'][] = [
'INITIALIZE_IFRAME',
'IS_MONETIZATION_ALLOWED_ON_START',
'IS_MONETIZATION_ALLOWED_ON_RESUME',
];

export class FrameManager {
private window: Cradle['window'];
private document: Cradle['document'];
Expand Down Expand Up @@ -103,7 +109,7 @@ export class FrameManager {

const frameDetails = this.frames.get(frame);

const stopMonetizationTags: StopMonetizationPayload[] =
const stopMonetizationTags: StopMonetizationPayload =
frameDetails?.requestIds.map((requestId) => ({
requestId,
intent: 'remove',
Expand Down Expand Up @@ -179,18 +185,13 @@ export class FrameManager {
private bindMessageHandler() {
this.window.addEventListener(
'message',
(event: any) => {
(event: MessageEvent<ContentToContentMessage>) => {
const { message, payload, id } = event.data;
if (
![
ContentToContentAction.INITIALIZE_IFRAME,
ContentToContentAction.IS_MONETIZATION_ALLOWED_ON_START,
ContentToContentAction.IS_MONETIZATION_ALLOWED_ON_RESUME,
].includes(message)
) {
if (!HANDLED_MESSAGES.includes(message)) {
return;
}
const frame = this.findIframe(event.source);
const eventSource = event.source as Window;
const frame = this.findIframe(eventSource);
if (!frame) {
event.stopPropagation();
return;
Expand All @@ -199,50 +200,42 @@ export class FrameManager {
if (event.origin === this.window.location.href) return;

switch (message) {
case ContentToContentAction.INITIALIZE_IFRAME:
case 'INITIALIZE_IFRAME':
event.stopPropagation();
this.frames.set(frame, {
frameId: id,
requestIds: [],
});
return;

case ContentToContentAction.IS_MONETIZATION_ALLOWED_ON_START:
case 'IS_MONETIZATION_ALLOWED_ON_START':
event.stopPropagation();
if (frame.allow === 'monetization') {
this.frames.set(frame, {
frameId: id,
requestIds: payload.map(
(p: StartMonetizationPayload) => p.requestId,
(p: StartMonetizationPayloadEntry) => p.requestId,
),
});
event.source.postMessage(
{
message: ContentToContentAction.START_MONETIZATION,
id,
payload,
},
eventSource.postMessage(
{ message: 'START_MONETIZATION', id, payload },
'*',
);
}

return;

case ContentToContentAction.IS_MONETIZATION_ALLOWED_ON_RESUME:
case 'IS_MONETIZATION_ALLOWED_ON_RESUME':
event.stopPropagation();
if (frame.allow === 'monetization') {
this.frames.set(frame, {
frameId: id,
requestIds: payload.map(
(p: ResumeMonetizationPayload) => p.requestId,
(p: ResumeMonetizationPayloadEntry) => p.requestId,
),
});
event.source.postMessage(
{
message: ContentToContentAction.RESUME_MONETIZATION,
id,
payload,
},
eventSource.postMessage(
{ message: 'RESUME_MONETIZATION', id, payload },
'*',
);
}
Expand Down
Loading