File: tab_delegate_sync_adapter.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (87 lines) | stat: -rw-r--r-- 3,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/ntp_snippets/sessions/tab_delegate_sync_adapter.h"

#include "components/sync/driver/sync_service.h"
#include "components/sync_sessions/open_tabs_ui_delegate.h"

using syncer::SyncService;
using sync_sessions::OpenTabsUIDelegate;

namespace ntp_snippets {

TabDelegateSyncAdapter::TabDelegateSyncAdapter(SyncService* sync_service)
    : sync_service_(sync_service) {
  sync_service_->AddObserver(this);
}

TabDelegateSyncAdapter::~TabDelegateSyncAdapter() {
  sync_service_->RemoveObserver(this);
}

bool TabDelegateSyncAdapter::HasSessionsData() {
  // GetOpenTabsUIDelegate will be a nullptr if sync has not started, or if the
  // sessions data type is not enabled.
  return sync_service_->GetOpenTabsUIDelegate() != nullptr;
}

std::vector<const sync_sessions::SyncedSession*>
TabDelegateSyncAdapter::GetAllForeignSessions() {
  std::vector<const sync_sessions::SyncedSession*> sessions;
  OpenTabsUIDelegate* delegate = sync_service_->GetOpenTabsUIDelegate();
  if (delegate != nullptr) {
    // The return bool from GetAllForeignSessions(...) is ignored.
    delegate->GetAllForeignSessions(&sessions);
  }
  return sessions;
}

void TabDelegateSyncAdapter::SubscribeForForeignTabChange(
    const base::Closure& change_callback) {
  DCHECK(change_callback_.is_null());
  change_callback_ = change_callback;
}

void TabDelegateSyncAdapter::OnStateChanged() {
  // OnStateChanged gets called very frequently, and usually is not important.
  // But there are some events, like disabling sync and signing out, that are
  // only captured through OnStateChange. In an attempt to send as few messages
  // as possible, track if there was session data, and always/only invoke the
  // callback when transitioning between states. This will also capture the case
  // when Open Tab is added/removed from syncing types. Note that this requires
  // the object behind GetOpenTabsUIDelegate() to have its real data when it
  // becomes available. Otherwise we might transition to think we have session
  // data, but invoke our callback while the GetOpenTabsUIDelegate() returns bad
  // results. Fortunately, this isn't a problem. GetOpenTabsUIDelegate() is
  // guarded by verifying the data type is RUNNING, which always means the
  // sessions merge has already happened.
  if (had_session_data_ != HasSessionsData()) {
    InvokeCallback();
  }
}

void TabDelegateSyncAdapter::OnSyncConfigurationCompleted() {
  // Ignored. This event can let us know when the set of enabled data types
  // change. However, we want to avoid useless notifications as much as
  // possible, and all of the information captured in this event will also be
  // covered by OnStateChange.
}

void TabDelegateSyncAdapter::OnForeignSessionUpdated() {
  // Foreign tab data changed, always invoke the callback to generate new
  // suggestions. Interestingly, this is only triggered after sync model type
  // apply, not after merge. The merge case should always be handled by
  // OnStateChange.
  InvokeCallback();
}

void TabDelegateSyncAdapter::InvokeCallback() {
  had_session_data_ = HasSessionsData();
  if (!change_callback_.is_null()) {
    change_callback_.Run();
  }
}

}  // namespace ntp_snippets