Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / sync / test / integration / sync_app_helper.cc
blobb4d3fcbbdf057a9b774df54d445c98349b8ec626
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.
5 #include "chrome/browser/sync/test/integration/sync_app_helper.h"
7 #include "chrome/browser/extensions/extension_service.h"
8 #include "chrome/browser/extensions/launch_util.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/sync/test/integration/extensions_helper.h"
11 #include "chrome/browser/sync/test/integration/sync_datatype_helper.h"
12 #include "chrome/browser/sync/test/integration/sync_extension_helper.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "chrome/common/extensions/manifest_handlers/app_launch_info.h"
15 #include "chrome/common/extensions/sync_helper.h"
16 #include "components/crx_file/id_util.h"
17 #include "extensions/browser/app_sorting.h"
18 #include "extensions/browser/extension_prefs.h"
19 #include "extensions/browser/extension_registry.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/extension_set.h"
23 using extensions::ExtensionPrefs;
25 namespace {
27 struct AppState {
28 AppState();
29 ~AppState();
30 bool IsValid() const;
31 bool Equals(const AppState& other) const;
33 syncer::StringOrdinal app_launch_ordinal;
34 syncer::StringOrdinal page_ordinal;
35 extensions::LaunchType launch_type;
36 GURL launch_web_url;
37 std::string description;
38 std::string name;
39 bool from_bookmark;
42 typedef std::map<std::string, AppState> AppStateMap;
44 AppState::AppState() : launch_type(extensions::LAUNCH_TYPE_INVALID) {}
46 AppState::~AppState() {}
48 bool AppState::IsValid() const {
49 return page_ordinal.IsValid() && app_launch_ordinal.IsValid();
52 bool AppState::Equals(const AppState& other) const {
53 return app_launch_ordinal.Equals(other.app_launch_ordinal) &&
54 page_ordinal.Equals(other.page_ordinal) &&
55 launch_type == other.launch_type &&
56 launch_web_url == other.launch_web_url &&
57 description == other.description && name == other.name &&
58 from_bookmark == other.from_bookmark;
61 // Load all the app specific values for |id| into |app_state|.
62 void LoadApp(content::BrowserContext* context,
63 const std::string& id,
64 AppState* app_state) {
65 ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
66 app_state->app_launch_ordinal = prefs->app_sorting()->GetAppLaunchOrdinal(id);
67 app_state->page_ordinal = prefs->app_sorting()->GetPageOrdinal(id);
68 app_state->launch_type = extensions::GetLaunchTypePrefValue(prefs, id);
69 ExtensionService* service =
70 extensions::ExtensionSystem::Get(context)->extension_service();
71 const extensions::Extension* extension = service->GetInstalledExtension(id);
72 // GetInstalledExtension(id) returns null if |id| is for a pending extension.
73 // In case of running tests against real backend servers, pending apps won't
74 // be installed.
75 if (extension) {
76 app_state->launch_web_url =
77 extensions::AppLaunchInfo::GetLaunchWebURL(extension);
78 app_state->description = extension->description();
79 app_state->name = extension->name();
80 app_state->from_bookmark = extension->from_bookmark();
84 // Returns a map from |profile|'s installed extensions to their state.
85 AppStateMap GetAppStates(Profile* profile) {
86 AppStateMap app_state_map;
88 scoped_ptr<const extensions::ExtensionSet> extensions(
89 extensions::ExtensionRegistry::Get(profile)
90 ->GenerateInstalledExtensionsSet());
91 for (const auto& extension : *extensions) {
92 if (extension->is_app() &&
93 extensions::sync_helper::IsSyncable(extension.get())) {
94 const std::string& id = extension->id();
95 LoadApp(profile, id, &(app_state_map[id]));
99 const extensions::PendingExtensionManager* pending_extension_manager =
100 extensions::ExtensionSystem::Get(profile)
101 ->extension_service()
102 ->pending_extension_manager();
104 std::list<std::string> pending_crx_ids;
105 pending_extension_manager->GetPendingIdsForUpdateCheck(&pending_crx_ids);
107 for (std::list<std::string>::const_iterator id = pending_crx_ids.begin();
108 id != pending_crx_ids.end(); ++id) {
109 LoadApp(profile, *id, &(app_state_map[*id]));
112 return app_state_map;
115 } // namespace
117 SyncAppHelper* SyncAppHelper::GetInstance() {
118 SyncAppHelper* instance = Singleton<SyncAppHelper>::get();
119 instance->SetupIfNecessary(sync_datatype_helper::test());
120 return instance;
123 void SyncAppHelper::SetupIfNecessary(SyncTest* test) {
124 if (setup_completed_)
125 return;
127 for (int i = 0; i < test->num_clients(); ++i) {
128 extensions::ExtensionSystem::Get(
129 test->GetProfile(i))->InitForRegularProfile(true);
131 if (test->use_verifier()) {
132 extensions::ExtensionSystem::Get(test->verifier())
133 ->InitForRegularProfile(true);
136 setup_completed_ = true;
139 bool SyncAppHelper::AppStatesMatch(Profile* profile1, Profile* profile2) {
140 if (!SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
141 profile1, profile2))
142 return false;
144 const AppStateMap& state_map1 = GetAppStates(profile1);
145 const AppStateMap& state_map2 = GetAppStates(profile2);
146 if (state_map1.size() != state_map2.size()) {
147 DVLOG(2) << "Number of Apps for profile " << profile1->GetDebugName()
148 << " does not match profile " << profile2->GetDebugName();
149 return false;
152 AppStateMap::const_iterator it1 = state_map1.begin();
153 AppStateMap::const_iterator it2 = state_map2.begin();
154 while (it1 != state_map1.end()) {
155 if (it1->first != it2->first) {
156 DVLOG(2) << "Apps for profile " << profile1->GetDebugName()
157 << " do not match profile " << profile2->GetDebugName();
158 return false;
159 } else if (!it1->second.IsValid()) {
160 DVLOG(2) << "Apps for profile " << profile1->GetDebugName()
161 << " are not valid.";
162 return false;
163 } else if (!it2->second.IsValid()) {
164 DVLOG(2) << "Apps for profile " << profile2->GetDebugName()
165 << " are not valid.";
166 return false;
167 } else if (!sync_datatype_helper::test()->UsingExternalServers() &&
168 !it1->second.Equals(it2->second)) {
169 // If this test is run against real backend servers then we do not expect
170 // to install pending apps. So, we don't check equality of AppStates of
171 // each app per profile.
172 DVLOG(2) << "App states for profile " << profile1->GetDebugName()
173 << " do not match profile " << profile2->GetDebugName();
174 return false;
176 ++it1;
177 ++it2;
180 return true;
183 syncer::StringOrdinal SyncAppHelper::GetPageOrdinalForApp(
184 Profile* profile,
185 const std::string& name) {
186 return ExtensionPrefs::Get(profile)->app_sorting()->GetPageOrdinal(
187 crx_file::id_util::GenerateId(name));
190 void SyncAppHelper::SetPageOrdinalForApp(
191 Profile* profile,
192 const std::string& name,
193 const syncer::StringOrdinal& page_ordinal) {
194 ExtensionPrefs::Get(profile)->app_sorting()->SetPageOrdinal(
195 crx_file::id_util::GenerateId(name), page_ordinal);
198 syncer::StringOrdinal SyncAppHelper::GetAppLaunchOrdinalForApp(
199 Profile* profile,
200 const std::string& name) {
201 return ExtensionPrefs::Get(profile)->app_sorting()->GetAppLaunchOrdinal(
202 crx_file::id_util::GenerateId(name));
205 void SyncAppHelper::SetAppLaunchOrdinalForApp(
206 Profile* profile,
207 const std::string& name,
208 const syncer::StringOrdinal& app_launch_ordinal) {
209 ExtensionPrefs::Get(profile)->app_sorting()->SetAppLaunchOrdinal(
210 crx_file::id_util::GenerateId(name), app_launch_ordinal);
213 void SyncAppHelper::FixNTPOrdinalCollisions(Profile* profile) {
214 ExtensionPrefs::Get(profile)->app_sorting()->FixNTPOrdinalCollisions();
217 SyncAppHelper::SyncAppHelper() : setup_completed_(false) {}
219 SyncAppHelper::~SyncAppHelper() {}