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

android_webview / browser / metrics / visibility_metrics_logger.h [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ANDROID_WEBVIEW_BROWSER_METRICS_VISIBILITY_METRICS_LOGGER_H_
#define ANDROID_WEBVIEW_BROWSER_METRICS_VISIBILITY_METRICS_LOGGER_H_

#include <map>
#include <string>
#include <vector>

#include "base/functional/callback.h"
#include "base/time/time.h"

namespace android_webview {

// Records how much of the screen is covered by WebViews. This helps us
// determine what WebView is being used for.
//
// Lifetime: Singleton
class VisibilityMetricsLogger {
 public:
  // These values are persisted to logs and must match the WebViewUrlScheme enum
  // defined in enums.xml. Entries should not be renumbered and numeric values
  // should never be reused.
  enum class Scheme {
    kEmpty = 0,
    kUnknown = 1,
    kHttp = 2,
    kHttps = 3,
    kFile = 4,
    kFtp = 5,
    kData = 6,
    kJavaScript = 7,
    kAbout = 8,
    kChrome = 9,
    kBlob = 10,
    kContent = 11,
    kIntent = 12,
    kMaxValue = kIntent,
  };

  static Scheme SchemeStringToEnum(const std::string& scheme);

  struct VisibilityInfo {
    bool view_attached = false;
    bool view_visible = false;
    bool window_visible = false;
    Scheme scheme = Scheme::kEmpty;

    bool IsVisible() const;
  };

  // These values are persisted to logs. Entries should not be renumbered and
  // numeric values should never be reused.
  enum class Visibility {
    kVisible = 0,
    kNotVisible = 1,
    kMaxValue = kNotVisible
  };

  class Client {
   public:
    virtual VisibilityInfo GetVisibilityInfo() = 0;
  };

  enum class ClientAction {
    kAdded = 0,
    kRemoved = 1,
    kVisibilityChanged = 2,
    kMaxValue = kVisibilityChanged,
  };

  VisibilityMetricsLogger();
  virtual ~VisibilityMetricsLogger();

  VisibilityMetricsLogger(const VisibilityMetricsLogger&) = delete;
  VisibilityMetricsLogger& operator=(const VisibilityMetricsLogger&) = delete;

  void AddClient(Client* client);
  void RemoveClient(Client* client);
  void ClientVisibilityChanged(Client* client);
  void UpdateScreenCoverage(int global_percentage,
                            const std::vector<Scheme>& schemes,
                            const std::vector<int>& scheme_percentages);

  void RecordMetrics();

  // Set a callback that is executed when global visibility changes, i.e. when:
  //  - false => true: no client was visible and one becomes visible.
  //  - true => false: >=1 clients were visible and all became hidden.
  using OnVisibilityChangedCallback =
      base::RepeatingCallback<void(bool /*visible*/)>;
  void SetOnVisibilityChangedCallback(OnVisibilityChangedCallback);

 private:
  void UpdateDurations();
  void ProcessClientUpdate(Client* client,
                           const VisibilityInfo& info,
                           ClientAction action);
  void RecordVisibilityMetrics();
  void RecordVisibleSchemeMetrics();
  void RecordScreenCoverageMetrics();

  // Counts the number of visible clients.
  size_t all_clients_visible_count_ = 0;
  // Counts the number of visible clients per scheme.
  size_t per_scheme_visible_counts_[static_cast<size_t>(Scheme::kMaxValue) +
                                    1] = {};

  struct WebViewDurationTracker {
    // Duration any WebView meets the tracking criteria
    base::TimeDelta any_webview_tracked_duration_ = base::Seconds(0);
    // Duration no WebViews meet the tracking criteria
    base::TimeDelta no_webview_tracked_duration_ = base::Seconds(0);
    // Total duration that WebViews meet the tracking criteria (i.e. if
    // 2x WebViews meet the criteria for 1 second then increment by 2 seconds)
    base::TimeDelta per_webview_duration_ = base::Seconds(0);
    // Total duration that WebViews exist but do not meet the tracking criteria
    base::TimeDelta per_webview_untracked_duration_ = base::Seconds(0);
  };

  WebViewDurationTracker all_clients_tracker_;
  WebViewDurationTracker
      per_scheme_trackers_[static_cast<size_t>(Scheme::kMaxValue) + 1] = {};

  base::TimeTicks last_update_time_;
  std::map<Client*, VisibilityInfo> client_visibility_;

  // The screen coverage percentage for all visible AwContents merged together.
  int global_coverage_percentage_ = 0;

  // The durations by screen coverage percentage for all visible AwContents
  // merged together.
  base::TimeDelta global_coverage_percentage_durations_[101] = {};

  // The currently visible schemes and their screen coverage percentages. A
  // scheme can occur more than once at a time so this uses a multimap.
  std::multimap<Scheme, int> schemes_to_coverage_percentages_;

  // The durations by screen coverage percentage and visible scheme.
  std::map<Scheme, std::map<int, base::TimeDelta>>
      schemes_to_percentages_to_durations_;

  OnVisibilityChangedCallback on_visibility_changed_callback_;
};

}  // namespace android_webview

#endif  // ANDROID_WEBVIEW_BROWSER_METRICS_VISIBILITY_METRICS_LOGGER_H_