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