Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / activity_actions.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_
7
8 #include <string>
9 #include <vector>
10
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"
19
20 namespace base {
21 class ListValue;
22 class DictionaryValue;
23 }
24
25 namespace rappor {
26 class RapporService;
27 }
28
29 namespace extensions {
30
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.
46   };
47
48   // The type of ad injection an action performed.
49   enum InjectionType {
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.
55   };
56
57   // A useful shorthand for methods that take or return collections of Action
58   // objects.
59   typedef std::vector<scoped_refptr<Action> > ActionVector;
60
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);
69
70   // Creates and returns a mutable copy of an Action.
71   scoped_refptr<Action> Clone() const;
72
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;
78
79   // The extension which caused this record to be generated.
80   const std::string& extension_id() const { return extension_id_; }
81
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; }
85
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_; }
89
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; }
93
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
98   // first.
99   const base::ListValue* args() const { return args_.get(); }
100   void set_args(scoped_ptr<base::ListValue> args);
101   base::ListValue* mutable_args();
102
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);
106
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; }
110
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);
114
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; }
121
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();
126
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
129   // is set to -1.
130   int64 action_id() const { return action_id_; }
131
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);
139
140   // Number of merged records for this action.
141   int count() const { return count_; }
142   void set_count(int count) { count_ = count; }
143
144   // Flatten the activity's type-specific fields into an ExtensionActivity.
145   scoped_ptr<api::activity_log_private::ExtensionActivity>
146       ConvertToExtensionActivity();
147
148   // Print an action as a regular string for debugging purposes.
149   virtual std::string PrintForDebug() const;
150
151  protected:
152   virtual ~Action();
153
154  private:
155   friend class base::RefCountedThreadSafe<Action>;
156
157   // Uploads the URL to RAPPOR (preserving privacy) if this might have been an
158   // ad injection.
159   void MaybeUploadUrl(rappor::RapporService* rappor_service) const;
160
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;
165
166   std::string extension_id_;
167   base::Time time_;
168   ActionType action_type_;
169   std::string api_name_;
170   scoped_ptr<base::ListValue> args_;
171   GURL page_url_;
172   std::string page_title_;
173   bool page_incognito_;
174   GURL arg_url_;
175   bool arg_incognito_;
176   scoped_ptr<base::DictionaryValue> other_;
177   int count_;
178   int64 action_id_;
179
180   DISALLOW_COPY_AND_ASSIGN(Action);
181 };
182
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;
190 };
191
192 // Like ActionComparator, but ignores the time field and the action ID field in
193 // comparisons.
194 struct ActionComparatorExcludingTimeAndActionId {
195   // Evaluates the comparison lhs < rhs.
196   bool operator()(const scoped_refptr<Action>& lhs,
197                   const scoped_refptr<Action>& rhs) const;
198 };
199
200 }  // namespace extensions
201
202 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_