summaryrefslogtreecommitdiffstats
path: root/components/enhanced_bookmarks/bookmark_server_cluster_service.h
diff options
context:
space:
mode:
authornoyau <noyau@chromium.org>2014-10-08 04:11:11 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-08 11:11:35 +0000
commitdaaac3af72f231dec2df4dc566b84356d3dc10df (patch)
tree00c29c35ec84a81e461e3a1f53a8ca37fc421515 /components/enhanced_bookmarks/bookmark_server_cluster_service.h
parent7e6d411816fc12913f0a7ae6c32bc38e73f721e8 (diff)
downloadchromium_src-daaac3af72f231dec2df4dc566b84356d3dc10df.zip
chromium_src-daaac3af72f231dec2df4dc566b84356d3dc10df.tar.gz
chromium_src-daaac3af72f231dec2df4dc566b84356d3dc10df.tar.bz2
Bring up of the enhanced bookmarks cluster service.
This service retrieves the cluster of bookmarks from the bookmark server. BUG=None Committed: https://crrev.com/6bb374fc58bfeeb7966b6a548b1c7b41af9c8e47 Cr-Commit-Position: refs/heads/master@{#298582} Review URL: https://codereview.chromium.org/539173004 Cr-Commit-Position: refs/heads/master@{#298682}
Diffstat (limited to 'components/enhanced_bookmarks/bookmark_server_cluster_service.h')
-rw-r--r--components/enhanced_bookmarks/bookmark_server_cluster_service.h125
1 files changed, 125 insertions, 0 deletions
diff --git a/components/enhanced_bookmarks/bookmark_server_cluster_service.h b/components/enhanced_bookmarks/bookmark_server_cluster_service.h
new file mode 100644
index 0000000..ae7cc08
--- /dev/null
+++ b/components/enhanced_bookmarks/bookmark_server_cluster_service.h
@@ -0,0 +1,125 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_
+#define COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_
+
+#include <string>
+#include <vector>
+
+#include "base/compiler_specific.h"
+#include "components/enhanced_bookmarks/bookmark_server_service.h"
+#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
+#include "components/signin/core/browser/signin_manager_base.h"
+#include "net/url_request/url_fetcher.h"
+
+class PrefService;
+
+namespace enhanced_bookmarks {
+
+// Manages requests to the bookmark server to retrieve the current clustering
+// state for the bookmarks. A cluster is simply a named set of bookmarks related
+// to each others.
+class BookmarkServerClusterService : public KeyedService,
+ public BookmarkServerService,
+ public SigninManagerBase::Observer {
+ public:
+ // Maps a cluster name to the stars.id of the bookmarks.
+ typedef std::map<std::string, std::vector<std::string>> ClusterMap;
+ // |application_language_code| should be a ISO 639-1 compliant string. Aka
+ // 'en' or 'en-US'. Note that this code should only specify the language, not
+ // the locale, so 'en_US' (english language with US locale) and 'en-GB_US'
+ // (British english person in the US) are not language code.
+ BookmarkServerClusterService(
+ const std::string& application_language_code,
+ scoped_refptr<net::URLRequestContextGetter> request_context_getter,
+ ProfileOAuth2TokenService* token_service,
+ SigninManagerBase* signin_manager,
+ EnhancedBookmarkModel* enhanced_bookmark_model,
+ PrefService* pref_service);
+ virtual ~BookmarkServerClusterService();
+
+ // Retrieves all the bookmarks associated with a cluster. The returned
+ // BookmarkNodes are owned by the bookmark model, and one must listen to the
+ // model observer notification to clear them.
+ const std::vector<const BookmarkNode*> BookmarksForClusterNamed(
+ const std::string& cluster_name) const;
+
+ // Returns the clusters in which the passed bookmark is in, if any.
+ const std::vector<std::string> ClustersForBookmark(
+ const BookmarkNode* bookmark) const;
+
+ // Dynamically generates a vector of all clusters names.
+ const std::vector<std::string> GetClusters() const;
+
+ // Registers server cluster service prefs.
+ static void RegisterPrefs(user_prefs::PrefRegistrySyncable* registry);
+
+ protected:
+ // BookmarkServerService methods.
+ virtual scoped_ptr<net::URLFetcher> CreateFetcher() override;
+ virtual bool ProcessResponse(const std::string& response,
+ bool* should_notify) override;
+ virtual void CleanAfterFailure() override;
+
+ // EnhancedBookmarkModelObserver methods.
+ virtual void EnhancedBookmarkModelLoaded() override;
+ virtual void EnhancedBookmarkAdded(const BookmarkNode* node) override;
+ virtual void EnhancedBookmarkRemoved(const BookmarkNode* node) override;
+ virtual void EnhancedBookmarkAllUserNodesRemoved() override;
+ virtual void EnhancedBookmarkRemoteIdChanged(
+ const BookmarkNode* node,
+ const std::string& old_remote_id,
+ const std::string& remote_id) override;
+
+ private:
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Cluster);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, SignOut);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, Serialization);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, SaveToPrefs);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, BadAuth);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest, EmptyAuth);
+ FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest,
+ ClearClusterMapOnRemoveAllBookmarks);
+
+ // Overriden from SigninManagerBase::Observer.
+ virtual void GoogleSignedOut(const std::string& account_id,
+ const std::string& username) override;
+
+ // Updates |cluster_data_| with the |cluster_map| and saves the result to
+ // profile prefs. All changes to |cluster_data_| should go through this method
+ // to ensure profile prefs is always up to date.
+ // TODO(noyau): This is probably a misuse of profile prefs. While the expected
+ // amount of data is small (<1kb), it can theoretically reach megabytes in
+ // size.
+ void SwapModel(ClusterMap* cluster_map);
+ // Updates |cluster_data_| from profile prefs.
+ void LoadModel();
+
+ // Serialize the |cluster_map| into the returned dictionary value.. The
+ // |auth_id| uniquely identify the signed in user, to avoid deserializing data
+ // for a different one.
+ static scoped_ptr<base::DictionaryValue> Serialize(
+ const ClusterMap& cluster_map,
+ const std::string& auth_id);
+ // Returns true on success.
+ // The result is swapped into |out_map|.
+ // |auth_id| must match the serialized auth_id for this method to succeed.
+ static bool Deserialize(const base::DictionaryValue& value,
+ const std::string& auth_id,
+ ClusterMap* out_map);
+
+ // The ISO 639-1 code of the language used by the application.
+ const std::string application_language_code_;
+ // The preferences services associated with the relevant profile.
+ PrefService* pref_service_;
+ // The cluster data, a map from cluster name to a vector of stars.id.
+ ClusterMap cluster_data_;
+
+ DISALLOW_COPY_AND_ASSIGN(BookmarkServerClusterService);
+};
+
+} // namespace enhanced_bookmarks
+
+#endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_