Home | History | Annotate | Download | only in app_list
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef CHROME_BROWSER_UI_APP_LIST_APPS_MODEL_BUILDER_H_
      6 #define CHROME_BROWSER_UI_APP_LIST_APPS_MODEL_BUILDER_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/gtest_prod_util.h"
     12 #include "chrome/browser/extensions/install_observer.h"
     13 #include "ui/app_list/app_list_model.h"
     14 #include "ui/base/models/list_model_observer.h"
     15 
     16 class AppListControllerDelegate;
     17 class ExtensionAppItem;
     18 class ExtensionSet;
     19 class Profile;
     20 
     21 namespace extensions {
     22 class Extension;
     23 class InstallTracker;
     24 }
     25 
     26 namespace gfx {
     27 class ImageSkia;
     28 }
     29 
     30 class AppsModelBuilder : public ui::ListModelObserver,
     31                          public extensions::InstallObserver {
     32  public:
     33   AppsModelBuilder(Profile* profile,
     34                    app_list::AppListModel::Apps* model,
     35                    AppListControllerDelegate* controller);
     36   virtual ~AppsModelBuilder();
     37 
     38   // Populates the model.
     39   void Build();
     40 
     41   // Returns app instance with id |extension_id|.
     42   ExtensionAppItem* GetApp(const std::string& extension_id);
     43 
     44  private:
     45   typedef std::vector<ExtensionAppItem*> Apps;
     46 
     47   // Overridden from extensions::InstallObserver:
     48   virtual void OnBeginExtensionInstall(const std::string& extension_id,
     49                                        const std::string& extension_name,
     50                                        const gfx::ImageSkia& installing_icon,
     51                                        bool is_app,
     52                                        bool is_platform_app) OVERRIDE;
     53 
     54   virtual void OnDownloadProgress(const std::string& extension_id,
     55                                   int percent_downloaded) OVERRIDE;
     56 
     57   virtual void OnInstallFailure(const std::string& extension_id) OVERRIDE;
     58   virtual void OnExtensionInstalled(
     59       const extensions::Extension* extension) OVERRIDE {}
     60   virtual void OnExtensionLoaded(
     61       const extensions::Extension* extension) OVERRIDE;
     62   virtual void OnExtensionUnloaded(
     63       const extensions::Extension* extension) OVERRIDE;
     64   virtual void OnExtensionUninstalled(
     65       const extensions::Extension* extension) OVERRIDE;
     66   virtual void OnAppsReordered() OVERRIDE;
     67   virtual void OnAppInstalledToAppList(
     68       const std::string& extension_id) OVERRIDE;
     69   virtual void OnShutdown() OVERRIDE;
     70 
     71   // Adds apps in |extensions| to |apps|.
     72   void AddApps(const ExtensionSet* extensions, Apps* apps);
     73 
     74   // Populates the model with apps.
     75   void PopulateApps();
     76 
     77   // Re-sort apps in case app ordinal prefs are changed.
     78   void ResortApps();
     79 
     80   // Inserts an app based on app ordinal prefs.
     81   void InsertApp(ExtensionAppItem* app);
     82 
     83   // Returns the index of the application app with |app_id| in |model_|. If
     84   // no match is found, returns -1.
     85   int FindApp(const std::string& app_id);
     86 
     87   // Sets which app is intended to be highlighted. Will remove the highlight
     88   // from a currently highlighted app.
     89   void SetHighlightedApp(const std::string& extension_id);
     90 
     91   // Sets the application app with |highlight_app_id_| in |model_| as
     92   // highlighted if |highlighted_app_pending_| is true. If such an app is found,
     93   // reset |highlighted_app_pending_| so that won't be highlighted again until
     94   // another call to SetHighlightedApp() is made.
     95   void UpdateHighlight();
     96 
     97   // Returns app instance at given |index|.
     98   ExtensionAppItem* GetAppAt(size_t index);
     99 
    100   // ui::ListModelObserver overrides:
    101   virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE;
    102   virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE;
    103   virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE;
    104   virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE;
    105 
    106   Profile* profile_;
    107   AppListControllerDelegate* controller_;
    108 
    109   // Sub apps model of AppListModel that represents apps grid view.
    110   app_list::AppListModel::Apps* model_;
    111 
    112   std::string highlight_app_id_;
    113 
    114   // True if we haven't set |highlight_app_id_| to be highlighted. This happens
    115   // if we try to highlight an app that doesn't exist in the list yet.
    116   bool highlighted_app_pending_;
    117 
    118   // True to ignore |model_| changes.
    119   bool ignore_changes_;
    120 
    121   // We listen to this to show app installing progress.
    122   extensions::InstallTracker* tracker_;
    123 
    124   DISALLOW_COPY_AND_ASSIGN(AppsModelBuilder);
    125 };
    126 
    127 #endif  // CHROME_BROWSER_UI_APP_LIST_APPS_MODEL_BUILDER_H_
    128