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_EXTENSIONS_HELPER_H_
6 #define CHROME_BROWSER_SYNC_TEST_INTEGRATION_EXTENSIONS_HELPER_H_
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include "base/compiler_specific.h"
13 #include "chrome/browser/sync/test/integration/status_change_checker.h"
14 #include "chrome/browser/sync/test/integration/sync_test.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "extensions/browser/extension_registry_observer.h"
18 #include "extensions/common/extension.h"
19 
20 class Profile;
21 class SyncedExtensionInstaller;
22 
23 namespace extensions_helper {
24 
25 // Returns true iff profiles with indices |index1| and |index2| have the same
26 // extensions.
27 bool HasSameExtensions(int index1, int index2) WARN_UNUSED_RESULT;
28 
29 // Returns true iff the profile with index |index| has the same extensions
30 // as the verifier.
31 bool HasSameExtensionsAsVerifier(int index) WARN_UNUSED_RESULT;
32 
33 // Returns true iff all existing profiles have the same extensions
34 // as the verifier.
35 bool AllProfilesHaveSameExtensionsAsVerifier() WARN_UNUSED_RESULT;
36 
37 // Returns true iff all existing profiles have the same extensions.
38 bool AllProfilesHaveSameExtensions() WARN_UNUSED_RESULT;
39 
40 // Installs the extension for the given index to |profile|, and returns the
41 // extension ID of the new extension.
42 std::string InstallExtension(Profile* profile, int index);
43 
44 // Installs the extension for the given index to all profiles (including the
45 // verifier), and returns the extension ID of the new extension.
46 std::string InstallExtensionForAllProfiles(int index);
47 
48 // Uninstalls the extension for the given index from |profile|. Assumes that
49 // it was previously installed.
50 void UninstallExtension(Profile* profile, int index);
51 
52 // Returns a vector containing the indices of all currently installed
53 // test extensions on |profile|.
54 std::vector<int> GetInstalledExtensions(Profile* profile);
55 
56 // Installs all pending synced extensions for |profile|.
57 void InstallExtensionsPendingForSync(Profile* profile);
58 
59 // Enables the extension for the given index on |profile|.
60 void EnableExtension(Profile* profile, int index);
61 
62 // Disables the extension for the given index on |profile|.
63 void DisableExtension(Profile* profile, int index);
64 
65 // Returns true if the extension with index |index| is enabled on |profile|.
66 bool IsExtensionEnabled(Profile* profile, int index);
67 
68 // Enables the extension for the given index in incognito mode on |profile|.
69 void IncognitoEnableExtension(Profile* profile, int index);
70 
71 // Disables the extension for the given index in incognito mode on |profile|.
72 void IncognitoDisableExtension(Profile* profile, int index);
73 
74 // Returns true if the extension with index |index| is enabled in incognito
75 // mode on |profile|.
76 bool IsIncognitoEnabled(Profile* profile, int index);
77 
78 // Runs the message loop until all profiles have same extensions. Returns false
79 // on timeout.
80 bool AwaitAllProfilesHaveSameExtensions();
81 
82 }  // namespace extensions_helper
83 
84 // A helper class to implement waiting for a set of profiles to have matching
85 // extensions lists. It waits for calls on both interfaces:
86 // ExtensionRegistryObserver and NotificationObserver. Observing
87 // NOTIFICATION_EXTENSION_UPDATING_STARTED notification is needed for tests
88 // against local server because in such tests extensions are not installed and
89 // ExtensionRegistryObserver methods are not called.
90 class ExtensionsMatchChecker : public StatusChangeChecker,
91                                public extensions::ExtensionRegistryObserver,
92                                public content::NotificationObserver {
93  public:
94   ExtensionsMatchChecker();
95   ~ExtensionsMatchChecker() override;
96 
97   // StatusChangeChecker implementation.
98   bool IsExitConditionSatisfied(std::ostream* os) override;
99 
100   // extensions::ExtensionRegistryObserver implementation.
101   void OnExtensionLoaded(content::BrowserContext* context,
102                          const extensions::Extension* extension) override;
103   void OnExtensionUnloaded(content::BrowserContext* context,
104                            const extensions::Extension* extension,
105                            extensions::UnloadedExtensionReason reason) override;
106   void OnExtensionInstalled(content::BrowserContext* browser_context,
107                             const extensions::Extension* extension,
108                             bool is_update) override;
109   void OnExtensionUninstalled(content::BrowserContext* browser_context,
110                               const extensions::Extension* extension,
111                               extensions::UninstallReason reason) override;
112 
113   // content::NotificationObserver implementation.
114   void Observe(int type,
115                const content::NotificationSource& source,
116                const content::NotificationDetails& details) override;
117 
118  private:
119   std::vector<Profile*> profiles_;
120   std::vector<std::unique_ptr<SyncedExtensionInstaller>>
121       synced_extension_installers_;
122   content::NotificationRegistrar registrar_;
123 
124   DISALLOW_COPY_AND_ASSIGN(ExtensionsMatchChecker);
125 };
126 
127 #endif  // CHROME_BROWSER_SYNC_TEST_INTEGRATION_EXTENSIONS_HELPER_H_
128