-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from HunnySajid/refactor/background
Refactor/background
- Loading branch information
Showing
24 changed files
with
551 additions
and
494 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
const CS = "cs-"; // content-script | ||
const UI = "ui-"; // pages like popup, new tab etc | ||
const EXTERNAL = "external-"; // onExternalMessage | ||
|
||
const EVENT_TYPE = { | ||
action_icon: "action-icon", | ||
authentication: "authentication", | ||
create_resource: "create-resource", | ||
delete_resource: "delete-resource", | ||
fetch_resource: "fetch-resource", | ||
update_resource: "update-resource", | ||
vendor_info: "vendor-info", | ||
}; | ||
|
||
export const CS_EVENTS = { | ||
action_icon_set: `${CS}-${EVENT_TYPE.action_icon}-set`, | ||
action_icon_unset: `${CS}-${EVENT_TYPE.action_icon}-unset`, | ||
action_icon_set_tab: `${CS}-${EVENT_TYPE.action_icon}-set-tab`, | ||
action_icon_unset_tab: `${CS}-${EVENT_TYPE.action_icon}-unset-tab`, | ||
|
||
fetch_resource_auto_signin_signature: `${CS}-${EVENT_TYPE.fetch_resource}-auto-signin-signature`, | ||
fetch_resource_tab_signin: `${CS}-${EVENT_TYPE.fetch_resource}-tab-signin`, | ||
|
||
vendor_info_get_vendor_data: `${CS}-${EVENT_TYPE.vendor_info}-get-vendor-data`, | ||
vendor_info_attempt_set_vendor_url: `${CS}-${EVENT_TYPE.vendor_info}-attempt-set-vendor-url`, | ||
|
||
authentication_check_agent_connection: `${CS}-${EVENT_TYPE.authentication}-check-agent-connection`, | ||
authentication_get_signed_headers: `${CS}-${EVENT_TYPE.authentication}-get-signed-headers`, | ||
}; | ||
|
||
export const UI_EVENTS = { | ||
action_icon_unset: `${UI}-${EVENT_TYPE.action_icon}-unset`, | ||
authentication_check_agent_connection: `${UI}-${EVENT_TYPE.authentication}-check-agent-connection`, | ||
authentication_disconnect_agent: `${UI}-${EVENT_TYPE.authentication}-disconnect-agent`, | ||
authentication_connect_agent: `${UI}-${EVENT_TYPE.authentication}-connect-agent`, | ||
authentication_boot_connect_agent: `${UI}-${EVENT_TYPE.authentication}-boot-connect-agent`, | ||
authentication_generate_passcode: `${UI}-${EVENT_TYPE.authentication}-generate-passcode`, | ||
|
||
create_resource_signin: `${UI}-${EVENT_TYPE.create_resource}-signin`, | ||
create_resource_identifier: `${UI}-${EVENT_TYPE.create_resource}-identifier`, | ||
|
||
fetch_resource_identifiers: `${UI}-${EVENT_TYPE.fetch_resource}-identifiers`, | ||
fetch_resource_signins: `${UI}-${EVENT_TYPE.fetch_resource}-signins`, | ||
fetch_resource_credentials: `${UI}-${EVENT_TYPE.fetch_resource}-credentials`, | ||
|
||
update_resource_auto_signin: `${UI}-${EVENT_TYPE.update_resource}-auto-signin`, | ||
delete_resource_signins: `${UI}-${EVENT_TYPE.delete_resource}-signins`, | ||
}; | ||
|
||
export const EXTERNAL_EVENTS = { | ||
fetch_resource_auto_signin_signature: `${EXTERNAL}-${EVENT_TYPE.fetch_resource}-auto-signin-signature`, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { IHandler } from "@config/types"; | ||
|
||
export function handleSetActionIcon({ sendResponse }: IHandler) { | ||
chrome.action.setBadgeBackgroundColor({ color: "#008000" }, () => { | ||
chrome.action.setBadgeText({ text: "1" }); | ||
sendResponse({ data: { success: true } }); | ||
}); | ||
} | ||
|
||
export function handleUnsetActionIcon({ sendResponse }: IHandler) { | ||
chrome.action.setBadgeText({ text: "" }); | ||
sendResponse({ data: { success: true } }); | ||
} | ||
|
||
export function handleSetTabActionIcon({ sendResponse, tabId }: IHandler) { | ||
chrome.action.setBadgeBackgroundColor({ color: "#008000" }, () => { | ||
chrome.action.setBadgeText({ tabId: tabId, text: "1" }); | ||
sendResponse({ data: { success: true } }); | ||
}); | ||
} | ||
|
||
export function handleUnsetTabActionIcon({ sendResponse, tabId }: IHandler) { | ||
chrome.action.setBadgeText({ tabId: tabId, text: "" }); | ||
sendResponse({ data: { success: true } }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { signifyService } from "@pages/background/services/signify"; | ||
import { userService } from "@pages/background/services/user"; | ||
import { IHandler } from "@config/types"; | ||
|
||
export async function handleCheckAgentConnection({ | ||
sendResponse, | ||
url, | ||
}: IHandler) { | ||
const isConnected = await signifyService.isConnected(); | ||
sendResponse({ data: { isConnected, tabUrl: url } }); | ||
} | ||
|
||
export async function handleDisconnectAgent({ sendResponse }: IHandler) { | ||
await signifyService.disconnect(); | ||
sendResponse({ data: { isConnected: false } }); | ||
} | ||
|
||
export async function handleConnectAgent({ sendResponse, data }: IHandler) { | ||
const resp = (await signifyService.connect( | ||
data.agentUrl, | ||
data.passcode | ||
)) as any; | ||
if (resp?.error) { | ||
// TODO: improve error messages | ||
// Current messages are not descrptive enough e.g | ||
// bran must be 21 characters | ||
// agent does not exist for controller <controller-id> | ||
// using custom error message for now instead of resp?.error?.message | ||
|
||
sendResponse({ | ||
error: { | ||
code: 404, | ||
message: resp?.error?.message, | ||
}, | ||
}); | ||
} else { | ||
await userService.setPasscode(data.passcode); | ||
sendResponse({ data: { success: true } }); | ||
} | ||
} | ||
|
||
export async function handleBootConnectAgent({ sendResponse, data }: IHandler) { | ||
const resp = (await signifyService.bootAndConnect( | ||
data.agentUrl, | ||
data.bootUrl, | ||
data.passcode | ||
)) as any; | ||
if (resp?.error) { | ||
sendResponse({ | ||
error: { | ||
code: 404, | ||
message: resp?.error?.message, | ||
}, | ||
}); | ||
} else { | ||
await userService.setPasscode(data.passcode); | ||
sendResponse({ data: { success: true } }); | ||
} | ||
} | ||
|
||
export async function handleGeneratePasscode({ sendResponse, data }: IHandler) { | ||
const passcode = signifyService.generatePasscode(); | ||
sendResponse({ data: { passcode } }); | ||
} | ||
|
||
export async function handleGetSignedHeaders({ | ||
sendResponse, | ||
url, | ||
data, | ||
}: IHandler) { | ||
const resp = await signifyService.getSignedHeaders({ | ||
url: url!, | ||
signin: data.signin, | ||
}); | ||
|
||
sendResponse({ | ||
data: resp, | ||
}); | ||
} |
Oops, something went wrong.