@@ -11,7 +11,7 @@ import {
1111 ResizeType ,
1212} from './common'
1313
14- const debug = require ( 'debug' ) ( 'electron-chrome-extensions:browserAction' )
14+ const d = require ( 'debug' ) ( 'electron-chrome-extensions:browserAction' )
1515
1616if ( ! app . isReady ( ) ) {
1717 protocol . registerSchemesAsPrivileged ( [ { scheme : 'crx' , privileges : { bypassCSP : true } } ] )
@@ -147,6 +147,8 @@ export class BrowserActionAPI {
147147 handle (
148148 'browserAction.setIcon' ,
149149 ( event , { tabId, ...details } : chrome . browserAction . TabIconDetails ) => {
150+ // TODO: icon paths need to be resolved relative to the sender url. In
151+ // the case of service workers, we need an API to get the script url.
150152 setDetails ( event , { tabId, icon : details } , 'icon' )
151153 setDetails ( event , { tabId, iconModified : Date . now ( ) } , 'iconModified' )
152154 } ,
@@ -206,16 +208,11 @@ export class BrowserActionAPI {
206208 this . removeActions ( extension . id )
207209 } )
208210
209- session . protocol . registerBufferProtocol ( 'crx' , this . handleCrxRequest )
211+ session . protocol . handle ( 'crx' , this . handleCrxRequest )
210212 }
211213
212- private handleCrxRequest = (
213- request : Electron . ProtocolRequest ,
214- callback : ( response : Electron . ProtocolResponse ) => void ,
215- ) => {
216- debug ( '%s' , request . url )
217-
218- let response : Electron . ProtocolResponse
214+ private handleCrxRequest = ( request : GlobalRequest ) : GlobalResponse => {
215+ d ( '%s' , request . url )
219216
220217 try {
221218 const url = new URL ( request . url )
@@ -255,33 +252,33 @@ export class BrowserActionAPI {
255252 const imageData = matchSize ( iconDetails . imageData as any , imageSize , resizeType )
256253 iconImage = imageData ? nativeImage . createFromDataURL ( imageData ) : undefined
257254 }
255+
256+ if ( iconImage ?. isEmpty ( ) ) {
257+ d ( 'crx: icon image is empty' , iconDetails )
258+ }
258259 }
259260
260261 if ( iconImage ) {
261- response = {
262- statusCode : 200 ,
263- mimeType : 'image/png' ,
264- data : iconImage . toPNG ( ) ,
265- }
266- } else {
267- response = { statusCode : 400 }
262+ return new Response ( iconImage . toPNG ( ) , {
263+ status : 200 ,
264+ headers : {
265+ 'Content-Type' : 'image/png' ,
266+ } ,
267+ } )
268268 }
269269
270- break
270+ d ( 'crx: no icon image for %s' , extensionId )
271+ return new Response ( null , { status : 400 } )
271272 }
272273 default : {
273- response = { statusCode : 400 }
274+ d ( 'crx: invalid request %s' , requestType )
275+ return new Response ( null , { status : 400 } )
274276 }
275277 }
276278 } catch ( e ) {
277279 console . error ( e )
278-
279- response = {
280- statusCode : 500 ,
281- }
280+ return new Response ( null , { status : 500 } )
282281 }
283-
284- callback ( response )
285282 }
286283
287284 private getAction ( extensionId : string ) {
@@ -370,7 +367,7 @@ export class BrowserActionAPI {
370367 if ( type != 'frame' ) return
371368 const { eventType, extensionId, tabId } = details
372369
373- debug (
370+ d (
374371 `activate [eventType: ${ eventType } , extensionId: '${ extensionId } ', tabId: ${ tabId } , senderId: ${ sender ?. id } ]` ,
375372 )
376373
@@ -394,7 +391,7 @@ export class BrowserActionAPI {
394391 this . popup . destroy ( )
395392 this . popup = undefined
396393 if ( toggleExtension ) {
397- debug ( 'skipping activate to close popup' )
394+ d ( 'skipping activate to close popup' )
398395 return
399396 }
400397 }
@@ -421,11 +418,11 @@ export class BrowserActionAPI {
421418 anchorRect,
422419 } )
423420
424- debug ( `opened popup: ${ popupUrl } ` )
421+ d ( `opened popup: ${ popupUrl } ` )
425422
426423 this . ctx . emit ( 'browser-action-popup-created' , this . popup )
427424 } else {
428- debug ( `dispatching onClicked for ${ extensionId } ` )
425+ d ( `dispatching onClicked for ${ extensionId } ` )
429426
430427 const tabDetails = this . ctx . store . tabDetailsCache . get ( tab . id )
431428 this . ctx . router . sendEvent ( extensionId , 'browserAction.onClicked' , tabDetails )
@@ -481,7 +478,7 @@ export class BrowserActionAPI {
481478 append ( {
482479 label : 'Remove extension' ,
483480 click : ( ) => {
484- debug ( `removing extension "${ extension . name } " (${ extension . id } )` )
481+ d ( `removing extension "${ extension . name } " (${ extension . id } )` )
485482 this . ctx . session . removeExtension ( extension . id )
486483 } ,
487484 } )
@@ -499,7 +496,7 @@ export class BrowserActionAPI {
499496 ? this . ctx . store . getWindowById ( options . windowId )
500497 : this . ctx . store . getCurrentWindow ( )
501498 if ( ! window || window . isDestroyed ( ) ) {
502- debug ( 'openPopup: window %d destroyed' , window ?. id )
499+ d ( 'openPopup: window %d destroyed' , window ?. id )
503500 return
504501 }
505502
@@ -523,7 +520,8 @@ export class BrowserActionAPI {
523520 this . queuedUpdate = true
524521 queueMicrotask ( ( ) => {
525522 this . queuedUpdate = false
526- debug ( `dispatching update to ${ this . observers . size } observer(s)` )
523+ if ( this . observers . size === 0 ) return
524+ d ( `dispatching update to ${ this . observers . size } observer(s)` )
527525 Array . from ( this . observers ) . forEach ( ( observer ) => {
528526 if ( ! observer . isDestroyed ( ) ) {
529527 observer . send ?.( 'browserAction.update' )
0 commit comments