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"
19 class DictionaryValue
;
22 namespace extensions
{
24 // This is the interface for extension actions that are to be recorded in
26 class Action
: public base::RefCountedThreadSafe
<Action
> {
28 // Types of log entries that can be stored. The numeric values are stored in
29 // the database, so keep them stable. Append values only.
33 UNUSED_ACTION_API_BLOCKED
= 2, // Not in use, but reserved for future.
34 ACTION_CONTENT_SCRIPT
= 3,
35 ACTION_DOM_ACCESS
= 4,
37 ACTION_WEB_REQUEST
= 6,
38 ACTION_ANY
= 1001, // Used for lookups of unspecified type.
41 // A useful shorthand for methods that take or return collections of Action
43 typedef std::vector
<scoped_refptr
<Action
> > ActionVector
;
45 // Creates a new activity log Action object. The extension_id and type
46 // fields are immutable. All other fields can be filled in with the
47 // accessors/mutators below.
48 Action(const std::string
& extension_id
,
49 const base::Time
& time
,
50 const ActionType action_type
,
51 const std::string
& api_name
,
52 int64 action_id
= -1);
54 // Creates and returns a mutable copy of an Action.
55 scoped_refptr
<Action
> Clone() const;
57 // The extension which caused this record to be generated.
58 const std::string
& extension_id() const { return extension_id_
; }
60 // The time the record was generated (or some approximation).
61 const base::Time
& time() const { return time_
; }
62 void set_time(const base::Time
& time
) { time_
= time
; }
64 // The ActionType distinguishes different classes of actions that can be
65 // logged, and determines which other fields are expected to be filled in.
66 ActionType
action_type() const { return action_type_
; }
68 // The specific API call used or accessed, for example "chrome.tabs.get".
69 const std::string
& api_name() const { return api_name_
; }
70 void set_api_name(const std::string api_name
) { api_name_
= api_name
; }
72 // Any applicable arguments. This might be null to indicate no data
73 // available (a distinct condition from an empty argument list).
74 // mutable_args() returns a pointer to the list stored in the Action which
75 // can be modified in place; if the list was null an empty list is created
77 const base::ListValue
* args() const { return args_
.get(); }
78 void set_args(scoped_ptr
<base::ListValue
> args
);
79 base::ListValue
* mutable_args();
81 // The URL of the page which was modified or accessed.
82 const GURL
& page_url() const { return page_url_
; }
83 void set_page_url(const GURL
& page_url
);
85 // The title of the above page if available.
86 const std::string
& page_title() const { return page_title_
; }
87 void set_page_title(const std::string
& title
) { page_title_
= title
; }
89 // A URL which appears in the arguments of the API call, if present.
90 const GURL
& arg_url() const { return arg_url_
; }
91 void set_arg_url(const GURL
& arg_url
);
93 // Get or set a flag indicating whether the page or argument values above
94 // refer to incognito pages.
95 bool page_incognito() const { return page_incognito_
; }
96 void set_page_incognito(bool incognito
) { page_incognito_
= incognito
; }
97 bool arg_incognito() const { return arg_incognito_
; }
98 void set_arg_incognito(bool incognito
) { arg_incognito_
= incognito
; }
100 // A dictionary where any additional data can be stored.
101 const base::DictionaryValue
* other() const { return other_
.get(); }
102 void set_other(scoped_ptr
<base::DictionaryValue
> other
);
103 base::DictionaryValue
* mutable_other();
105 // An ID that identifies an action stored in the Activity Log database. If the
106 // action is not retrieved from the database, e.g., live stream, then the ID
108 int64
action_id() const { return action_id_
; }
110 // Helper methods for serializing and deserializing URLs into strings. If
111 // the URL is marked as incognito, then the string is prefixed with
112 // kIncognitoUrl ("<incognito>").
113 std::string
SerializePageUrl() const;
114 void ParsePageUrl(const std::string
& url
);
115 std::string
SerializeArgUrl() const;
116 void ParseArgUrl(const std::string
& url
);
118 // Number of merged records for this action.
119 int count() const { return count_
; }
120 void set_count(int count
) { count_
= count
; }
122 // Flatten the activity's type-specific fields into an ExtensionActivity.
123 scoped_ptr
<api::activity_log_private::ExtensionActivity
>
124 ConvertToExtensionActivity();
126 // Print an action as a regular string for debugging purposes.
127 virtual std::string
PrintForDebug() const;
133 friend class base::RefCountedThreadSafe
<Action
>;
135 std::string extension_id_
;
137 ActionType action_type_
;
138 std::string api_name_
;
139 scoped_ptr
<base::ListValue
> args_
;
141 std::string page_title_
;
142 bool page_incognito_
;
145 scoped_ptr
<base::DictionaryValue
> other_
;
149 DISALLOW_COPY_AND_ASSIGN(Action
);
152 // A comparator for Action class objects; this performs a lexicographic
153 // comparison of the fields of the Action object (in an unspecfied order).
154 // This can be used to use Action objects as keys in STL containers.
155 struct ActionComparator
{
156 // Evaluates the comparison lhs < rhs.
157 bool operator()(const scoped_refptr
<Action
>& lhs
,
158 const scoped_refptr
<Action
>& rhs
) const;
161 // Like ActionComparator, but ignores the time field and the action ID field in
163 struct ActionComparatorExcludingTimeAndActionId
{
164 // Evaluates the comparison lhs < rhs.
165 bool operator()(const scoped_refptr
<Action
>& lhs
,
166 const scoped_refptr
<Action
>& rhs
) const;
169 } // namespace extensions
171 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_