summaryrefslogtreecommitdiff
path: root/chromium/components/feed/core/v2/types.h
blob: 3973ffdca606965646e7a7e311c6ee1ed152f086 (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
// Copyright 2020 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.

#ifndef COMPONENTS_FEED_CORE_V2_TYPES_H_
#define COMPONENTS_FEED_CORE_V2_TYPES_H_

#include <cstdint>
#include <iosfwd>
#include <string>
#include <vector>

#include "base/containers/flat_set.h"
#include "base/time/time.h"
#include "base/types/id_type.h"
#include "base/values.h"
#include "components/feed/core/proto/v2/wire/client_info.pb.h"
#include "components/feed/core/proto/v2/wire/reliability_logging_enums.pb.h"
#include "components/feed/core/v2/enums.h"
#include "components/feed/core/v2/public/common_enums.h"
#include "components/feed/core/v2/public/types.h"

namespace feed {

// Make sure public types are included here too.
// See components/feed/core/v2/public/types.h.
using ::feed::ChromeInfo;
using ::feed::EphemeralChangeId;
using ::feed::Experiments;
using ::feed::WebFeedSubscriptionRequestStatus;
using ::feed::WebFeedSubscriptionStatus;

// Uniquely identifies a revision of a |feedstore::Content|. If Content changes,
// it is assigned a new revision number.
using ContentRevision = base::IdTypeU32<class ContentRevisionClass>;

// ID for a stored pending action.
using LocalActionId = base::IdType32<class LocalActionIdClass>;

std::string ToString(ContentRevision c);
ContentRevision ToContentRevision(const std::string& str);

// Metadata sent with Feed requests.
struct RequestMetadata {
  RequestMetadata();
  ~RequestMetadata();
  RequestMetadata(RequestMetadata&&);
  RequestMetadata& operator=(RequestMetadata&&);

  feedwire::ClientInfo ToClientInfo() const;

  ChromeInfo chrome_info;
  std::string language_tag;
  std::string client_instance_id;
  std::string session_id;
  DisplayMetrics display_metrics;
  ContentOrder content_order = ContentOrder::kUnspecified;
  bool notice_card_acknowledged = false;
  bool autoplay_enabled = false;
  std::vector<std::string> acknowledged_notice_keys;
};

// Data internal to MetricsReporter which is persisted to Prefs.
struct PersistentMetricsData {
  // The midnight time for the day in which this metric was recorded.
  base::Time current_day_start;
  // The total recorded time spent on the Feed for the current day.
  base::TimeDelta accumulated_time_spent_in_feed;
};

base::Value PersistentMetricsDataToValue(const PersistentMetricsData& data);
PersistentMetricsData PersistentMetricsDataFromValue(const base::Value& value);

class LoadLatencyTimes {
 public:
  enum StepKind {
    // Time from when the LoadStreamTask was created to when it is executed.
    kTaskExecution,
    // Time spent loading the stream state from storage.
    kLoadFromStore,
    // Time spent querying for and uploading stored actions. Recorded even if
    // no actions are uploaded.
    kUploadActions,
    // Time spent making the FeedQuery (or WebFeed List Contents) request.
    kQueryRequest,
    // A view was reported in the stream, indicating the stream was shown.
    kStreamViewed,
  };
  struct Step {
    StepKind kind;
    base::TimeDelta latency;
  };

  LoadLatencyTimes();
  ~LoadLatencyTimes();
  LoadLatencyTimes(const LoadLatencyTimes&) = delete;
  LoadLatencyTimes& operator=(const LoadLatencyTimes&) = delete;

  void StepComplete(StepKind kind);

  const std::vector<Step>& steps() const { return steps_; }

 private:
  base::TimeTicks last_time_;
  std::vector<Step> steps_;
};

// Tracks a set of `feedstore::Content` content IDs, for tracking whether unread
// content is received from the server.
class ContentIdSet {
 public:
  ContentIdSet();
  ~ContentIdSet();
  explicit ContentIdSet(base::flat_set<int64_t> ids);
  ContentIdSet(const ContentIdSet&);
  ContentIdSet(ContentIdSet&&);
  ContentIdSet& operator=(const ContentIdSet&);
  ContentIdSet& operator=(ContentIdSet&&);

  // Returns whether this set contains all items.
  bool ContainsAllOf(const ContentIdSet& items) const;
  bool IsEmpty() const;
  const base::flat_set<int64_t>& values() const { return content_ids_; }

  bool operator==(const ContentIdSet& rhs) const;

 private:
  // Note, we only store the `id` field of ContentId, with the assumption that
  // `id` is unique enough given these are only `feedstore::Content` ids.
  base::flat_set<int64_t> content_ids_;
};

std::ostream& operator<<(std::ostream& s, const ContentIdSet& id_set);

struct ContentStats {
  int card_count = 0;
  int total_content_frame_size_bytes = 0;
  int shared_state_size = 0;
};

struct LaunchResult {
  LoadStreamStatus load_stream_status;
  feedwire::DiscoverLaunchResult launch_result;

  LaunchResult(LoadStreamStatus load_stream_status,
               feedwire::DiscoverLaunchResult launch_result);
  LaunchResult(const LaunchResult& other);
  ~LaunchResult();
  LaunchResult& operator=(const LaunchResult& other);
};

}  // namespace feed

#endif  // COMPONENTS_FEED_CORE_V2_TYPES_H_