diff options
author | noyau <noyau@chromium.org> | 2014-10-07 15:00:43 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-10-07 22:01:00 +0000 |
commit | 6bb374fc58bfeeb7966b6a548b1c7b41af9c8e47 (patch) | |
tree | 7b9d87b6c35ea4350be96c62016e5c2a77fd5d0b /components/enhanced_bookmarks/bookmark_server_cluster_service.h | |
parent | da97e832e41826d3ce0e6bd5c495feb636345ece (diff) | |
download | chromium_src-6bb374fc58bfeeb7966b6a548b1c7b41af9c8e47.zip chromium_src-6bb374fc58bfeeb7966b6a548b1c7b41af9c8e47.tar.gz chromium_src-6bb374fc58bfeeb7966b6a548b1c7b41af9c8e47.tar.bz2 |
Bring up of the enhanced bookmarks cluster service.
This service retrieves the cluster of bookmarks from the bookmark
server.
BUG=None
Review URL: https://codereview.chromium.org/539173004
Cr-Commit-Position: refs/heads/master@{#298582}
Diffstat (limited to 'components/enhanced_bookmarks/bookmark_server_cluster_service.h')
-rw-r--r-- | components/enhanced_bookmarks/bookmark_server_cluster_service.h | 125 |
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_ |