1a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
2a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)// found in the LICENSE file.
4a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
5a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/bookmarks/enhanced_bookmarks_features.h"
6a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
7a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "base/command_line.h"
8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/metrics/histogram.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/prefs/pref_service.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/prefs/scoped_user_pref_update.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/sha1.h"
125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/browser_process.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/flags_storage.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/profiles/profile.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/signin/signin_manager_factory.h"
17a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/pref_names.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/signin/core/browser/signin_manager.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "components/sync_driver/pref_names.h"
21a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "components/variations/variations_associated_data.h"
22a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "extensions/common/features/feature.h"
23a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "extensions/common/features/feature_provider.h"
24a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)const char kFieldTrialName[] = "EnhancedBookmarks";
28a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Get extension id from Finch EnhancedBookmarks group parameters.
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)std::string GetEnhancedBookmarksExtensionIdFromFinch() {
315f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return variations::GetVariationParamValue(kFieldTrialName, "id");
32cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
33cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
34cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Returns true if enhanced bookmarks experiment is enabled from Finch.
35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool IsEnhancedBookmarksExperimentEnabledFromFinch() {
366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const std::string ext_id = GetEnhancedBookmarksExtensionIdFromFinch();
376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(OS_ANDROID)
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return !ext_id.empty();
396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#else
40cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const extensions::FeatureProvider* feature_provider =
41cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      extensions::FeatureProvider::GetPermissionFeatures();
42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  extensions::Feature* feature = feature_provider->GetFeature("metricsPrivate");
43cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return feature && feature->IsIdInWhitelist(ext_id);
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};  // namespace
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)bool GetBookmarksExperimentExtensionID(const PrefService* user_prefs,
50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                       std::string* extension_id) {
51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  BookmarksExperimentState bookmarks_experiment_state =
52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      static_cast<BookmarksExperimentState>(user_prefs->GetInteger(
53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
5446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_ENABLED_FROM_FINCH) {
55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    *extension_id = GetEnhancedBookmarksExtensionIdFromFinch();
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return !extension_id->empty();
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
5846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_ENABLED) {
59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    *extension_id = user_prefs->GetString(
60cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        sync_driver::prefs::kEnhancedBookmarksExtensionId);
61cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return !extension_id->empty();
62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return false;
65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void UpdateBookmarksExperimentState(
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefService* user_prefs,
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    PrefService* local_state,
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool user_signed_in,
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    BookmarksExperimentState experiment_enabled_from_sync) {
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) PrefService* flags_storage = local_state;
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#if defined(OS_CHROMEOS)
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // Chrome OS is using user prefs for flags storage.
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  flags_storage = user_prefs;
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#endif
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  BookmarksExperimentState bookmarks_experiment_state_before =
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      static_cast<BookmarksExperimentState>(user_prefs->GetInteger(
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // If user signed out, clear possible previous state.
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!user_signed_in) {
8346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bookmarks_experiment_state_before = BOOKMARKS_EXPERIMENT_NONE;
8446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    ForceFinchBookmarkExperimentIfNeeded(flags_storage,
8546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        BOOKMARKS_EXPERIMENT_NONE);
86cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
87cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
88cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // kEnhancedBookmarksExperiment flag could have values "", "1" and "0".
89cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // "0" - user opted out.
90cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  bool opt_out = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
91cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                     switches::kEnhancedBookmarksExperiment) == "0";
92cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
93cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  BookmarksExperimentState bookmarks_experiment_new_state =
9446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      BOOKMARKS_EXPERIMENT_NONE;
95cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
96cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (IsEnhancedBookmarksExperimentEnabledFromFinch() && !user_signed_in) {
97cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (opt_out) {
98cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Experiment enabled but user opted out.
9946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_OPT_OUT_FROM_FINCH;
100cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else {
101cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Experiment enabled.
10246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_ENABLED_FROM_FINCH;
103cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
10446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  } else if (experiment_enabled_from_sync == BOOKMARKS_EXPERIMENT_ENABLED) {
105cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Experiment enabled from Chrome sync.
106cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (opt_out) {
107cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Experiment enabled but user opted out.
10846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state =
10946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          BOOKMARKS_EXPERIMENT_ENABLED_USER_OPT_OUT;
110cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else {
111cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Experiment enabled.
11246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_ENABLED;
113cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
11446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  } else if (experiment_enabled_from_sync == BOOKMARKS_EXPERIMENT_NONE) {
115cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // Experiment is not enabled from Chrome sync.
11646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_NONE;
11746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  } else if (bookmarks_experiment_state_before ==
11846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)             BOOKMARKS_EXPERIMENT_ENABLED) {
119cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (opt_out) {
120cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // Experiment enabled but user opted out.
12146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state =
12246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          BOOKMARKS_EXPERIMENT_ENABLED_USER_OPT_OUT;
123cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else {
12446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_ENABLED;
125cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
126cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  } else if (bookmarks_experiment_state_before ==
12746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)             BOOKMARKS_EXPERIMENT_ENABLED_USER_OPT_OUT) {
128cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (opt_out) {
12946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state =
13046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          BOOKMARKS_EXPERIMENT_ENABLED_USER_OPT_OUT;
131cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    } else {
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // User opted in again.
13346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_ENABLED;
134cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
135cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
136cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_ANDROID)
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  bool opt_in = !opt_out
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      && CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          switches::kEnhancedBookmarksExperiment) == "1";
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (opt_in && bookmarks_experiment_new_state == BOOKMARKS_EXPERIMENT_NONE)
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    bookmarks_experiment_new_state = BOOKMARKS_EXPERIMENT_ENABLED;
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
145cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  UMA_HISTOGRAM_ENUMERATION("EnhancedBookmarks.SyncExperimentState",
146cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                            bookmarks_experiment_new_state,
14746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                            BOOKMARKS_EXPERIMENT_ENUM_SIZE);
148cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  user_prefs->SetInteger(
149cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      sync_driver::prefs::kEnhancedBookmarksExperimentEnabled,
150cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      bookmarks_experiment_new_state);
151cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ForceFinchBookmarkExperimentIfNeeded(flags_storage,
152cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                       bookmarks_experiment_new_state);
153cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)}
154cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid InitBookmarksExperimentState(Profile* profile) {
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SigninManagerBase* signin = SigninManagerFactory::GetForProfile(profile);
157ab8f6f0bd665d3c1ff476eb06c58c42630e462d4Ben Murdoch  bool is_signed_in = signin && signin->IsAuthenticated();
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  UpdateBookmarksExperimentState(
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      profile->GetPrefs(),
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      g_browser_process->local_state(),
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      is_signed_in,
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      BOOKMARKS_EXPERIMENT_ENABLED_FROM_SYNC_UNKNOWN);
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
165cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)void ForceFinchBookmarkExperimentIfNeeded(
166cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    PrefService* flags_storage,
167a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    BookmarksExperimentState bookmarks_experiment_state) {
168cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!flags_storage)
169cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
170cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  ListPrefUpdate update(flags_storage, prefs::kEnabledLabsExperiments);
171a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::ListValue* experiments_list = update.Get();
172cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (!experiments_list)
173cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return;
174a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  size_t index;
17546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  if (bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_NONE) {
176a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->Remove(
177a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringValue(switches::kManualEnhancedBookmarks), &index);
178a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->Remove(
179a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringValue(switches::kManualEnhancedBookmarksOptout), &index);
18046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  } else if (bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_ENABLED) {
181a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->Remove(
182a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringValue(switches::kManualEnhancedBookmarksOptout), &index);
183a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->AppendIfNotPresent(
184a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        new base::StringValue(switches::kManualEnhancedBookmarks));
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (bookmarks_experiment_state ==
18646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                 BOOKMARKS_EXPERIMENT_ENABLED_USER_OPT_OUT) {
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->Remove(
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        base::StringValue(switches::kManualEnhancedBookmarks), &index);
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    experiments_list->AppendIfNotPresent(
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        new base::StringValue(switches::kManualEnhancedBookmarksOptout));
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool IsEnhancedBookmarksExperimentEnabled(
1951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    about_flags::FlagsStorage* flags_storage) {
1961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#if defined(OS_CHROMEOS)
1971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // We are not setting command line flags on Chrome OS to avoid browser restart
1981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // but still have flags in flags_storage. So check flags_storage instead.
1991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  const std::set<std::string> flags = flags_storage->GetFlags();
2001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (flags.find(switches::kManualEnhancedBookmarks) != flags.end())
2011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return true;
2021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (flags.find(switches::kManualEnhancedBookmarksOptout) != flags.end())
2031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return true;
2041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#else
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  CommandLine* command_line = CommandLine::ForCurrentProcess();
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (command_line->HasSwitch(switches::kManualEnhancedBookmarks) ||
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      command_line->HasSwitch(switches::kManualEnhancedBookmarksOptout)) {
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return true;
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
2101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#endif
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
212cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return IsEnhancedBookmarksExperimentEnabledFromFinch();
213a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
214a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
21546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#if defined(OS_ANDROID)
2161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool IsEnhancedBookmarkImageFetchingEnabled(const PrefService* user_prefs) {
2171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (IsEnhancedBookmarksEnabled(user_prefs))
21846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)    return true;
21946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
22046d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // Salient images are collected from visited bookmarked pages even if the
22146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // enhanced bookmark feature is turned off. This is to have some images
22246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // available so that in the future, when the feature is turned on, the user
22346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // experience is not a big list of flat colors. However as a precautionary
22446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  // measure it is possible to disable this collection of images from finch.
2255f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string disable_fetching = variations::GetVariationParamValue(
22646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      kFieldTrialName, "DisableImagesFetching");
22746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  return disable_fetching.empty();
22846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)}
2291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
2301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool IsEnhancedBookmarksEnabled(const PrefService* user_prefs) {
2311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  BookmarksExperimentState bookmarks_experiment_state =
2321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      static_cast<BookmarksExperimentState>(user_prefs->GetInteger(
2331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          sync_driver::prefs::kEnhancedBookmarksExperimentEnabled));
2341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_ENABLED ||
2351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      bookmarks_experiment_state == BOOKMARKS_EXPERIMENT_ENABLED_FROM_FINCH;
2361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
23746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#endif
23846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)
239a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool IsEnableDomDistillerSet() {
240a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->
241a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      HasSwitch(switches::kEnableDomDistiller)) {
242a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return true;
243a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
2445f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (variations::GetVariationParamValue(
245a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          kFieldTrialName, "enable-dom-distiller") == "1")
246a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return true;
247a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
248a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return false;
249a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
250a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
251a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)bool IsEnableSyncArticlesSet() {
252a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  if (CommandLine::ForCurrentProcess()->
253a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      HasSwitch(switches::kEnableSyncArticles)) {
254a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return true;
255a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  }
2565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (variations::GetVariationParamValue(
257a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)          kFieldTrialName, "enable-sync-articles") == "1")
258a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return true;
259a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)
260a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  return false;
261a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)}
262