File: media_engagement_preloaded_list.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (131 lines) | stat: -rw-r--r-- 3,734 bytes parent folder | download | duplicates (5)
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
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#include "chrome/browser/media/media_engagement_preloaded_list.h"

#include <cstdint>

#include "base/files/file_util.h"
#include "base/no_destructor.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "chrome/browser/media/media_engagement_preload.pb.h"
#include "net/base/lookup_string_in_fixed_set.h"
#include "url/origin.h"
#include "url/url_canon.h"
#include "url/url_constants.h"

// static
MediaEngagementPreloadedList* MediaEngagementPreloadedList::GetInstance() {
  static base::NoDestructor<MediaEngagementPreloadedList> instance;
  return instance.get();
}

MediaEngagementPreloadedList::MediaEngagementPreloadedList() {
  // This class is allowed to be instantiated on any thread.
  DETACH_FROM_SEQUENCE(sequence_checker_);
}

MediaEngagementPreloadedList::~MediaEngagementPreloadedList() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

bool MediaEngagementPreloadedList::loaded() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return is_loaded_;
}

bool MediaEngagementPreloadedList::empty() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return dafsa_.empty();
}

bool MediaEngagementPreloadedList::LoadFromFile(const base::FilePath& path) {
  DETACH_FROM_SEQUENCE(sequence_checker_);

  // Check the file exists.
  if (!base::PathExists(path)) {
    return false;
  }

  // Read the file to a string.
  std::string file_data;
  if (!base::ReadFileToString(path, &file_data)) {
    return false;
  }

  // Load the preloaded list into a proto message.
  chrome_browser_media::PreloadedData message;
  if (!message.ParseFromString(file_data)) {
    return false;
  }

  // Copy data from the protobuf message.
  dafsa_ =
      std::vector<uint8_t>(message.dafsa().c_str(),
                           message.dafsa().c_str() + message.dafsa().length());

  is_loaded_ = true;
  return true;
}

bool MediaEngagementPreloadedList::CheckOriginIsPresent(
    const url::Origin& origin) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Check if we have loaded the data.
  if (!loaded()) {
    return false;
  }

  // Check if the data is empty.
  if (empty()) {
    return false;
  }

  // By default we are just searching for the hostname.
  std::string location(origin.host());

  // Add :<port> if we use a non-default port.
  if (origin.port() != url::DefaultPortForScheme(origin.scheme())) {
    location.push_back(':');
    std::string port(base::NumberToString(origin.port()));
    location.append(std::move(port));
  }

  // Check the string is present and check the scheme matches.
  DafsaResult result = CheckStringIsPresent(location);
  switch (result) {
    case DafsaResult::kFoundHttpsOnly:
      // Only HTTPS is allowed by default.
      if (origin.scheme() == url::kHttpsScheme) {
        return true;
      }
      break;
    case DafsaResult::kFoundHttpOrHttps:
      // Allow either HTTP or HTTPS.
      if (origin.scheme() == url::kHttpScheme ||
          origin.scheme() == url::kHttpsScheme) {
        return true;
      }
      break;
    case DafsaResult::kNotFound:
      break;
  }

  // If we do not match then we should return false.
  return false;
}

MediaEngagementPreloadedList::DafsaResult
MediaEngagementPreloadedList::CheckStringIsPresent(
    const std::string& input) const {
  return static_cast<MediaEngagementPreloadedList::DafsaResult>(
      net::LookupStringInFixedSet(dafsa_, input));
}