sync_app_helper.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
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#include "chrome/browser/sync/test/integration/sync_app_helper.h"
6
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/sync_helper.h"
15#include "extensions/browser/app_sorting.h"
16#include "extensions/browser/extension_prefs.h"
17#include "extensions/browser/extension_registry.h"
18#include "extensions/browser/extension_system.h"
19#include "extensions/common/extension.h"
20#include "extensions/common/extension_set.h"
21#include "extensions/common/id_util.h"
22
23using extensions::ExtensionPrefs;
24
25namespace {
26
27struct AppState {
28  AppState();
29  ~AppState();
30  bool IsValid() const;
31  bool Equals(const AppState& other) const;
32
33  syncer::StringOrdinal app_launch_ordinal;
34  syncer::StringOrdinal page_ordinal;
35  extensions::LaunchType launch_type;
36};
37
38typedef std::map<std::string, AppState> AppStateMap;
39
40AppState::AppState() : launch_type(extensions::LAUNCH_TYPE_INVALID) {}
41
42AppState::~AppState() {}
43
44bool AppState::IsValid() const {
45  return page_ordinal.IsValid() && app_launch_ordinal.IsValid();
46}
47
48bool AppState::Equals(const AppState& other) const {
49  return app_launch_ordinal.Equals(other.app_launch_ordinal) &&
50      page_ordinal.Equals(other.page_ordinal) &&
51      launch_type == other.launch_type;
52}
53
54// Load all the app specific values for |id| into |app_state|.
55void LoadApp(content::BrowserContext* context,
56             const std::string& id,
57             AppState* app_state) {
58  ExtensionPrefs* prefs = ExtensionPrefs::Get(context);
59  app_state->app_launch_ordinal = prefs->app_sorting()->GetAppLaunchOrdinal(id);
60  app_state->page_ordinal = prefs->app_sorting()->GetPageOrdinal(id);
61  app_state->launch_type = extensions::GetLaunchTypePrefValue(prefs, id);
62}
63
64// Returns a map from |profile|'s installed extensions to their state.
65AppStateMap GetAppStates(Profile* profile) {
66  AppStateMap app_state_map;
67
68  scoped_ptr<const extensions::ExtensionSet> extensions(
69      extensions::ExtensionRegistry::Get(profile)
70          ->GenerateInstalledExtensionsSet());
71  for (extensions::ExtensionSet::const_iterator it = extensions->begin();
72       it != extensions->end(); ++it) {
73    if (extensions::sync_helper::IsSyncableApp(it->get())) {
74      const std::string& id = (*it)->id();
75      LoadApp(profile, id, &(app_state_map[id]));
76    }
77  }
78
79  const extensions::PendingExtensionManager* pending_extension_manager =
80      extensions::ExtensionSystem::Get(profile)
81          ->extension_service()
82          ->pending_extension_manager();
83
84  std::list<std::string> pending_crx_ids;
85  pending_extension_manager->GetPendingIdsForUpdateCheck(&pending_crx_ids);
86
87  for (std::list<std::string>::const_iterator id = pending_crx_ids.begin();
88       id != pending_crx_ids.end(); ++id) {
89    LoadApp(profile, *id, &(app_state_map[*id]));
90  }
91
92  return app_state_map;
93}
94
95}  // namespace
96
97SyncAppHelper* SyncAppHelper::GetInstance() {
98  SyncAppHelper* instance = Singleton<SyncAppHelper>::get();
99  instance->SetupIfNecessary(sync_datatype_helper::test());
100  return instance;
101}
102
103void SyncAppHelper::SetupIfNecessary(SyncTest* test) {
104  if (setup_completed_)
105    return;
106
107  for (int i = 0; i < test->num_clients(); ++i) {
108    extensions::ExtensionSystem::Get(
109        test->GetProfile(i))->InitForRegularProfile(true);
110  }
111  extensions::ExtensionSystem::Get(
112      test->verifier())->InitForRegularProfile(true);
113
114  setup_completed_ = true;
115}
116
117bool SyncAppHelper::AppStatesMatch(Profile* profile1, Profile* profile2) {
118  if (!SyncExtensionHelper::GetInstance()->ExtensionStatesMatch(
119          profile1, profile2))
120    return false;
121
122  const AppStateMap& state_map1 = GetAppStates(profile1);
123  const AppStateMap& state_map2 = GetAppStates(profile2);
124  if (state_map1.size() != state_map2.size()) {
125    DVLOG(2) << "Number of Apps for profile " << profile1->GetDebugName()
126             << " does not match profile " << profile2->GetDebugName();
127    return false;
128  }
129
130  AppStateMap::const_iterator it1 = state_map1.begin();
131  AppStateMap::const_iterator it2 = state_map2.begin();
132  while (it1 != state_map1.end()) {
133    if (it1->first != it2->first) {
134      DVLOG(2) << "Apps for profile " << profile1->GetDebugName()
135               << " do not match profile " << profile2->GetDebugName();
136      return false;
137    } else if (!it1->second.IsValid()) {
138      DVLOG(2) << "Apps for profile " << profile1->GetDebugName()
139               << " are not valid.";
140      return false;
141    } else if (!it2->second.IsValid()) {
142      DVLOG(2) << "Apps for profile " << profile2->GetDebugName()
143               << " are not valid.";
144      return false;
145    } else if (!it1->second.Equals(it2->second)) {
146      DVLOG(2) << "App states for profile " << profile1->GetDebugName()
147               << " do not match profile " << profile2->GetDebugName();
148      return false;
149    }
150    ++it1;
151    ++it2;
152  }
153
154  return true;
155}
156
157syncer::StringOrdinal SyncAppHelper::GetPageOrdinalForApp(
158    Profile* profile,
159    const std::string& name) {
160  return ExtensionPrefs::Get(profile)->app_sorting()->GetPageOrdinal(
161      extensions::id_util::GenerateId(name));
162}
163
164void SyncAppHelper::SetPageOrdinalForApp(
165    Profile* profile,
166    const std::string& name,
167    const syncer::StringOrdinal& page_ordinal) {
168  ExtensionPrefs::Get(profile)->app_sorting()->SetPageOrdinal(
169      extensions::id_util::GenerateId(name), page_ordinal);
170}
171
172syncer::StringOrdinal SyncAppHelper::GetAppLaunchOrdinalForApp(
173    Profile* profile,
174    const std::string& name) {
175  return ExtensionPrefs::Get(profile)->app_sorting()->GetAppLaunchOrdinal(
176      extensions::id_util::GenerateId(name));
177}
178
179void SyncAppHelper::SetAppLaunchOrdinalForApp(
180    Profile* profile,
181    const std::string& name,
182    const syncer::StringOrdinal& app_launch_ordinal) {
183  ExtensionPrefs::Get(profile)->app_sorting()->SetAppLaunchOrdinal(
184      extensions::id_util::GenerateId(name), app_launch_ordinal);
185}
186
187void SyncAppHelper::FixNTPOrdinalCollisions(Profile* profile) {
188  ExtensionPrefs::Get(profile)->app_sorting()->FixNTPOrdinalCollisions();
189}
190
191SyncAppHelper::SyncAppHelper() : setup_completed_(false) {}
192
193SyncAppHelper::~SyncAppHelper() {}
194