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_
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 "net/url_request/url_fetcher.h"
19 namespace enhanced_bookmarks
{
21 // Manages requests to the bookmark server to retrieve the current clustering
22 // state for the bookmarks. A cluster is simply a named set of bookmarks related
24 class BookmarkServerClusterService
: public KeyedService
,
25 public BookmarkServerService
,
26 public SigninManagerBase::Observer
{
28 // Maps a cluster name to the stars.id of the bookmarks.
29 typedef std::map
<std::string
, std::vector
<std::string
>> ClusterMap
;
30 // |application_language_code| should be a ISO 639-1 compliant string. Aka
31 // 'en' or 'en-US'. Note that this code should only specify the language, not
32 // the locale, so 'en_US' (english language with US locale) and 'en-GB_US'
33 // (British english person in the US) are not language code.
34 BookmarkServerClusterService(
35 const std::string
& application_language_code
,
36 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
37 ProfileOAuth2TokenService
* token_service
,
38 SigninManagerBase
* signin_manager
,
39 EnhancedBookmarkModel
* enhanced_bookmark_model
,
40 PrefService
* pref_service
);
41 ~BookmarkServerClusterService() override
;
43 // Retrieves all the bookmarks associated with a cluster. The returned
44 // BookmarkNodes are owned by the bookmark model, and one must listen to the
45 // model observer notification to clear them.
46 const std::vector
<const BookmarkNode
*> BookmarksForClusterNamed(
47 const std::string
& cluster_name
) const;
49 // Returns the clusters in which the passed bookmark is in, if any.
50 const std::vector
<std::string
> ClustersForBookmark(
51 const BookmarkNode
* bookmark
) const;
53 // Dynamically generates a vector of all clusters names.
54 const std::vector
<std::string
> GetClusters() const;
56 // Registers server cluster service prefs.
57 static void RegisterPrefs(user_prefs::PrefRegistrySyncable
* registry
);
60 // BookmarkServerService methods.
61 scoped_ptr
<net::URLFetcher
> CreateFetcher() override
;
62 bool ProcessResponse(const std::string
& response
,
63 bool* should_notify
) override
;
64 void CleanAfterFailure() override
;
66 // EnhancedBookmarkModelObserver methods.
67 void EnhancedBookmarkModelLoaded() override
;
68 void EnhancedBookmarkAdded(const BookmarkNode
* node
) override
;
69 void EnhancedBookmarkRemoved(const BookmarkNode
* node
) override
;
70 void EnhancedBookmarkNodeChanged(const BookmarkNode
* node
) override
;
71 void EnhancedBookmarkAllUserNodesRemoved() override
;
72 void EnhancedBookmarkRemoteIdChanged(const BookmarkNode
* node
,
73 const std::string
& old_remote_id
,
74 const std::string
& remote_id
) override
;
77 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, Cluster
);
78 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, SignOut
);
79 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, Serialization
);
80 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, SaveToPrefs
);
81 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, BadAuth
);
82 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, EmptyAuth
);
83 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
,
84 ClearClusterMapOnRemoveAllBookmarks
);
86 // Overriden from SigninManagerBase::Observer.
87 void GoogleSignedOut(const std::string
& account_id
,
88 const std::string
& username
) override
;
90 // Updates |cluster_data_| with the |cluster_map| and saves the result to
91 // profile prefs. All changes to |cluster_data_| should go through this method
92 // to ensure profile prefs is always up to date.
93 // TODO(noyau): This is probably a misuse of profile prefs. While the expected
94 // amount of data is small (<1kb), it can theoretically reach megabytes in
96 void SwapModel(ClusterMap
* cluster_map
);
97 // Updates |cluster_data_| from profile prefs.
100 // Serialize the |cluster_map| into the returned dictionary value.. The
101 // |auth_id| uniquely identify the signed in user, to avoid deserializing data
102 // for a different one.
103 static scoped_ptr
<base::DictionaryValue
> Serialize(
104 const ClusterMap
& cluster_map
,
105 const std::string
& auth_id
);
106 // Returns true on success.
107 // The result is swapped into |out_map|.
108 // |auth_id| must match the serialized auth_id for this method to succeed.
109 static bool Deserialize(const base::DictionaryValue
& value
,
110 const std::string
& auth_id
,
111 ClusterMap
* out_map
);
113 // The ISO 639-1 code of the language used by the application.
114 const std::string application_language_code_
;
115 // The preferences services associated with the relevant profile.
116 PrefService
* pref_service_
;
117 // The cluster data, a map from cluster name to a vector of stars.id.
118 ClusterMap cluster_data_
;
120 DISALLOW_COPY_AND_ASSIGN(BookmarkServerClusterService
);
123 } // namespace enhanced_bookmarks
125 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_