
        Services.obs.removeObserver(this, "browser-delayed-startup-finished");
        this.init();
        break;
    }
  },

  shutdown() {
    if (this.initialized) {
      lazy.AddonManagerPrivate.unregisterProvider(this);
      this.initialized = false;
      this.clearAddonCache();
    }
  },

  init() {
    if (!lazy.LOCAL_MODEL_MANAGEMENT_ENABLED || this.initialized) {
      return;
    }

    this.initialized = true;
    lazy.AddonManagerPrivate.registerProvider(this, [MODELHUB_ADDON_TYPE]);
    this.modelHub = new lazy.ModelHub();
  },

  async onUninstalled(addon) {
    if (!this.cache.has(addon.id)) {
      return;
    }
    this.cache.delete(addon.id);
    lazy.AddonManagerPrivate.callAddonListeners("onUninstalled", addon);
  },

  clearAddonCache() {
    this.cache.clear();
  },

  getWrapperIdForModel(model) {
    return [
      lazy.computeSha256HashAsString(`${model.name}:${model.revision}`),
      MODELHUB_ADDON_ID_SUFFIX,
    ].join("");
  },

  async refreshAddonCache() {
    // Return earlier if the model hub provider was disabled.
    // by the time it was being called.
    if (!lazy.LOCAL_MODEL_MANAGEMENT_ENABLED) {
      return;
    }

    this.clearAddonCache();

    const models = await this.modelHub.listModels();

    for (const model of models) {
      const { metadata } = await this.modelHub.listFiles({
        model: model.name,
        revision: model.revision,
      });

      const modelIconURL = await this.modelHub.getOwnerIcon(model.name);
      const id = this.getWrapperIdForModel(model);

      const wrapper = new ModelHubAddonWrapper({
        provider: this,
        id,
        model: model.name,
        version: model.revision,
        totalSize: metadata.totalSize,
        lastUsed: new Date(metadata.lastUsed),
        updateDate: new Date(metadata.updateDate),
        modelIconURL,
        engineIds: metadata.engineIds,
      });
      this.cache.set(wrapper.id, wrapper);
    }
  },
};

Services.obs.addObserver(ModelHubProvider, "browser-delayed-startup-finished");
PK