summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/ui/webui/explore_sites_internals/explore_sites_internals_page_handler.cc
blob: b9a460ddfb3ad3aee70012cf84e022372073b321 (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
// Copyright 2018 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/ui/webui/explore_sites_internals/explore_sites_internals_page_handler.h"

#include <string>
#include <utility>
#include <vector>

#include "base/containers/flat_map.h"
#include "chrome/browser/android/chrome_feature_list.h"
#include "chrome/browser/android/explore_sites/explore_sites_feature.h"
#include "chrome/browser/android/explore_sites/url_util.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/explore_sites_internals/explore_sites_internals.mojom.h"
#include "components/language/core/browser/pref_names.h"
#include "components/prefs/pref_service.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"

namespace explore_sites {
using chrome::android::explore_sites::ExploreSitesVariation;
using chrome::android::explore_sites::GetExploreSitesVariation;

namespace {

std::string GetChromeFlagsSetupString() {
  switch (GetExploreSitesVariation()) {
    case ExploreSitesVariation::ENABLED:
      return "Enabled";
    case ExploreSitesVariation::EXPERIMENT:
      return "Experiment";
    case ExploreSitesVariation::PERSONALIZED:
      return "Personalized";
    case ExploreSitesVariation::MOST_LIKELY:
      return "Most Likely";
    case ExploreSitesVariation::DISABLED:
      return "Disabled";
  }
}
}  // namespace

ExploreSitesInternalsPageHandler::ExploreSitesInternalsPageHandler(
    mojo::PendingReceiver<explore_sites_internals::mojom::PageHandler> receiver,
    ExploreSitesService* explore_sites_service,
    Profile* profile)
    : receiver_(this, std::move(receiver)),
      explore_sites_service_(explore_sites_service),
      profile_(profile) {}

ExploreSitesInternalsPageHandler::~ExploreSitesInternalsPageHandler() {}

void ExploreSitesInternalsPageHandler::GetProperties(
    GetPropertiesCallback callback) {
  base::flat_map<std::string, std::string> properties;
  properties["chrome-flags-setup"] = GetChromeFlagsSetupString();
  properties["server-endpoint"] = GetCatalogURL().spec();
  properties["country-code"] = explore_sites_service_->GetCountryCode();
  std::move(callback).Run(properties);
}

void ExploreSitesInternalsPageHandler::ClearCachedExploreSitesCatalog(
    ClearCachedExploreSitesCatalogCallback callback) {
  if (ExploreSitesVariation::DISABLED == GetExploreSitesVariation()) {
    std::move(callback).Run(false);
    return;
  }

  explore_sites_service_->ClearCachedCatalogsForDebugging();
  std::move(callback).Run(true);
}

void ExploreSitesInternalsPageHandler::ForceNetworkRequest(
    ForceNetworkRequestCallback callback) {
  explore_sites_service_->UpdateCatalogFromNetwork(
      true /* is_immediate_fetch */,
      profile_->GetPrefs()->GetString(language::prefs::kAcceptLanguages),
      std::move(callback));
}

void ExploreSitesInternalsPageHandler::OverrideCountryCode(
    const std::string& country_code,
    OverrideCountryCodeCallback callback) {
  if (ExploreSitesVariation::DISABLED == GetExploreSitesVariation()) {
    std::move(callback).Run(false);
    return;
  }

  explore_sites_service_->OverrideCountryCodeForDebugging(country_code);
  std::move(callback).Run(true);
}

}  // namespace explore_sites