- add sources.
[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 "base/values.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/extensions/api/activity_log_private.h"
16 #include "sql/connection.h"
17 #include "sql/statement.h"
18 #include "sql/transaction.h"
19 #include "url/gurl.h"
20
21 namespace extensions {
22
23 // This is the interface for extension actions that are to be recorded in
24 // the activity log.
25 class Action : public base::RefCountedThreadSafe<Action> {
26  public:
27   // Types of log entries that can be stored.  The numeric values are stored in
28   // the database, so keep them stable.  Append values only.
29   enum ActionType {
30     ACTION_API_CALL = 0,
31     ACTION_API_EVENT = 1,
32     UNUSED_ACTION_API_BLOCKED = 2,  // Not in use, but reserved for future.
33     ACTION_CONTENT_SCRIPT = 3,
34     ACTION_DOM_ACCESS = 4,
35     ACTION_DOM_EVENT = 5,
36     ACTION_WEB_REQUEST = 6,
37     ACTION_ANY = 1001,              // Used for lookups of unspecified type.
38   };
39
40   // A useful shorthand for methods that take or return collections of Action
41   // objects.
42   typedef std::vector<scoped_refptr<Action> > ActionVector;
43
44   // Creates a new activity log Action object.  The extension_id and type
45   // fields are immutable.  All other fields can be filled in with the
46   // accessors/mutators below.
47   Action(const std::string& extension_id,
48          const base::Time& time,
49          const ActionType action_type,
50          const std::string& api_name);
51
52   // Creates and returns a mutable copy of an Action.
53   scoped_refptr<Action> Clone() const;
54
55   // The extension which caused this record to be generated.
56   const std::string& extension_id() const { return extension_id_; }
57
58   // The time the record was generated (or some approximation).
59   const base::Time& time() const { return time_; }
60   void set_time(const base::Time& time) { time_ = time; }
61
62   // The ActionType distinguishes different classes of actions that can be
63   // logged, and determines which other fields are expected to be filled in.
64   ActionType action_type() const { return action_type_; }
65
66   // The specific API call used or accessed, for example "chrome.tabs.get".
67   const std::string& api_name() const { return api_name_; }
68   void set_api_name(const std::string api_name) { api_name_ = api_name; }
69
70   // Any applicable arguments.  This might be null to indicate no data
71   // available (a distinct condition from an empty argument list).
72   // mutable_args() returns a pointer to the list stored in the Action which
73   // can be modified in place; if the list was null an empty list is created
74   // first.
75   const ListValue* args() const { return args_.get(); }
76   void set_args(scoped_ptr<ListValue> args);
77   ListValue* mutable_args();
78
79   // The URL of the page which was modified or accessed.
80   const GURL& page_url() const { return page_url_; }
81   void set_page_url(const GURL& page_url);
82
83   // The title of the above page if available.
84   const std::string& page_title() const { return page_title_; }
85   void set_page_title(const std::string& title) { page_title_ = title; }
86
87   // A URL which appears in the arguments of the API call, if present.
88   const GURL& arg_url() const { return arg_url_; }
89   void set_arg_url(const GURL& arg_url);
90
91   // Get or set a flag indicating whether the page or argument values above
92   // refer to incognito pages.
93   bool page_incognito() const { return page_incognito_; }
94   void set_page_incognito(bool incognito) { page_incognito_ = incognito; }
95   bool arg_incognito() const { return arg_incognito_; }
96   void set_arg_incognito(bool incognito) { arg_incognito_ = incognito; }
97
98   // A dictionary where any additional data can be stored.
99   const DictionaryValue* other() const { return other_.get(); }
100   void set_other(scoped_ptr<DictionaryValue> other);
101   DictionaryValue* mutable_other();
102
103   // Helper methods for serializing and deserializing URLs into strings.  If
104   // the URL is marked as incognito, then the string is prefixed with
105   // kIncognitoUrl ("<incognito>").
106   std::string SerializePageUrl() const;
107   void ParsePageUrl(const std::string& url);
108   std::string SerializeArgUrl() const;
109   void ParseArgUrl(const std::string& url);
110
111   // Number of merged records for this action.
112   int count() const { return count_; }
113   void set_count(int count) { count_ = count; }
114
115   // Flatten the activity's type-specific fields into an ExtensionActivity.
116   scoped_ptr<api::activity_log_private::ExtensionActivity>
117       ConvertToExtensionActivity();
118
119   // Print an action as a regular string for debugging purposes.
120   virtual std::string PrintForDebug() const;
121
122  protected:
123   virtual ~Action();
124
125  private:
126   friend class base::RefCountedThreadSafe<Action>;
127
128   std::string extension_id_;
129   base::Time time_;
130   ActionType action_type_;
131   std::string api_name_;
132   scoped_ptr<ListValue> args_;
133   GURL page_url_;
134   std::string page_title_;
135   bool page_incognito_;
136   GURL arg_url_;
137   bool arg_incognito_;
138   scoped_ptr<DictionaryValue> other_;
139   int count_;
140
141   DISALLOW_COPY_AND_ASSIGN(Action);
142 };
143
144 // A comparator for Action class objects; this performs a lexicographic
145 // comparison of the fields of the Action object (in an unspecfied order).
146 // This can be used to use Action objects as keys in STL containers.
147 struct ActionComparator {
148   // Evaluates the comparison lhs < rhs.
149   bool operator()(const scoped_refptr<Action>& lhs,
150                   const scoped_refptr<Action>& rhs) const;
151 };
152
153 // Like ActionComparator, but ignores the time field in comparisons.
154 struct ActionComparatorExcludingTime {
155   // Evaluates the comparison lhs < rhs.
156   bool operator()(const scoped_refptr<Action>& lhs,
157                   const scoped_refptr<Action>& rhs) const;
158 };
159
160 }  // namespace extensions
161
162 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_