Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / enhanced_bookmarks / chrome_bookmark_server_cluster_service.cc
blob82c5be6a976b5598302e2f822faa7c24ff482ca9
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,
24 token_service,
25 signin_manager,
26 enhanced_bookmark_model,
27 pref_service),
28 sync_service_(sync_service) {
29 if (sync_service_)
30 sync_service_->AddObserver(this);
33 ChromeBookmarkServerClusterService::~ChromeBookmarkServerClusterService() {
34 if (sync_service_)
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() {
48 // Do nothing.
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
59 // changed.
60 if (refreshes_needed_ > 0) {
61 DCHECK(model_->loaded());
62 if (observers_.might_have_observers()) {
63 TriggerTokenRequest(false);
64 sync_refresh_skipped_ = false;
65 } else {
66 sync_refresh_skipped_ = true;
68 --refreshes_needed_;
72 void ChromeBookmarkServerClusterService::EnhancedBookmarkAdded(
73 const BookmarkNode* node) {
74 BookmarkServerClusterService::EnhancedBookmarkAdded(node);
75 InvalidateCache();
78 void ChromeBookmarkServerClusterService::EnhancedBookmarkRemoved(
79 const BookmarkNode* node) {
80 BookmarkServerClusterService::EnhancedBookmarkRemoved(node);
81 InvalidateCache();
84 void ChromeBookmarkServerClusterService::EnhancedBookmarkNodeChanged(
85 const BookmarkNode* node) {
86 BookmarkServerClusterService::EnhancedBookmarkNodeChanged(node);
87 InvalidateCache();
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