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 bookmarks::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 bookmarks::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 bookmarks::BookmarkNode
* node
) override
;
69 void EnhancedBookmarkRemoved(const bookmarks::BookmarkNode
* node
) override
;
70 void EnhancedBookmarkNodeChanged(
71 const bookmarks::BookmarkNode
* node
) override
;
72 void EnhancedBookmarkAllUserNodesRemoved() override
;
73 void EnhancedBookmarkRemoteIdChanged(const bookmarks::BookmarkNode
* node
,
74 const std::string
& old_remote_id
,
75 const std::string
& remote_id
) override
;
78 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, Cluster
);
79 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, SignOut
);
80 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, Serialization
);
81 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, SaveToPrefs
);
82 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, BadAuth
);
83 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
, EmptyAuth
);
84 FRIEND_TEST_ALL_PREFIXES(BookmarkServerServiceTest
,
85 ClearClusterMapOnRemoveAllBookmarks
);
87 // Overriden from SigninManagerBase::Observer.
88 void GoogleSignedOut(const std::string
& account_id
,
89 const std::string
& username
) override
;
91 // Updates |cluster_data_| with the |cluster_map| and saves the result to
92 // profile prefs. All changes to |cluster_data_| should go through this method
93 // to ensure profile prefs is always up to date.
94 // TODO(noyau): This is probably a misuse of profile prefs. While the expected
95 // amount of data is small (<1kb), it can theoretically reach megabytes in
97 void SwapModel(ClusterMap
* cluster_map
);
98 // Updates |cluster_data_| from profile prefs.
101 // Serialize the |cluster_map| into the returned dictionary value.. The
102 // |auth_id| uniquely identify the signed in user, to avoid deserializing data
103 // for a different one.
104 static scoped_ptr
<base::DictionaryValue
> Serialize(
105 const ClusterMap
& cluster_map
,
106 const std::string
& auth_id
);
107 // Returns true on success.
108 // The result is swapped into |out_map|.
109 // |auth_id| must match the serialized auth_id for this method to succeed.
110 static bool Deserialize(const base::DictionaryValue
& value
,
111 const std::string
& auth_id
,
112 ClusterMap
* out_map
);
114 // The ISO 639-1 code of the language used by the application.
115 const std::string application_language_code_
;
116 // The preferences services associated with the relevant profile.
117 PrefService
* pref_service_
;
118 // The cluster data, a map from cluster name to a vector of stars.id.
119 ClusterMap cluster_data_
;
121 DISALLOW_COPY_AND_ASSIGN(BookmarkServerClusterService
);
124 } // namespace enhanced_bookmarks
126 #endif // COMPONENTS_ENHANCED_BOOKMARKS_BOOKMARK_SERVER_CLUSTER_SERVICE_H_