Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / enhanced_bookmarks / chrome_bookmark_server_cluster_service.cc
blob2643b3999cf432621c9525d332cbe68172e5b656
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 namespace enhanced_bookmarks {
12 ChromeBookmarkServerClusterService::ChromeBookmarkServerClusterService(
13 const std::string& application_language_code,
14 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
15 ProfileOAuth2TokenService* token_service,
16 SigninManagerBase* signin_manager,
17 EnhancedBookmarkModel* enhanced_bookmark_model,
18 PrefService* pref_service,
19 ProfileSyncService* sync_service)
20 : BookmarkServerClusterService(application_language_code,
21 request_context_getter,
22 token_service,
23 signin_manager,
24 enhanced_bookmark_model,
25 pref_service),
26 sync_service_(sync_service) {
27 if (sync_service_)
28 sync_service_->AddObserver(this);
31 ChromeBookmarkServerClusterService::~ChromeBookmarkServerClusterService() {
32 if (sync_service_)
33 sync_service_->RemoveObserver(this);
36 void ChromeBookmarkServerClusterService::AddObserver(
37 BookmarkServerServiceObserver* observer) {
38 BookmarkServerClusterService::AddObserver(observer);
39 if (sync_refresh_skipped_) {
40 TriggerTokenRequest(false);
41 sync_refresh_skipped_ = false;
45 void ChromeBookmarkServerClusterService::OnStateChanged() {
46 // Do nothing.
49 void ChromeBookmarkServerClusterService::OnSyncCycleCompleted() {
50 // The stars cluster API relies on the information in chrome-sync. Sending a
51 // cluster request immediately after a bookmark is changed from the bookmark
52 // observer notification will yield the wrong results. The request must be
53 // delayed until the sync cycle has completed.
54 // Note that we will be skipping calling this cluster API if there is no
55 // observer attached, because calling that is meaningless without UI to show.
56 // We also will avoid requesting for clusters if the bookmark data hasn't
57 // changed.
58 if (refreshes_needed_ > 0) {
59 DCHECK(model_->loaded());
60 if (observers_.might_have_observers()) {
61 TriggerTokenRequest(false);
62 sync_refresh_skipped_ = false;
63 } else {
64 sync_refresh_skipped_ = true;
66 --refreshes_needed_;
70 void ChromeBookmarkServerClusterService::EnhancedBookmarkAdded(
71 const BookmarkNode* node) {
72 BookmarkServerClusterService::EnhancedBookmarkAdded(node);
73 InvalidateCache();
76 void ChromeBookmarkServerClusterService::EnhancedBookmarkRemoved(
77 const BookmarkNode* node) {
78 BookmarkServerClusterService::EnhancedBookmarkRemoved(node);
79 InvalidateCache();
82 void ChromeBookmarkServerClusterService::EnhancedBookmarkNodeChanged(
83 const BookmarkNode* node) {
84 BookmarkServerClusterService::EnhancedBookmarkNodeChanged(node);
85 InvalidateCache();
88 void ChromeBookmarkServerClusterService::InvalidateCache() {
89 // Bookmark changes can happen locally or via sync. It is difficult to
90 // determine if a given SyncCycle contains all the local modifications.
92 // Consider the following sequence:
93 // 1. SyncCycleBeginning (bookmark version:1)
94 // 2. Bookmarks mutate locally (bookmark version:2)
95 // 3. SyncCycleCompleted (bookmark version:1)
97 // In this case, the bookmarks modified locally won't be sent to the server
98 // until the next SyncCycleCompleted. Since we can't accurately determine
99 // if a bookmark change has been sent on a SyncCycleCompleted, we're always
100 // assuming that we need to wait for 2 sync cycles.
101 refreshes_needed_ = 2;
104 } // namespace enhanced_bookmarks