1// Copyright 2014 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_APPS_DRIVE_DRIVE_APP_PROVIDER_H_
6#define CHROME_BROWSER_APPS_DRIVE_DRIVE_APP_PROVIDER_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/macros.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/memory/scoped_vector.h"
15#include "base/memory/weak_ptr.h"
16#include "chrome/browser/drive/drive_app_registry_observer.h"
17#include "extensions/browser/extension_registry_observer.h"
18
19namespace drive {
20struct DriveAppInfo;
21}
22
23class BrowserContextKeyedServiceFactory;
24class DriveAppConverter;
25class DriveAppMapping;
26class DriveServiceBridge;
27class ExtensionService;
28class Profile;
29
30// DriveAppProvider is the integration point for Drive apps. It ensures each
31// Drive app has a corresponding Chrome app in the extension system. If there
32// is no matching Chrome app, a local URL app would be created. The class
33// processes app changes from both DriveAppRegistry and extension system to
34// keep the two in sync.
35class DriveAppProvider : public drive::DriveAppRegistryObserver,
36                         public extensions::ExtensionRegistryObserver {
37 public:
38  explicit DriveAppProvider(Profile* profile);
39  virtual ~DriveAppProvider();
40
41  // Appends PKS factories this class depends on.
42  static void AppendDependsOnFactories(
43      std::set<BrowserContextKeyedServiceFactory*>* factories);
44
45  void SetDriveServiceBridgeForTest(scoped_ptr<DriveServiceBridge> test_bridge);
46
47 private:
48  friend class DriveAppProviderTest;
49
50  typedef std::set<std::string> IdSet;
51  typedef std::vector<drive::DriveAppInfo> DriveAppInfos;
52
53  // Maps |drive_app_id| to |new_app|'s id in mapping. Auto uninstall existing
54  // mapped app if it is an URL app.
55  void UpdateMappingAndExtensionSystem(const std::string& drive_app_id,
56                                       const extensions::Extension* new_app,
57                                       bool is_new_app_generated);
58
59  // Deferred processing of relevant extension installed message.
60  void ProcessDeferredOnExtensionInstalled(const std::string drive_app_id,
61                                           const std::string chrome_app_id);
62
63  void SchedulePendingConverters();
64  void OnLocalAppConverted(const DriveAppConverter* converter, bool success);
65
66  bool IsMappedUrlAppUpToDate(const drive::DriveAppInfo& drive_app) const;
67
68  void AddOrUpdateDriveApp(const drive::DriveAppInfo& drive_app);
69  void ProcessRemovedDriveApp(const std::string& drive_app_id);
70
71  // drive::DriveAppRegistryObserver overrides:
72  virtual void OnDriveAppRegistryUpdated() OVERRIDE;
73
74  // extensions::ExtensionRegistryObserver overrides:
75  virtual void OnExtensionInstalled(
76      content::BrowserContext* browser_context,
77      const extensions::Extension* extension,
78      bool is_update) OVERRIDE;
79  virtual void OnExtensionUninstalled(
80      content::BrowserContext* browser_context,
81      const extensions::Extension* extension,
82      extensions::UninstallReason reason) OVERRIDE;
83
84  Profile* profile_;
85
86  scoped_ptr<DriveServiceBridge> service_bridge_;
87  scoped_ptr<DriveAppMapping> mapping_;
88  DriveAppInfos drive_apps_;
89
90  // Tracks the pending web app convertions.
91  ScopedVector<DriveAppConverter> pending_converters_;
92
93  base::WeakPtrFactory<DriveAppProvider> weak_ptr_factory_;
94
95  DISALLOW_COPY_AND_ASSIGN(DriveAppProvider);
96};
97
98#endif  // CHROME_BROWSER_APPS_DRIVE_DRIVE_APP_PROVIDER_H_
99