12a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
22a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
32a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// found in the LICENSE file.
42a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
52a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#ifndef CHROME_BROWSER_EXTENSIONS_INSTALL_TRACKER_H_
62a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define CHROME_BROWSER_EXTENSIONS_INSTALL_TRACKER_H_
72a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
8116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <map>
9116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/observer_list.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/prefs/pref_change_registrar.h"
12116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "base/scoped_observer.h"
13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/extensions/active_install_data.h"
142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/extensions/install_observer.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "components/keyed_service/core/keyed_service.h"
162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_observer.h"
172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "content/public/browser/notification_registrar.h"
18116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "extensions/browser/extension_registry_observer.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
20c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochnamespace content {
21c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdochclass BrowserContext;
22c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch}
23c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace extensions {
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ExtensionPrefs;
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass ExtensionRegistry;
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class InstallTracker : public KeyedService,
30116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       public content::NotificationObserver,
31116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                       public ExtensionRegistryObserver {
322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) public:
335f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  InstallTracker(content::BrowserContext* browser_context,
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                 extensions::ExtensionPrefs* prefs);
352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ~InstallTracker();
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
37c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  static InstallTracker* Get(content::BrowserContext* context);
38c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void AddObserver(InstallObserver* observer);
402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveObserver(InstallObserver* observer);
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
42116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // If an install is currently in progress for |extension_id|, returns details
43116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // of the installation. This instance retains ownership of the returned
44116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // pointer. Returns NULL if the extension is not currently being installed.
45116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  const ActiveInstallData* GetActiveInstall(
46116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& extension_id) const;
47116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
48116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Registers an install initiated by the user to allow checking of duplicate
49116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // installs. Download of the extension has not necessarily started.
50116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // RemoveActiveInstall() must be called when install is complete regardless of
51116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // success or failure. Consider using ScopedActiveInstall rather than calling
52116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // this directly.
53116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void AddActiveInstall(const ActiveInstallData& install_data);
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
55116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Deregisters an active install.
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void RemoveActiveInstall(const std::string& extension_id);
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnBeginExtensionInstall(
59f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const InstallObserver::ExtensionInstallParams& params);
60effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void OnBeginExtensionDownload(const std::string& extension_id);
612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnDownloadProgress(const std::string& extension_id,
622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          int percent_downloaded);
63effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch  void OnBeginCrxInstall(const std::string& extension_id);
640529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void OnFinishCrxInstall(const std::string& extension_id, bool success);
652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnInstallFailure(const std::string& extension_id);
662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
67f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // NOTE(limasdf): For extension [un]load and [un]installed, use
68f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  //                ExtensionRegistryObserver.
69f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Overriddes for KeyedService.
712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Shutdown() OVERRIDE;
722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
730529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch private:
740529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  void OnAppsReordered();
750529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // content::NotificationObserver implementation.
772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void Observe(int type,
782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       const content::NotificationSource& source,
792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                       const content::NotificationDetails& details) OVERRIDE;
802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // ExtensionRegistryObserver implementation.
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                    const Extension* extension,
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                    bool is_update) OVERRIDE;
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Maps extension id to the details of an active install.
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  typedef std::map<std::string, ActiveInstallData> ActiveInstallsMap;
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ActiveInstallsMap active_installs_;
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ObserverList<InstallObserver> observers_;
912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  content::NotificationRegistrar registrar_;
922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  PrefChangeRegistrar pref_change_registrar_;
93116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      extension_registry_observer_;
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InstallTracker);
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)};
982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}  // namespace extensions
1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif  // CHROME_BROWSER_EXTENSIONS_INSTALL_TRACKER_H_
102