sync_extension_helper.h revision 1e9bf3e0803691d0a228da41fc608347b6db4340
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  // Returns true if the extension with the given name is enabled on |profile|.
48  bool IsExtensionEnabled(Profile* profile, const std::string& name) const;
49
50  // Enables the extension with the given name to run in incognito mode
51  void IncognitoEnableExtension(Profile* profile, const std::string& name);
52
53  // Disables the extension with the given name from running in incognito mode
54  void IncognitoDisableExtension(Profile* profile, const std::string& name);
55
56  // Returns true iff the extension is enabled in incognito mode on |profile|.
57  bool IsIncognitoEnabled(Profile* profile, const std::string& name) const;
58
59  // Returns true iff the extension with the given id is pending
60  // install in |profile|.
61  bool IsExtensionPendingInstallForSync(
62      Profile* profile, const std::string& id) const;
63
64  // Installs all extensions pending sync in |profile| of the given
65  // type.
66  void InstallExtensionsPendingForSync(Profile* profile);
67
68  // Returns true iff |profile1| and |profile2| have the same extensions and
69  // they are all in the same state.
70  static bool ExtensionStatesMatch(Profile* profile1, Profile* profile2);
71
72 private:
73  struct ExtensionState {
74    enum EnabledState { DISABLED, PENDING, ENABLED };
75
76    ExtensionState();
77    ~ExtensionState();
78    bool Equals(const ExtensionState &other) const;
79
80    EnabledState enabled_state;
81    bool incognito_enabled;
82  };
83
84  typedef std::map<std::string, ExtensionState> ExtensionStateMap;
85  typedef std::map<std::string, scoped_refptr<extensions::Extension> >
86      ExtensionNameMap;
87  typedef std::map<Profile*, ExtensionNameMap> ProfileExtensionNameMap;
88  typedef std::map<std::string, std::string> StringMap;
89  typedef std::map<std::string, extensions::Manifest::Type> TypeMap;
90
91  friend struct DefaultSingletonTraits<SyncExtensionHelper>;
92
93  SyncExtensionHelper();
94  ~SyncExtensionHelper();
95
96  // Returns a map from |profile|'s installed extensions to their state.
97  static ExtensionStateMap GetExtensionStates(Profile* profile);
98
99  // Initializes extensions for |profile| and creates an entry in
100  // |profile_extensions_| for it.
101  void SetupProfile(Profile* profile);
102
103  // Returns an extension for the given name in |profile|.  type and
104  // index.  Two extensions with the name but different profiles will
105  // have the same id.
106  scoped_refptr<extensions::Extension> GetExtension(
107      Profile* profile, const std::string& name,
108      extensions::Manifest::Type type) WARN_UNUSED_RESULT;
109
110  ProfileExtensionNameMap profile_extensions_;
111  StringMap id_to_name_;
112  TypeMap id_to_type_;
113  bool setup_completed_;
114
115  DISALLOW_COPY_AND_ASSIGN(SyncExtensionHelper);
116};
117
118#endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_SYNC_EXTENSION_HELPER_H_
119