1// Copyright 2013 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_EXTENSIONS_INSTALL_TRACKER_H_
6#define CHROME_BROWSER_EXTENSIONS_INSTALL_TRACKER_H_
7
8#include <map>
9
10#include "base/observer_list.h"
11#include "base/prefs/pref_change_registrar.h"
12#include "base/scoped_observer.h"
13#include "chrome/browser/extensions/active_install_data.h"
14#include "chrome/browser/extensions/install_observer.h"
15#include "components/keyed_service/core/keyed_service.h"
16#include "content/public/browser/notification_observer.h"
17#include "content/public/browser/notification_registrar.h"
18#include "extensions/browser/extension_registry_observer.h"
19
20namespace content {
21class BrowserContext;
22}
23
24namespace extensions {
25
26class ExtensionPrefs;
27class ExtensionRegistry;
28
29class InstallTracker : public KeyedService,
30                       public content::NotificationObserver,
31                       public ExtensionRegistryObserver {
32 public:
33  InstallTracker(content::BrowserContext* browser_context,
34                 extensions::ExtensionPrefs* prefs);
35  virtual ~InstallTracker();
36
37  static InstallTracker* Get(content::BrowserContext* context);
38
39  void AddObserver(InstallObserver* observer);
40  void RemoveObserver(InstallObserver* observer);
41
42  // If an install is currently in progress for |extension_id|, returns details
43  // of the installation. This instance retains ownership of the returned
44  // pointer. Returns NULL if the extension is not currently being installed.
45  const ActiveInstallData* GetActiveInstall(
46      const std::string& extension_id) const;
47
48  // Registers an install initiated by the user to allow checking of duplicate
49  // installs. Download of the extension has not necessarily started.
50  // RemoveActiveInstall() must be called when install is complete regardless of
51  // success or failure. Consider using ScopedActiveInstall rather than calling
52  // this directly.
53  void AddActiveInstall(const ActiveInstallData& install_data);
54
55  // Deregisters an active install.
56  void RemoveActiveInstall(const std::string& extension_id);
57
58  void OnBeginExtensionInstall(
59      const InstallObserver::ExtensionInstallParams& params);
60  void OnBeginExtensionDownload(const std::string& extension_id);
61  void OnDownloadProgress(const std::string& extension_id,
62                          int percent_downloaded);
63  void OnBeginCrxInstall(const std::string& extension_id);
64  void OnFinishCrxInstall(const std::string& extension_id, bool success);
65  void OnInstallFailure(const std::string& extension_id);
66
67  // NOTE(limasdf): For extension [un]load and [un]installed, use
68  //                ExtensionRegistryObserver.
69
70  // Overriddes for KeyedService.
71  virtual void Shutdown() OVERRIDE;
72
73 private:
74  void OnAppsReordered();
75
76  // content::NotificationObserver implementation.
77  virtual void Observe(int type,
78                       const content::NotificationSource& source,
79                       const content::NotificationDetails& details) OVERRIDE;
80
81  // ExtensionRegistryObserver implementation.
82  virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
83                                    const Extension* extension,
84                                    bool is_update) OVERRIDE;
85
86  // Maps extension id to the details of an active install.
87  typedef std::map<std::string, ActiveInstallData> ActiveInstallsMap;
88  ActiveInstallsMap active_installs_;
89
90  ObserverList<InstallObserver> observers_;
91  content::NotificationRegistrar registrar_;
92  PrefChangeRegistrar pref_change_registrar_;
93  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
94      extension_registry_observer_;
95
96  DISALLOW_COPY_AND_ASSIGN(InstallTracker);
97};
98
99}  // namespace extensions
100
101#endif  // CHROME_BROWSER_EXTENSIONS_INSTALL_TRACKER_H_
102