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_
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"
22 class DictionaryValue;
29 namespace extensions {
31 // This is the interface for extension actions that are to be recorded in
33 class Action : public base::RefCountedThreadSafe<Action> {
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.
40 UNUSED_ACTION_API_BLOCKED = 2, // Not in use, but reserved for future.
41 ACTION_CONTENT_SCRIPT = 3,
42 ACTION_DOM_ACCESS = 4,
44 ACTION_WEB_REQUEST = 6,
45 ACTION_ANY = 1001, // Used for lookups of unspecified type.
48 // The type of ad injection an action performed.
50 NO_AD_INJECTION = 0, // No ad injection occurred.
51 INJECTION_NEW_AD, // A new ad was injected.
52 INJECTION_REMOVED_AD, // An ad was removed.
53 INJECTION_REPLACED_AD, // An ad was replaced.
54 NUM_INJECTION_TYPES // Place any new injection types above this entry.
57 // A useful shorthand for methods that take or return collections of Action
59 typedef std::vector<scoped_refptr<Action> > ActionVector;
61 // Creates a new activity log Action object. The extension_id and type
62 // fields are immutable. All other fields can be filled in with the
63 // accessors/mutators below.
64 Action(const std::string& extension_id,
65 const base::Time& time,
66 const ActionType action_type,
67 const std::string& api_name,
68 int64 action_id = -1);
70 // Creates and returns a mutable copy of an Action.
71 scoped_refptr<Action> Clone() const;
73 // Return the type of ad-injection performed in the |action|, or
74 // NO_AD_INJECTION if none was present.
75 // TODO(rdevlin.cronin): This isn't done.
76 // See crbug.com/357204.
77 InjectionType DidInjectAd(rappor::RapporService* rappor_service) const;
79 // The extension which caused this record to be generated.
80 const std::string& extension_id() const { return extension_id_; }
82 // The time the record was generated (or some approximation).
83 const base::Time& time() const { return time_; }
84 void set_time(const base::Time& time) { time_ = time; }
86 // The ActionType distinguishes different classes of actions that can be
87 // logged, and determines which other fields are expected to be filled in.
88 ActionType action_type() const { return action_type_; }
90 // The specific API call used or accessed, for example "chrome.tabs.get".
91 const std::string& api_name() const { return api_name_; }
92 void set_api_name(const std::string api_name) { api_name_ = api_name; }
94 // Any applicable arguments. This might be null to indicate no data
95 // available (a distinct condition from an empty argument list).
96 // mutable_args() returns a pointer to the list stored in the Action which
97 // can be modified in place; if the list was null an empty list is created
99 const base::ListValue* args() const { return args_.get(); }
100 void set_args(scoped_ptr<base::ListValue> args);
101 base::ListValue* mutable_args();
103 // The URL of the page which was modified or accessed.
104 const GURL& page_url() const { return page_url_; }
105 void set_page_url(const GURL& page_url);
107 // The title of the above page if available.
108 const std::string& page_title() const { return page_title_; }
109 void set_page_title(const std::string& title) { page_title_ = title; }
111 // A URL which appears in the arguments of the API call, if present.
112 const GURL& arg_url() const { return arg_url_; }
113 void set_arg_url(const GURL& arg_url);
115 // Get or set a flag indicating whether the page or argument values above
116 // refer to incognito pages.
117 bool page_incognito() const { return page_incognito_; }
118 void set_page_incognito(bool incognito) { page_incognito_ = incognito; }
119 bool arg_incognito() const { return arg_incognito_; }
120 void set_arg_incognito(bool incognito) { arg_incognito_ = incognito; }
122 // A dictionary where any additional data can be stored.
123 const base::DictionaryValue* other() const { return other_.get(); }
124 void set_other(scoped_ptr<base::DictionaryValue> other);
125 base::DictionaryValue* mutable_other();
127 // An ID that identifies an action stored in the Activity Log database. If the
128 // action is not retrieved from the database, e.g., live stream, then the ID
130 int64 action_id() const { return action_id_; }
132 // Helper methods for serializing and deserializing URLs into strings. If
133 // the URL is marked as incognito, then the string is prefixed with
134 // kIncognitoUrl ("<incognito>").
135 std::string SerializePageUrl() const;
136 void ParsePageUrl(const std::string& url);
137 std::string SerializeArgUrl() const;
138 void ParseArgUrl(const std::string& url);
140 // Number of merged records for this action.
141 int count() const { return count_; }
142 void set_count(int count) { count_ = count; }
144 // Flatten the activity's type-specific fields into an ExtensionActivity.
145 scoped_ptr<api::activity_log_private::ExtensionActivity>
146 ConvertToExtensionActivity();
148 // Print an action as a regular string for debugging purposes.
149 virtual std::string PrintForDebug() const;
155 friend class base::RefCountedThreadSafe<Action>;
157 // Uploads the URL to RAPPOR (preserving privacy) if this might have been an
159 void MaybeUploadUrl(rappor::RapporService* rappor_service) const;
161 // Checks an action with the appendChild API for ad injection.
162 InjectionType CheckAppendChild() const;
163 // Checks an action that modified the src of an element for ad injection.
164 InjectionType CheckSrcModification() const;
166 std::string extension_id_;
168 ActionType action_type_;
169 std::string api_name_;
170 scoped_ptr<base::ListValue> args_;
172 std::string page_title_;
173 bool page_incognito_;
176 scoped_ptr<base::DictionaryValue> other_;
180 DISALLOW_COPY_AND_ASSIGN(Action);
183 // A comparator for Action class objects; this performs a lexicographic
184 // comparison of the fields of the Action object (in an unspecfied order).
185 // This can be used to use Action objects as keys in STL containers.
186 struct ActionComparator {
187 // Evaluates the comparison lhs < rhs.
188 bool operator()(const scoped_refptr<Action>& lhs,
189 const scoped_refptr<Action>& rhs) const;
192 // Like ActionComparator, but ignores the time field and the action ID field in
194 struct ActionComparatorExcludingTimeAndActionId {
195 // Evaluates the comparison lhs < rhs.
196 bool operator()(const scoped_refptr<Action>& lhs,
197 const scoped_refptr<Action>& rhs) const;
200 } // namespace extensions
202 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_