sync_extension_helper.h revision f2477e01787aa58f445919b809d89e252beef54f
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| of the given
71  // type.
72  void InstallExtensionsPendingForSync(Profile* profile);
73
74  // Returns true iff |profile1| and |profile2| have the same extensions and
75  // they are all in the same state.
76  static bool ExtensionStatesMatch(Profile* profile1, Profile* profile2);
77
78 private:
79  struct ExtensionState {
80    enum EnabledState { DISABLED, PENDING, ENABLED };
81
82    ExtensionState();
83    ~ExtensionState();
84    bool Equals(const ExtensionState &other) const;
85
86    EnabledState enabled_state;
87    bool incognito_enabled;
88  };
89
90  typedef std::map<std::string, ExtensionState> ExtensionStateMap;
91  typedef std::map<std::string, scoped_refptr<extensions::Extension> >
92      ExtensionNameMap;
93  typedef std::map<Profile*, ExtensionNameMap> ProfileExtensionNameMap;
94  typedef std::map<std::string, std::string> StringMap;
95  typedef std::map<std::string, extensions::Manifest::Type> TypeMap;
96
97  friend struct DefaultSingletonTraits<SyncExtensionHelper>;
98
99  SyncExtensionHelper();
100  ~SyncExtensionHelper();
101
102  // Returns a map from |profile|'s installed extensions to their state.
103  static ExtensionStateMap GetExtensionStates(Profile* profile);
104
105  // Initializes extensions for |profile| and creates an entry in
106  // |profile_extensions_| for it.
107  void SetupProfile(Profile* profile);
108
109  // Returns an extension for the given name in |profile|.  type and
110  // index.  Two extensions with the name but different profiles will
111  // have the same id.
112  scoped_refptr<extensions::Extension> GetExtension(
113      Profile* profile, const std::string& name,
114      extensions::Manifest::Type type) WARN_UNUSED_RESULT;
115
116  ProfileExtensionNameMap profile_extensions_;
117  StringMap id_to_name_;
118  TypeMap id_to_type_;
119  bool setup_completed_;
120
121  DISALLOW_COPY_AND_ASSIGN(SyncExtensionHelper);
122};
123
124#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_EXTENSION_HELPER_H_
125