summaryrefslogtreecommitdiffstats
path: root/chrome/browser/bookmarks/enhanced_bookmarks_features.cc
blob: 5ce9b9aefa09d514ad487f7d910f1034f633b698 (plain)
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright 2013 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 "chrome/browser/bookmarks/enhanced_bookmarks_features.h"

#include "base/command_line.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/sha1.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/sync_driver/pref_names.h"
#include "components/variations/variations_associated_data.h"
#include "extensions/common/features/feature.h"
#include "extensions/common/features/feature_provider.h"

namespace {

const char kFieldTrialName[] = "EnhancedBookmarks";

// Get extension id from Finch EnhancedBookmarks group parameters.
std::string GetEnhancedBookmarksExtensionIdFromFinch() {
  return chrome_variations::GetVariationParamValue(kFieldTrialName, "id");
}

// Returns true if enhanced bookmarks experiment is enabled from Finch.
bool IsEnhancedBookmarksExperimentEnabledFromFinch() {
  std::string ext_id = GetEnhancedBookmarksExtensionIdFromFinch();
  extensions::FeatureProvider* feature_provider =
      extensions::FeatureProvider::GetPermissionFeatures();
  extensions::Feature* feature = feature_provider->GetFeature("metricsPrivate");
  return feature && feature->IsIdInWhitelist(ext_id);
}

};  // namespace

bool GetBookmarksExperimentExtensionID(const PrefService* user_prefs,
                                       std::string* extension_id) {
  BookmarksExperimentState bookmarks_experiment_state =
      static_cast<BookmarksExperimentState>(user_prefs->GetInteger(
          sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
  if (bookmarks_experiment_state == kBookmarksExperimentEnabledFromFinch) {
    *extension_id = GetEnhancedBookmarksExtensionIdFromFinch();
    return !extension_id->empty();
  }
  if (bookmarks_experiment_state == kBookmarksExperimentEnabled) {
    *extension_id = user_prefs->GetString(
        sync_driver::prefs::kEnhancedBookmarksExtensionId);
    return !extension_id->empty();
  }

  return false;
}

void UpdateBookmarksExperimentState(PrefService* user_prefs,
                                    PrefService* local_state,
                                    bool user_signed_in) {
  BookmarksExperimentState bookmarks_experiment_state_before =
      static_cast<BookmarksExperimentState>(user_prefs->GetInteger(
          sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
  // If user signed out, clear possible previous state.
  if (!user_signed_in) {
    bookmarks_experiment_state_before = kNoBookmarksExperiment;
    ForceFinchBookmarkExperimentIfNeeded(local_state, kNoBookmarksExperiment);
  }

  // kEnhancedBookmarksExperiment flag could have values "", "1" and "0".
  // "0" - user opted out.
  bool opt_out = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
                     switches::kEnhancedBookmarksExperiment) == "0";

  BookmarksExperimentState bookmarks_experiment_new_state =
      kNoBookmarksExperiment;
  bool enabled_from_finch = false;
  if (IsEnhancedBookmarksExperimentEnabledFromFinch()) {
    enabled_from_finch = !user_signed_in;
    if (user_signed_in) {
      bookmarks_experiment_new_state =
          kBookmarksExperimentEnabledFromFinchUserSignedIn;
    }
  }

  if (enabled_from_finch) {
    if (opt_out) {
      // Experiment enabled but user opted out.
      bookmarks_experiment_new_state = kBookmarksExperimentOptOutFromFinch;
    } else {
      // Experiment enabled.
      bookmarks_experiment_new_state = kBookmarksExperimentEnabledFromFinch;
    }
  } else if (bookmarks_experiment_state_before == kBookmarksExperimentEnabled) {
    if (opt_out) {
      // Experiment enabled but user opted out.
      bookmarks_experiment_new_state = kBookmarksExperimentEnabledUserOptOut;
    } else {
      bookmarks_experiment_new_state = kBookmarksExperimentEnabled;
    }
  } else if (bookmarks_experiment_state_before ==
             kBookmarksExperimentEnabledUserOptOut) {
    if (opt_out) {
      bookmarks_experiment_new_state = kBookmarksExperimentEnabledUserOptOut;
    } else {
      // User opted in again.
      bookmarks_experiment_new_state = kBookmarksExperimentEnabled;
    }
  }

  UMA_HISTOGRAM_ENUMERATION("EnhancedBookmarks.SyncExperimentState",
                            bookmarks_experiment_new_state,
                            kBookmarksExperimentEnumSize);
  user_prefs->SetInteger(
      sync_driver::prefs::kEnhancedBookmarksExperimentEnabled,
      bookmarks_experiment_new_state);
  if (bookmarks_experiment_state_before != bookmarks_experiment_new_state)
    ForceFinchBookmarkExperimentIfNeeded(local_state,
                                         bookmarks_experiment_new_state);
}

void ForceFinchBookmarkExperimentIfNeeded(
    PrefService* local_state,
    BookmarksExperimentState bookmarks_experiment_state) {
// Chrome OS doesnt use local storage for experiments flags.
#if !defined(OS_CHROMEOS)
  if (!local_state)
    return;
  ListPrefUpdate update(local_state, prefs::kEnabledLabsExperiments);
  base::ListValue* experiments_list = update.Get();
  if (!experiments_list)
    return;
  size_t index;
  if (bookmarks_experiment_state == kNoBookmarksExperiment) {
    experiments_list->Remove(
        base::StringValue(switches::kManualEnhancedBookmarks), &index);
    experiments_list->Remove(
        base::StringValue(switches::kManualEnhancedBookmarksOptout), &index);
  } else if (bookmarks_experiment_state == kBookmarksExperimentEnabled) {
    experiments_list->Remove(
        base::StringValue(switches::kManualEnhancedBookmarksOptout), &index);
    experiments_list->AppendIfNotPresent(
        new base::StringValue(switches::kManualEnhancedBookmarks));
  } else if (bookmarks_experiment_state ==
                 kBookmarksExperimentEnabledUserOptOut) {
    experiments_list->Remove(
        base::StringValue(switches::kManualEnhancedBookmarks), &index);
    experiments_list->AppendIfNotPresent(
        new base::StringValue(switches::kManualEnhancedBookmarksOptout));
  }
#endif
}

bool IsEnhancedBookmarksExperimentEnabled() {
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(switches::kManualEnhancedBookmarks) ||
      command_line->HasSwitch(switches::kManualEnhancedBookmarksOptout)) {
    return true;
  }

  return IsEnhancedBookmarksExperimentEnabledFromFinch();
}

bool IsEnableDomDistillerSet() {
  if (CommandLine::ForCurrentProcess()->
      HasSwitch(switches::kEnableDomDistiller)) {
    return true;
  }
  if (chrome_variations::GetVariationParamValue(
          kFieldTrialName, "enable-dom-distiller") == "1")
    return true;

  return false;
}

bool IsEnableSyncArticlesSet() {
  if (CommandLine::ForCurrentProcess()->
      HasSwitch(switches::kEnableSyncArticles)) {
    return true;
  }
  if (chrome_variations::GetVariationParamValue(
          kFieldTrialName, "enable-sync-articles") == "1")
    return true;

  return false;
}