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_SYNC_TEST_INTEGRATION_SYNC_EXTENSION_HELPER_H_
6#define CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_EXTENSION_HELPER_H_
7
8#include <map>
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13#include "base/compiler_specific.h"
14#include "base/memory/ref_counted.h"
15#include "base/memory/singleton.h"
16#include "extensions/common/manifest.h"
17
18class Profile;
19class SyncTest;
20
21namespace extensions {
22class Extension;
23}
24
25class SyncExtensionHelper {
26 public:
27  // Singleton implementation.
28  static SyncExtensionHelper* GetInstance();
29
30  // Initializes the profiles in |test| and registers them with
31  // internal data structures.
32  void SetupIfNecessary(SyncTest* test);
33
34  // Installs the extension with the given name to |profile|, and returns the
35  // extension ID of the new extension.
36  std::string InstallExtension(Profile* profile,
37                               const std::string& name,
38                               extensions::Manifest::Type type);
39
40  // Uninstalls the extension with the given name from |profile|.
41  void UninstallExtension(Profile* profile, const std::string& name);
42
43  // Returns a vector containing the names of all currently installed extensions
44  // on |profile|.
45  std::vector<std::string> GetInstalledExtensionNames(Profile* profile) const;
46
47  // Enables the extension with the given name on |profile|.
48  void EnableExtension(Profile* profile, const std::string& name);
49
50  // Disables the extension with the given name on |profile|.
51  void DisableExtension(Profile* profile, const std::string& name);
52
53  // Returns true if the extension with the given name is enabled on |profile|.
54  bool IsExtensionEnabled(Profile* profile, const std::string& name) const;
55
56  // Enables the extension with the given name to run in incognito mode
57  void IncognitoEnableExtension(Profile* profile, const std::string& name);
58
59  // Disables the extension with the given name from running in incognito mode
60  void IncognitoDisableExtension(Profile* profile, const std::string& name);
61
62  // Returns true iff the extension is enabled in incognito mode on |profile|.
63  bool IsIncognitoEnabled(Profile* profile, const std::string& name) const;
64
65  // Returns true iff the extension with the given id is pending
66  // install in |profile|.
67  bool IsExtensionPendingInstallForSync(
68      Profile* profile, const std::string& id) const;
69
70  // Installs all extensions pending sync in |profile|.
71  void InstallExtensionsPendingForSync(Profile* profile);
72
73  // Returns true iff |profile1| and |profile2| have the same extensions and
74  // they are all in the same state.
75  static bool ExtensionStatesMatch(Profile* profile1, Profile* profile2);
76
77 private:
78  struct ExtensionState {
79    enum EnabledState { DISABLED, PENDING, ENABLED };
80
81    ExtensionState();
82    ~ExtensionState();
83    bool Equals(const ExtensionState &other) const;
84
85    EnabledState enabled_state;
86    bool incognito_enabled;
87  };
88
89  typedef std::map<std::string, ExtensionState> ExtensionStateMap;
90  typedef std::map<std::string, scoped_refptr<extensions::Extension> >
91      ExtensionNameMap;
92  typedef std::map<Profile*, ExtensionNameMap> ProfileExtensionNameMap;
93  typedef std::map<std::string, std::string> StringMap;
94  typedef std::map<std::string, extensions::Manifest::Type> TypeMap;
95
96  friend struct DefaultSingletonTraits<SyncExtensionHelper>;
97
98  SyncExtensionHelper();
99  ~SyncExtensionHelper();
100
101  // Returns a map from |profile|'s installed extensions to their state.
102  static ExtensionStateMap GetExtensionStates(Profile* profile);
103
104  // Initializes extensions for |profile| and creates an entry in
105  // |profile_extensions_| for it.
106  void SetupProfile(Profile* profile);
107
108  // Returns an extension for the given name in |profile|.  type and
109  // index.  Two extensions with the name but different profiles will
110  // have the same id.
111  scoped_refptr<extensions::Extension> GetExtension(
112      Profile* profile, const std::string& name,
113      extensions::Manifest::Type type) WARN_UNUSED_RESULT;
114
115  ProfileExtensionNameMap profile_extensions_;
116  StringMap id_to_name_;
117  TypeMap id_to_type_;
118  bool setup_completed_;
119
120  DISALLOW_COPY_AND_ASSIGN(SyncExtensionHelper);
121};
122
123#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_EXTENSION_HELPER_H_
124