File: activity_actions.h

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 (172 lines) | stat: -rw-r--r-- 6,552 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
#define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_

#include <stdint.h>

#include <string>
#include <vector>

#include "base/memory/ref_counted_memory.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/common/extensions/api/activity_log_private.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_id.h"
#include "url/gurl.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

namespace extensions {

// This is the interface for extension actions that are to be recorded in
// the activity log.
class Action : public base::RefCountedThreadSafe<Action> {
 public:
  // Types of log entries that can be stored.  The numeric values are stored in
  // the database, so keep them stable.  Append values only.
  enum ActionType {
    ACTION_API_CALL = 0,
    ACTION_API_EVENT = 1,
    UNUSED_ACTION_API_BLOCKED = 2,  // Not in use, but reserved for future.
    ACTION_CONTENT_SCRIPT = 3,
    ACTION_DOM_ACCESS = 4,
    ACTION_DOM_EVENT = 5,
    ACTION_WEB_REQUEST = 6,
    ACTION_ANY = 1001,              // Used for lookups of unspecified type.
  };

  // A useful shorthand for methods that take or return collections of Action
  // objects.
  typedef std::vector<scoped_refptr<Action> > ActionVector;

  // Creates a new activity log Action object.  The extension_id and type
  // fields are immutable.  All other fields can be filled in with the
  // accessors/mutators below.
  Action(const std::string& extension_id,
         const base::Time& time,
         const ActionType action_type,
         const std::string& api_name,
         int64_t action_id = -1);

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

  // Creates and returns a mutable copy of an Action.
  scoped_refptr<Action> Clone() const;

  // The extension which caused this record to be generated.
  const ExtensionId& extension_id() const { return extension_id_; }

  // The time the record was generated (or some approximation).
  const base::Time& time() const { return time_; }
  void set_time(const base::Time& time) { time_ = time; }

  // The ActionType distinguishes different classes of actions that can be
  // logged, and determines which other fields are expected to be filled in.
  ActionType action_type() const { return action_type_; }

  // The specific API call used or accessed, for example "chrome.tabs.get".
  const std::string& api_name() const { return api_name_; }
  void set_api_name(const std::string& api_name) { api_name_ = api_name; }

  // Any applicable arguments.  This might be null to indicate no data
  // available (a distinct condition from an empty argument list).
  // mutable_args() returns a pointer to the list stored in the Action which
  // can be modified in place; if the list was null an empty list is created
  // first.
  const std::optional<base::Value::List>& args() const { return args_; }
  void set_args(std::optional<base::Value::List> args);
  base::Value::List& mutable_args();

  // The URL of the page which was modified or accessed.
  const GURL& page_url() const { return page_url_; }
  void set_page_url(const GURL& page_url);

  // The title of the above page if available.
  const std::string& page_title() const { return page_title_; }
  void set_page_title(const std::string& title) { page_title_ = title; }

  // A URL which appears in the arguments of the API call, if present.
  const GURL& arg_url() const { return arg_url_; }
  void set_arg_url(const GURL& arg_url);

  // Get or set a flag indicating whether the page or argument values above
  // refer to incognito pages.
  bool page_incognito() const { return page_incognito_; }
  void set_page_incognito(bool incognito) { page_incognito_ = incognito; }
  bool arg_incognito() const { return arg_incognito_; }
  void set_arg_incognito(bool incognito) { arg_incognito_ = incognito; }

  // A dictionary where any additional data can be stored.
  const std::optional<base::Value::Dict>& other() const { return other_; }
  void set_other(std::optional<base::Value::Dict> other);
  base::Value::Dict& mutable_other();

  // An ID that identifies an action stored in the Activity Log database. If the
  // action is not retrieved from the database, e.g., live stream, then the ID
  // is set to -1.
  int64_t action_id() const { return action_id_; }

  // Helper methods for serializing and deserializing URLs into strings.  If
  // the URL is marked as incognito, then the string is prefixed with
  // kIncognitoUrl ("<incognito>").
  std::string SerializePageUrl() const;
  void ParsePageUrl(const std::string& url);
  std::string SerializeArgUrl() const;
  void ParseArgUrl(const std::string& url);

  // Number of merged records for this action.
  int count() const { return count_; }
  void set_count(int count) { count_ = count; }

  // Flatten the activity's type-specific fields into an ExtensionActivity.
  api::activity_log_private::ExtensionActivity ConvertToExtensionActivity();

  // Print an action as a regular string for debugging purposes.
  virtual std::string PrintForDebug() const;

 protected:
  virtual ~Action();

 private:
  friend class base::RefCountedThreadSafe<Action>;

  ExtensionId extension_id_;
  base::Time time_;
  ActionType action_type_;
  std::string api_name_;
  std::optional<base::Value::List> args_;
  GURL page_url_;
  std::string page_title_;
  bool page_incognito_{false};
  GURL arg_url_;
  bool arg_incognito_{false};
  std::optional<base::Value::Dict> other_;
  int count_{0};
  int64_t action_id_;
};

// A comparator for Action class objects; this performs a lexicographic
// comparison of the fields of the Action object (in an unspecfied order).
// This can be used to use Action objects as keys in STL containers.
struct ActionComparator {
  // Evaluates the comparison lhs < rhs.
  bool operator()(const scoped_refptr<Action>& lhs,
                  const scoped_refptr<Action>& rhs) const;
};

// Like ActionComparator, but ignores the time field and the action ID field in
// comparisons.
struct ActionComparatorExcludingTimeAndActionId {
  // Evaluates the comparison lhs < rhs.
  bool operator()(const scoped_refptr<Action>& lhs,
                  const scoped_refptr<Action>& rhs) const;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_