cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / components / enhanced_bookmarks / bookmark_server_cluster_service.h
blob8bbf5ed97689006c04a5f4aa14af44a6f08eaa24
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 // Overriden from SigninManagerBase::Observer.
92 void GoogleSignedOut(const std::string& account_id,
93 const std::string& username) override;
95 // Updates |cluster_data_| with the |cluster_map| and saves the result to
96 // profile prefs. All changes to |cluster_data_| should go through this method
97 // to ensure profile prefs is always up to date.
98 // TODO(noyau): This is probably a misuse of profile prefs. While the expected
99 // amount of data is small (<1kb), it can theoretically reach megabytes in
100 // size.
101 void SwapModel(ClusterMap* cluster_map);
102 // Updates |cluster_data_| from profile prefs.
103 void LoadModel();
105 // sync_driver::SyncServiceObserver methods.
106 void OnStateChanged() override;
107 void OnSyncCycleCompleted() override;
109 // This sets an internal flag to fetch new clusters.
110 void InvalidateCache();
112 // Serialize the |cluster_map| into the returned dictionary value.. The
113 // |auth_id| uniquely identify the signed in user, to avoid deserializing data
114 // for a different one.
115 static scoped_ptr<base::DictionaryValue> Serialize(
116 const ClusterMap& cluster_map,
117 const std::string& auth_id);
118 // Returns true on success.
119 // The result is swapped into |out_map|.
120 // |auth_id| must match the serialized auth_id for this method to succeed.
121 static bool Deserialize(const base::DictionaryValue& value,
122 const std::string& auth_id,
123 ClusterMap* out_map);
125 // The ISO 639-1 code of the language used by the application.
126 const std::string application_language_code_;
127 // This class observes the sync service for changes.
128 sync_driver::SyncService* sync_service_;
129 // The preferences services associated with the relevant profile.
130 PrefService* pref_service_;
131 // The cluster data, a map from cluster name to a vector of stars.id.
132 ClusterMap cluster_data_;
133 bool sync_refresh_skipped_;
134 // This holds the number of cluster refreshes needed.
135 int refreshes_needed_;
137 DISALLOW_COPY_AND_ASSIGN(BookmarkServerClusterService);
140 } // namespace enhanced_bookmarks
142 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_