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 #include "chrome/browser/enhanced_bookmarks/chrome_bookmark_server_cluster_service.h"
7 #include "chrome/browser/sync/profile_sync_service.h"
8 #include "components/enhanced_bookmarks/enhanced_bookmark_model.h"
10 using bookmarks::BookmarkNode
;
12 namespace enhanced_bookmarks
{
14 ChromeBookmarkServerClusterService::ChromeBookmarkServerClusterService(
15 const std::string
& application_language_code
,
16 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter
,
17 ProfileOAuth2TokenService
* token_service
,
18 SigninManagerBase
* signin_manager
,
19 EnhancedBookmarkModel
* enhanced_bookmark_model
,
20 PrefService
* pref_service
,
21 ProfileSyncService
* sync_service
)
22 : BookmarkServerClusterService(application_language_code
,
23 request_context_getter
,
26 enhanced_bookmark_model
,
28 sync_service_(sync_service
) {
30 sync_service_
->AddObserver(this);
33 ChromeBookmarkServerClusterService::~ChromeBookmarkServerClusterService() {
35 sync_service_
->RemoveObserver(this);
38 void ChromeBookmarkServerClusterService::AddObserver(
39 BookmarkServerServiceObserver
* observer
) {
40 BookmarkServerClusterService::AddObserver(observer
);
41 if (sync_refresh_skipped_
) {
42 TriggerTokenRequest(false);
43 sync_refresh_skipped_
= false;
47 void ChromeBookmarkServerClusterService::OnStateChanged() {
51 void ChromeBookmarkServerClusterService::OnSyncCycleCompleted() {
52 // The stars cluster API relies on the information in chrome-sync. Sending a
53 // cluster request immediately after a bookmark is changed from the bookmark
54 // observer notification will yield the wrong results. The request must be
55 // delayed until the sync cycle has completed.
56 // Note that we will be skipping calling this cluster API if there is no
57 // observer attached, because calling that is meaningless without UI to show.
58 // We also will avoid requesting for clusters if the bookmark data hasn't
60 if (refreshes_needed_
> 0) {
61 DCHECK(model_
->loaded());
62 if (observers_
.might_have_observers()) {
63 TriggerTokenRequest(false);
64 sync_refresh_skipped_
= false;
66 sync_refresh_skipped_
= true;
72 void ChromeBookmarkServerClusterService::EnhancedBookmarkAdded(
73 const BookmarkNode
* node
) {
74 BookmarkServerClusterService::EnhancedBookmarkAdded(node
);
78 void ChromeBookmarkServerClusterService::EnhancedBookmarkRemoved(
79 const BookmarkNode
* node
) {
80 BookmarkServerClusterService::EnhancedBookmarkRemoved(node
);
84 void ChromeBookmarkServerClusterService::EnhancedBookmarkNodeChanged(
85 const BookmarkNode
* node
) {
86 BookmarkServerClusterService::EnhancedBookmarkNodeChanged(node
);
90 void ChromeBookmarkServerClusterService::InvalidateCache() {
91 // Bookmark changes can happen locally or via sync. It is difficult to
92 // determine if a given SyncCycle contains all the local modifications.
94 // Consider the following sequence:
95 // 1. SyncCycleBeginning (bookmark version:1)
96 // 2. Bookmarks mutate locally (bookmark version:2)
97 // 3. SyncCycleCompleted (bookmark version:1)
99 // In this case, the bookmarks modified locally won't be sent to the server
100 // until the next SyncCycleCompleted. Since we can't accurately determine
101 // if a bookmark change has been sent on a SyncCycleCompleted, we're always
102 // assuming that we need to wait for 2 sync cycles.
103 refreshes_needed_
= 2;
106 } // namespace enhanced_bookmarks