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 "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"
21 namespace extensions
{
23 // This is the interface for extension actions that are to be recorded in
25 class Action
: public base::RefCountedThreadSafe
<Action
> {
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.
32 UNUSED_ACTION_API_BLOCKED
= 2, // Not in use, but reserved for future.
33 ACTION_CONTENT_SCRIPT
= 3,
34 ACTION_DOM_ACCESS
= 4,
36 ACTION_WEB_REQUEST
= 6,
37 ACTION_ANY
= 1001, // Used for lookups of unspecified type.
40 // A useful shorthand for methods that take or return collections of Action
42 typedef std::vector
<scoped_refptr
<Action
> > ActionVector
;
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
);
52 // Creates and returns a mutable copy of an Action.
53 scoped_refptr
<Action
> Clone() const;
55 // The extension which caused this record to be generated.
56 const std::string
& extension_id() const { return extension_id_
; }
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
; }
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_
; }
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
; }
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
75 const base::ListValue
* args() const { return args_
.get(); }
76 void set_args(scoped_ptr
<base::ListValue
> args
);
77 base::ListValue
* mutable_args();
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
);
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
; }
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
);
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
; }
98 // A dictionary where any additional data can be stored.
99 const base::DictionaryValue
* other() const { return other_
.get(); }
100 void set_other(scoped_ptr
<base::DictionaryValue
> other
);
101 base::DictionaryValue
* mutable_other();
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
);
111 // Number of merged records for this action.
112 int count() const { return count_
; }
113 void set_count(int count
) { count_
= count
; }
115 // Flatten the activity's type-specific fields into an ExtensionActivity.
116 scoped_ptr
<api::activity_log_private::ExtensionActivity
>
117 ConvertToExtensionActivity();
119 // Print an action as a regular string for debugging purposes.
120 virtual std::string
PrintForDebug() const;
126 friend class base::RefCountedThreadSafe
<Action
>;
128 std::string extension_id_
;
130 ActionType action_type_
;
131 std::string api_name_
;
132 scoped_ptr
<base::ListValue
> args_
;
134 std::string page_title_
;
135 bool page_incognito_
;
138 scoped_ptr
<base::DictionaryValue
> other_
;
141 DISALLOW_COPY_AND_ASSIGN(Action
);
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;
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;
160 } // namespace extensions
162 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_