File: remote_suggestions_status_service.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 (113 lines) | stat: -rw-r--r-- 3,955 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// 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/remote/remote_suggestions_status_service.h"

#include <string>

#include "components/ntp_snippets/features.h"
#include "components/ntp_snippets/pref_names.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/variations/variations_associated_data.h"

namespace ntp_snippets {

namespace {

const char kFetchingRequiresSignin[] = "fetching_requires_signin";
const char kFetchingRequiresSigninEnabled[] = "true";
const char kFetchingRequiresSigninDisabled[] = "false";

}  // namespace

RemoteSuggestionsStatusService::RemoteSuggestionsStatusService(
    SigninManagerBase* signin_manager,
    PrefService* pref_service)
    : status_(RemoteSuggestionsStatus::EXPLICITLY_DISABLED),
      require_signin_(false),
      signin_manager_(signin_manager),
      pref_service_(pref_service) {
  std::string param_value_str = variations::GetVariationParamValueByFeature(
      kArticleSuggestionsFeature, kFetchingRequiresSignin);
  if (param_value_str == kFetchingRequiresSigninEnabled) {
    require_signin_ = true;
  } else if (!param_value_str.empty() &&
             param_value_str != kFetchingRequiresSigninDisabled) {
    DLOG(WARNING) << "Unknow value for the variations parameter "
                  << kFetchingRequiresSignin << ": " << param_value_str;
  }
}

RemoteSuggestionsStatusService::~RemoteSuggestionsStatusService() = default;

// static
void RemoteSuggestionsStatusService::RegisterProfilePrefs(
    PrefRegistrySimple* registry) {
  registry->RegisterBooleanPref(prefs::kEnableSnippets, true);
}

void RemoteSuggestionsStatusService::Init(
    const StatusChangeCallback& callback) {
  DCHECK(status_change_callback_.is_null());

  status_change_callback_ = callback;

  // Notify about the current state before registering the observer, to make
  // sure we don't get a double notification due to an undefined start state.
  RemoteSuggestionsStatus old_status = status_;
  status_ = GetStatusFromDeps();
  status_change_callback_.Run(old_status, status_);

  pref_change_registrar_.Init(pref_service_);
  pref_change_registrar_.Add(
      prefs::kEnableSnippets,
      base::Bind(&RemoteSuggestionsStatusService::OnSnippetsEnabledChanged,
                 base::Unretained(this)));
}

void RemoteSuggestionsStatusService::OnSnippetsEnabledChanged() {
  OnStateChanged(GetStatusFromDeps());
}

void RemoteSuggestionsStatusService::OnStateChanged(
    RemoteSuggestionsStatus new_status) {
  if (new_status == status_) {
    return;
  }

  status_change_callback_.Run(status_, new_status);
  status_ = new_status;
}

bool RemoteSuggestionsStatusService::IsSignedIn() const {
  // TODO(dgn): remove the SigninManager dependency. It should be possible to
  // replace it by passing the new state via OnSignInStateChanged().
  return signin_manager_ && signin_manager_->IsAuthenticated();
}

void RemoteSuggestionsStatusService::OnSignInStateChanged() {
  OnStateChanged(GetStatusFromDeps());
}

RemoteSuggestionsStatus RemoteSuggestionsStatusService::GetStatusFromDeps()
    const {
  if (!pref_service_->GetBoolean(prefs::kEnableSnippets)) {
    DVLOG(1) << "[GetStatusFromDeps] Disabled via pref";
    return RemoteSuggestionsStatus::EXPLICITLY_DISABLED;
  }

  if (require_signin_ && !IsSignedIn()) {
    DVLOG(1) << "[GetStatusFromDeps] Signed out and disabled due to this.";
    return RemoteSuggestionsStatus::SIGNED_OUT_AND_DISABLED;
  }

  DVLOG(1) << "[GetStatusFromDeps] Enabled, signed "
           << (IsSignedIn() ? "in" : "out");
  return IsSignedIn() ? RemoteSuggestionsStatus::ENABLED_AND_SIGNED_IN
                      : RemoteSuggestionsStatus::ENABLED_AND_SIGNED_OUT;
}

}  // namespace ntp_snippets