Implement multiple alternative services per origin.
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_cluster_service.h
blob7249c018d8caf0676534863853c54b1742ec0055
1 // Copyright 2014 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 #ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_
6 #define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_
8 #include <string>
9 #include <vector>
11 #include "base/compiler_specific.h"
12 #include "components/enhanced_bookmarks/bookmark_server_service.h"
13 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
14 #include "components/signin/core/browser/signin_manager_base.h"
15 #include "components/sync_driver/sync_service_observer.h"
16 #include "net/url_request/url_fetcher.h"
18 class PrefService;
20 namespace sync_driver {
21 class SyncService;
24 namespace enhanced_bookmarks {
26 // Manages requests to the bookmark server to retrieve the current clustering
27 // state for the bookmarks. A cluster is simply a named set of bookmarks related
28 // to each others. Invalidates its data when a sync operation finishes.
29 class BookmarkServerClusterService : public KeyedService,
30 public BookmarkServerService,
31 public SigninManagerBase::Observer,
32 public sync_driver::SyncServiceObserver {
33 public:
34 // Maps a cluster name to the stars.id of the bookmarks.
35 typedef std::map<std::string, std::vector<std::string>> ClusterMap;
36 // |application_language_code| should be a ISO 639-1 compliant string. Aka
37 // 'en' or 'en-US'. Note that this code should only specify the language, not
38 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US'
39 // (British english person in the US) are not language code.
40 BookmarkServerClusterService(
41 const std::string& application_language_code,
42 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
43 ProfileOAuth2TokenService* token_service,
44 SigninManagerBase* signin_manager,
45 EnhancedBookmarkModel* enhanced_bookmark_model,
46 sync_driver::SyncService* sync_service,
47 PrefService* pref_service);
48 ~BookmarkServerClusterService() override;
50 // KeyedService methods.
51 void Shutdown() override;
53 // Retrieves all the bookmarks associated with a cluster. The returned
54 // BookmarkNodes are owned by the bookmark model, and one must listen to the
55 // model observer notification to clear them.
56 const std::vector<const bookmarks::BookmarkNode*> BookmarksForClusterNamed(
57 const std::string& cluster_name) const;
59 // Returns the clusters in which the passed bookmark is in, if any.
60 const std::vector<std::string> ClustersForBookmark(
61 const bookmarks::BookmarkNode* bookmark) const;
63 // Dynamically generates a vector of all clusters names.
64 const std::vector<std::string> GetClusters() const;
66 // BookmarkServerService methods.
67 void AddObserver(BookmarkServerServiceObserver* observer) override;
69 // Registers server cluster service prefs.
70 static void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry);
72 protected:
73 // BookmarkServerService methods.
74 scoped_ptr<net::URLFetcher> CreateFetcher() override;
75 bool ProcessResponse(const std::string& response,
76 bool* should_notify) override;
77 void CleanAfterFailure() override;
79 // EnhancedBookmarkModelObserver methods.
80 void EnhancedBookmarkModelLoaded() override;
81 void EnhancedBookmarkAdded(const bookmarks::BookmarkNode* node) override;
82 void EnhancedBookmarkRemoved(const bookmarks::BookmarkNode* node) override;
83 void EnhancedBookmarkNodeChanged(
84 const bookmarks::BookmarkNode* node) override;
85 void EnhancedBookmarkAllUserNodesRemoved() override;
86 void EnhancedBookmarkRemoteIdChanged(const bookmarks::BookmarkNode* node,
87 const std::string& old_remote_id,
88 const std::string& remote_id) override;
90 private:
91 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster);
92 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, SignOut);
93 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Serialization);
94 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, SaveToPrefs);
95 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, BadAuth);
96 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, EmptyAuth);
97 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest,
98 ClearClusterMapOnRemoveAllBookmarks);
100 // Overriden from SigninManagerBase::Observer.
101 void GoogleSignedOut(const std::string& account_id,
102 const std::string& username) override;
104 // Updates |cluster_data_| with the |cluster_map| and saves the result to
105 // profile prefs. All changes to |cluster_data_| should go through this method
106 // to ensure profile prefs is always up to date.
107 // TODO(noyau): This is probably a misuse of profile prefs. While the expected
108 // amount of data is small (<1kb), it can theoretically reach megabytes in
109 // size.
110 void SwapModel(ClusterMap* cluster_map);
111 // Updates |cluster_data_| from profile prefs.
112 void LoadModel();
114 // sync_driver::SyncServiceObserver methods.
115 void OnStateChanged() override;
116 void OnSyncCycleCompleted() override;
118 // This sets an internal flag to fetch new clusters.
119 void InvalidateCache();
121 // Serialize the |cluster_map| into the returned dictionary value.. The
122 // |auth_id| uniquely identify the signed in user, to avoid deserializing data
123 // for a different one.
124 static scoped_ptr<base::DictionaryValue> Serialize(
125 const ClusterMap& cluster_map,
126 const std::string& auth_id);
127 // Returns true on success.
128 // The result is swapped into |out_map|.
129 // |auth_id| must match the serialized auth_id for this method to succeed.
130 static bool Deserialize(const base::DictionaryValue& value,
131 const std::string& auth_id,
132 ClusterMap* out_map);
134 // The ISO 639-1 code of the language used by the application.
135 const std::string application_language_code_;
136 // This class observes the sync service for changes.
137 sync_driver::SyncService* sync_service_;
138 // The preferences services associated with the relevant profile.
139 PrefService* pref_service_;
140 // The cluster data, a map from cluster name to a vector of stars.id.
141 ClusterMap cluster_data_;
142 bool sync_refresh_skipped_;
143 // This holds the number of cluster refreshes needed.
144 int refreshes_needed_;
146 DISALLOW_COPY_AND_ASSIGN(BookmarkServerClusterService);
149 } // namespace enhanced_bookmarks
151 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_