Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / activity_actions.h
blob78dc40576a6d056ef6ddd63772fe62837c23426e
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
8 #include <string>
9 #include <vector>
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/extensions/api/activity_log_private.h"
15 #include "url/gurl.h"
17 namespace base {
18 class ListValue;
19 class DictionaryValue;
22 namespace rappor {
23 class RapporService;
26 namespace extensions {
28 // This is the interface for extension actions that are to be recorded in
29 // the activity log.
30 class Action : public base::RefCountedThreadSafe<Action> {
31 public:
32 // Types of log entries that can be stored. The numeric values are stored in
33 // the database, so keep them stable. Append values only.
34 enum ActionType {
35 ACTION_API_CALL = 0,
36 ACTION_API_EVENT = 1,
37 UNUSED_ACTION_API_BLOCKED = 2, // Not in use, but reserved for future.
38 ACTION_CONTENT_SCRIPT = 3,
39 ACTION_DOM_ACCESS = 4,
40 ACTION_DOM_EVENT = 5,
41 ACTION_WEB_REQUEST = 6,
42 ACTION_ANY = 1001, // Used for lookups of unspecified type.
45 // The type of ad injection an action performed. Do not delete or reorder
46 // these metrics, as they are used in histogramming.
47 enum InjectionType {
48 // No ad injection occurred.
49 NO_AD_INJECTION = 0,
50 // A new ad was injected.
51 INJECTION_NEW_AD,
52 // An ad was removed.
53 INJECTION_REMOVED_AD,
54 // An ad was replaced.
55 INJECTION_REPLACED_AD,
56 // Something occurred which heuristically looks like an ad injection, but we
57 // didn't categorize it as such (likely because we didn't recognize it as
58 // an ad network). If our list is effective, this should be significantly
59 // lower than the non-LIKELY counterparts.
60 INJECTION_LIKELY_NEW_AD,
61 INJECTION_LIKELY_REPLACED_AD,
63 // Place any new injection types above this entry.
64 NUM_INJECTION_TYPES
67 // The type of ad which was injected.
68 // Do not delete or reorder items in this enum, as it is used in
69 // histogramming.
70 enum AdType {
71 AD_TYPE_NONE,
72 AD_TYPE_IFRAME,
73 AD_TYPE_EMBED,
74 AD_TYPE_ANCHOR,
75 AD_TYPE_SCRIPT,
77 // Place any new injection types above this entry.
78 NUM_AD_TYPES
81 // A useful shorthand for methods that take or return collections of Action
82 // objects.
83 typedef std::vector<scoped_refptr<Action> > ActionVector;
85 // Creates a new activity log Action object. The extension_id and type
86 // fields are immutable. All other fields can be filled in with the
87 // accessors/mutators below.
88 Action(const std::string& extension_id,
89 const base::Time& time,
90 const ActionType action_type,
91 const std::string& api_name,
92 int64 action_id = -1);
94 // Creates and returns a mutable copy of an Action.
95 scoped_refptr<Action> Clone() const;
97 // Return the type of ad-injection performed in the |action|, or
98 // NO_AD_INJECTION if none was present.
99 // TODO(rdevlin.cronin): This isn't done.
100 // See crbug.com/357204.
101 InjectionType DidInjectAd(rappor::RapporService* rappor_service) const;
103 // The extension which caused this record to be generated.
104 const std::string& extension_id() const { return extension_id_; }
106 // The time the record was generated (or some approximation).
107 const base::Time& time() const { return time_; }
108 void set_time(const base::Time& time) { time_ = time; }
110 // The ActionType distinguishes different classes of actions that can be
111 // logged, and determines which other fields are expected to be filled in.
112 ActionType action_type() const { return action_type_; }
114 // The specific API call used or accessed, for example "chrome.tabs.get".
115 const std::string& api_name() const { return api_name_; }
116 void set_api_name(const std::string api_name) { api_name_ = api_name; }
118 // Any applicable arguments. This might be null to indicate no data
119 // available (a distinct condition from an empty argument list).
120 // mutable_args() returns a pointer to the list stored in the Action which
121 // can be modified in place; if the list was null an empty list is created
122 // first.
123 const base::ListValue* args() const { return args_.get(); }
124 void set_args(scoped_ptr<base::ListValue> args);
125 base::ListValue* mutable_args();
127 // The URL of the page which was modified or accessed.
128 const GURL& page_url() const { return page_url_; }
129 void set_page_url(const GURL& page_url);
131 // The title of the above page if available.
132 const std::string& page_title() const { return page_title_; }
133 void set_page_title(const std::string& title) { page_title_ = title; }
135 // A URL which appears in the arguments of the API call, if present.
136 const GURL& arg_url() const { return arg_url_; }
137 void set_arg_url(const GURL& arg_url);
139 // Get or set a flag indicating whether the page or argument values above
140 // refer to incognito pages.
141 bool page_incognito() const { return page_incognito_; }
142 void set_page_incognito(bool incognito) { page_incognito_ = incognito; }
143 bool arg_incognito() const { return arg_incognito_; }
144 void set_arg_incognito(bool incognito) { arg_incognito_ = incognito; }
146 // A dictionary where any additional data can be stored.
147 const base::DictionaryValue* other() const { return other_.get(); }
148 void set_other(scoped_ptr<base::DictionaryValue> other);
149 base::DictionaryValue* mutable_other();
151 // An ID that identifies an action stored in the Activity Log database. If the
152 // action is not retrieved from the database, e.g., live stream, then the ID
153 // is set to -1.
154 int64 action_id() const { return action_id_; }
156 // Helper methods for serializing and deserializing URLs into strings. If
157 // the URL is marked as incognito, then the string is prefixed with
158 // kIncognitoUrl ("<incognito>").
159 std::string SerializePageUrl() const;
160 void ParsePageUrl(const std::string& url);
161 std::string SerializeArgUrl() const;
162 void ParseArgUrl(const std::string& url);
164 // Number of merged records for this action.
165 int count() const { return count_; }
166 void set_count(int count) { count_ = count; }
168 // Flatten the activity's type-specific fields into an ExtensionActivity.
169 scoped_ptr<api::activity_log_private::ExtensionActivity>
170 ConvertToExtensionActivity();
172 // Print an action as a regular string for debugging purposes.
173 virtual std::string PrintForDebug() const;
175 protected:
176 virtual ~Action();
178 private:
179 friend class base::RefCountedThreadSafe<Action>;
181 // Returns true if a given |url| could be an ad.
182 bool UrlCouldBeAd(const GURL& url) const;
184 // Uploads the URL to RAPPOR (preserving privacy) if this might have been an
185 // ad injection.
186 void MaybeUploadUrl(rappor::RapporService* rappor_service) const;
188 // Checks an action that modified the src or href of an element for ad
189 // injection.
190 InjectionType CheckAttrModification() const;
191 // Checks an action that adds an element for ad injection.
192 InjectionType CheckElementAddition() const;
194 std::string extension_id_;
195 base::Time time_;
196 ActionType action_type_;
197 std::string api_name_;
198 scoped_ptr<base::ListValue> args_;
199 GURL page_url_;
200 std::string page_title_;
201 bool page_incognito_;
202 GURL arg_url_;
203 bool arg_incognito_;
204 scoped_ptr<base::DictionaryValue> other_;
205 int count_;
206 int64 action_id_;
208 DISALLOW_COPY_AND_ASSIGN(Action);
211 // A comparator for Action class objects; this performs a lexicographic
212 // comparison of the fields of the Action object (in an unspecfied order).
213 // This can be used to use Action objects as keys in STL containers.
214 struct ActionComparator {
215 // Evaluates the comparison lhs < rhs.
216 bool operator()(const scoped_refptr<Action>& lhs,
217 const scoped_refptr<Action>& rhs) const;
220 // Like ActionComparator, but ignores the time field and the action ID field in
221 // comparisons.
222 struct ActionComparatorExcludingTimeAndActionId {
223 // Evaluates the comparison lhs < rhs.
224 bool operator()(const scoped_refptr<Action>& lhs,
225 const scoped_refptr<Action>& rhs) const;
228 } // namespace extensions
230 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_