 
 # Retrieve the active URL shortener service.
 getActiveUrlShortener = ->
+  log.trace()
   # Attempt to lookup enabled URL shortener service.
   shortener = ext.queryUrlShortener (shortener) ->
     shortener.isEnabled()
-  return shortener if shortener?
-  # Should never reach here but we'll return goo.gl service by default after
-  # ensuring it's the active URL shortener service from now on to save some
-  # time.
-  store.modify 'googl', (googl) ->
-    googl.enabled = yes
-  getActiveUrlShortener()
+  unless shortener?
+    # Should never reach here but we'll return goo.gl service by default after
+    # ensuring it's the active URL shortener service from now on to save some
+    # time in the future.
+    store.modify 'googl', (googl) ->
+      googl.enabled = yes
+    shortener = getActiveUrlShortener()
+  log.debug "Getting details for #{shortener.title} URL shortener"
+  shortener
 
 # Background page setup
 # ---------------------
 
-ext = window.ext =
+ext = window.ext = new class Extension extends utils.Class
 
   # Public constants
   # ----------------


  # Update the toolbar/browser action depending on the current settings.
  updateToolbar: ->
    image    = 'images/icon_19.png'
    key      = store.get 'toolbar.key'
    template = getTemplateWithKey key if key
    title    = i18n.get 'name'
    buildPopup()
    if not template or store.get 'toolbar.popup'
      # Use Template's details to style the browser action.
      chrome.browserAction.setIcon  path:  utils.url image
      chrome.browserAction.setTitle title: title
      # Show the popup when the browser action is clicked.
      chrome.browserAction.setPopup popup: 'pages/popup.html'
    else
      # Replace Template's details with that of the selected template.
      if store.get 'toolbar.style'
        image = getImagePathForTemplate template if template.image isnt 0


# Attempt to retrieve the template with the specified `key`.
getTemplateWithKey = (key) ->
  log.trace()
  ext.queryTemplate (template) ->
    template.key is key

# Attempt to retrieve the template with the specified `menuId`.
getTemplateWithMenuId = (menuId) ->
  log.trace()
  ext.queryTemplate (template) ->
    template.menuId is menuId

# Attempt to retrieve the template with the specified keyboard `shortcut`.  
# Exclude disabled templates from this query.
getTemplateWithShortcut = (shortcut) ->
  log.trace()
  ext.queryTemplate (template) ->
    template.enabled and template.shortcut is shortcut

# Determine whether or not the specified extension is blacklisted.
isBlacklisted = (extensionId) ->
  log.trace()
  return yes for extension in BLACKLIST when extension is extensionId
  no

# Determine whether or not the specified extension is active on `tab`.
isExtensionActive = (tab, extensionId) ->
  log.trace()
  log.debug "Checking activity of #{extensionId}"
  isSpecialPage(tab) and tab.url.indexOf(extensionId) isnt -1

# Determine whether or not `tab` is currently displaying a page on the Chrome
# Extension Gallery (i.e. Web Store).
isExtensionGallery = (tab) ->
  log.trace()
  tab.url.indexOf('https://chrome.google.com/webstore') is 0

# Determine whether or not `tab` is currently displaying a *protected* page
# (i.e. a page that content scripts cannot be executed on).
isProtectedPage = (tab) ->
  log.trace()
  isSpecialPage(tab) or isExtensionGallery tab

# Determine whether or not `tab` is currently displaying a *special* page (i.e.
# a page that is internal to the browser).
isSpecialPage = (tab) ->
  log.trace()
  tab.url.indexOf('chrome') is 0

# Ensure `null` is returned instead of `object` if it is *empty*.
nullIfEmpty = (object) ->
  log.trace()
  if $.isEmptyObject object then null else object

# Listener for internal requests to the extension.  
# External requests are also routed through here, but only after being checked
# that they do not originate from a blacklisted extension.
onRequest = (request, sender, sendResponse) ->
  log.trace()
  # Don't allow shortcut requests when shortcuts are disabled.
  if request.type is 'shortcut' and not store.get 'shortcuts'
    return sendResponse?()
