Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/browser/profiles/profile.h" | |
| 12 #include "chrome/common/extensions/api/activity_log_private.h" | 13 #include "chrome/common/extensions/api/activity_log_private.h" |
| 13 #include "sql/connection.h" | 14 #include "sql/connection.h" |
| 14 #include "sql/statement.h" | 15 #include "sql/statement.h" |
| 15 #include "sql/transaction.h" | 16 #include "sql/transaction.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 // This is the interface for extension actions that are to be recorded in | 21 // This is the interface for extension actions that are to be recorded in |
| 21 // the activity log. | 22 // the activity log. |
| 22 class Action : public base::RefCountedThreadSafe<Action> { | 23 class Action : public base::RefCountedThreadSafe<Action> { |
| 23 public: | 24 public: |
| 24 // Types of log entries that can be stored. The numeric values are stored in | 25 // Types of log entries that can be stored. The numeric values are stored in |
| 25 // the database, so keep them stable. Append values only. | 26 // the database, so keep them stable. Append values only. |
| 26 enum ActionType { | 27 enum ActionType { |
| 27 ACTION_API_CALL = 0, | 28 ACTION_API_CALL = 0, |
| 28 ACTION_API_EVENT = 1, | 29 ACTION_API_EVENT = 1, |
| 29 ACTION_API_BLOCKED = 2, | 30 ACTION_API_BLOCKED = 2, |
| 30 ACTION_CONTENT_SCRIPT = 3, | 31 ACTION_CONTENT_SCRIPT = 3, |
| 31 ACTION_DOM_ACCESS = 4, | 32 ACTION_DOM_ACCESS = 4, |
| 32 ACTION_DOM_EVENT = 5, | 33 ACTION_DOM_EVENT = 5, |
| 33 ACTION_DOM_XHR = 6, | 34 ACTION_DOM_XHR = 6, |
| 34 ACTION_WEB_REQUEST = 7, | 35 ACTION_WEB_REQUEST = 7, |
| 35 }; | 36 }; |
| 36 | 37 |
| 38 // Creates a new activity log Action object. The extension_id, time, and | |
| 39 // type are immutable. All other fields can be filled in with the | |
| 40 // accessors/mutators below. | |
| 41 Action(const std::string& extension_id, | |
| 42 const base::Time& time, | |
| 43 const ActionType action_type); | |
| 44 | |
| 45 // The extension which caused this record to be generated. | |
| 46 const std::string& extension_id() const { return extension_id_; } | |
| 47 | |
| 48 // The time the record was generated (or some approximation). | |
| 49 const base::Time& time() const { return time_; } | |
| 50 | |
| 51 // The ActionType distinguishes different classes of actions that can be | |
| 52 // logged, and determines which other fields are expected to filled in. | |
| 53 ActionType action_type() const { return action_type_; } | |
| 54 | |
| 55 // The specific API call used or accessed, for example "chrome.tabs.get". | |
| 56 const std::string& api_name() const { return api_name_; } | |
| 57 void set_api_name(const std::string api_name) { api_name_ = api_name; } | |
|
felt
2013/07/23 17:07:17
Why are these all setters instead of constructors?
mvrable
2013/07/23 18:16:56
Mostly to handle optional fields and to avoid havi
felt
2013/07/23 19:06:33
api_name is sometimes null but usually not. Maybe
mvrable
2013/07/23 19:17:55
I'll add the API name to the constructor--patch wi
| |
| 58 | |
| 59 // Any applicable arguments. This might be null to indicate no data | |
| 60 // available (a distinct condition from an empty argument list). | |
| 61 // mutable_args() returns a pointer to the list stored in the Action which | |
| 62 // can be modified in place; if the list was null an empty list is created | |
| 63 // first. | |
| 64 const ListValue* args() const { return args_.get(); } | |
| 65 void set_args(scoped_ptr<ListValue> args); | |
| 66 ListValue* mutable_args(); | |
| 67 | |
| 68 // The URL of the page which was modified or accessed. | |
| 69 const GURL& page_url() const { return page_url_; } | |
| 70 void set_page_url(const GURL& page_url); | |
| 71 | |
| 72 // The title of the above page if available. | |
| 73 const std::string& page_title() const { return page_title_; } | |
| 74 void set_page_title(const std::string& title) { page_title_ = title; } | |
| 75 | |
| 76 // A URL which appears in the arguments of the API call, if present. | |
| 77 const GURL& arg_url() const { return arg_url_; } | |
| 78 void set_arg_url(const GURL& arg_url); | |
| 79 | |
| 80 // A dictionary where any additional data can be stored. | |
| 81 const DictionaryValue* other() const { return other_.get(); } | |
| 82 void set_other(scoped_ptr<DictionaryValue> other); | |
| 83 DictionaryValue* mutable_other(); | |
| 84 | |
| 37 // Record the action in the database. | 85 // Record the action in the database. |
| 38 virtual bool Record(sql::Connection* db) = 0; | 86 bool Record(sql::Connection* db); |
| 39 | 87 |
| 40 // Flatten the activity's type-specific fields into an ExtensionActivity. | 88 // Flatten the activity's type-specific fields into an ExtensionActivity. |
| 41 virtual scoped_ptr<api::activity_log_private::ExtensionActivity> | 89 scoped_ptr<api::activity_log_private::ExtensionActivity> |
| 42 ConvertToExtensionActivity() = 0; | 90 ConvertToExtensionActivity(); |
| 43 | 91 |
| 44 // Print an action as a regular string for debugging purposes. | 92 // Print an action as a regular string for debugging purposes. |
| 45 virtual std::string PrintForDebug() = 0; | 93 std::string PrintForDebug(); |
| 46 | |
| 47 const std::string& extension_id() const { return extension_id_; } | |
| 48 const base::Time& time() const { return time_; } | |
| 49 api::activity_log_private::ExtensionActivity::ActivityType activity_type() | |
| 50 const { return activity_type_; } | |
| 51 | 94 |
| 52 protected: | 95 protected: |
| 53 Action(const std::string& extension_id, | 96 ~Action(); |
| 54 const base::Time& time, | |
| 55 api::activity_log_private::ExtensionActivity::ActivityType type); | |
| 56 virtual ~Action() {} | |
| 57 | 97 |
| 58 private: | 98 private: |
| 59 friend class base::RefCountedThreadSafe<Action>; | 99 friend class base::RefCountedThreadSafe<Action>; |
| 60 | 100 |
| 61 std::string extension_id_; | 101 std::string extension_id_; |
| 62 base::Time time_; | 102 base::Time time_; |
| 63 api::activity_log_private::ExtensionActivity::ActivityType activity_type_; | 103 api::activity_log_private::ExtensionActivity::ActivityType activity_type_; |
| 104 ActionType action_type_; | |
| 105 std::string api_name_; | |
| 106 scoped_ptr<ListValue> args_; | |
| 107 GURL page_url_; | |
| 108 std::string page_title_; | |
| 109 GURL arg_url_; | |
| 110 scoped_ptr<DictionaryValue> other_; | |
| 64 | 111 |
| 65 DISALLOW_COPY_AND_ASSIGN(Action); | 112 DISALLOW_COPY_AND_ASSIGN(Action); |
| 66 }; | 113 }; |
| 67 | 114 |
| 68 // TODO(mvrable): This is a temporary class used to represent Actions which | 115 // Constants defined for various action types. |
| 69 // have been loaded from the SQLite database. Soon the entire Action hierarchy | 116 // TODO(mvrable): These are here for compatibility but should be moved |
| 70 // will be flattened out as the type-specific classes are eliminated, at which | 117 // elsewhere as cleanup progresses. |
| 71 // time some of the logic here will be moved. | 118 class APIAction { |
| 72 class WatchdogAction : public Action { | |
| 73 public: | 119 public: |
| 74 WatchdogAction(const std::string& extension_id, | 120 // These values should not be changed. Append any additional values to the |
| 75 const base::Time& time, | 121 // end with sequential numbers. |
| 76 const ActionType action_type, | 122 enum Type { |
| 77 const std::string& api_name, // full method name | 123 CALL = 0, |
| 78 scoped_ptr<ListValue> args, // the argument list | 124 EVENT_CALLBACK = 1, |
| 79 const GURL& page_url, // page the action occurred on | 125 UNKNOWN_TYPE = 2, |
| 80 const GURL& arg_url, // URL extracted from the argument list | 126 }; |
| 81 scoped_ptr<DictionaryValue> other); // any extra logging info | |
| 82 | 127 |
| 83 virtual bool Record(sql::Connection* db) OVERRIDE; | 128 static const char* kAlwaysLog[]; |
| 84 virtual scoped_ptr<api::activity_log_private::ExtensionActivity> | 129 static const int kSizeAlwaysLog; |
| 85 ConvertToExtensionActivity() OVERRIDE; | |
| 86 virtual std::string PrintForDebug() OVERRIDE; | |
| 87 | 130 |
| 88 protected: | 131 static const char* kIncognitoUrl; |
| 89 virtual ~WatchdogAction(); | 132 |
| 133 // Used to associate tab IDs with URLs. It will swap out the int in args with | |
| 134 // a URL as a string. If the tab is in incognito mode, we leave it alone as | |
| 135 // the original int. There is a small chance that the URL translation could | |
| 136 // be wrong, if the tab has already been navigated by the time of invocation. | |
| 137 static void LookupTabId(const std::string& api_call, | |
| 138 base::ListValue* args, | |
| 139 Profile* profile); | |
| 90 | 140 |
| 91 private: | 141 private: |
| 92 ActionType action_type_; | 142 DISALLOW_IMPLICIT_CONSTRUCTORS(APIAction); |
| 93 std::string api_name_; | 143 }; |
| 94 scoped_ptr<ListValue> args_; | 144 |
| 95 GURL page_url_; | 145 class BlockedAction { |
| 96 GURL arg_url_; | 146 public: |
| 97 scoped_ptr<DictionaryValue> other_; | 147 // These values should not be changed. Append any additional values to the |
| 148 // end with sequential numbers. | |
| 149 enum Reason { | |
| 150 UNKNOWN = 0, | |
| 151 ACCESS_DENIED = 1, | |
| 152 QUOTA_EXCEEDED = 2, | |
| 153 }; | |
| 154 | |
| 155 private: | |
| 156 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockedAction); | |
| 98 }; | 157 }; |
| 99 | 158 |
| 100 } // namespace extensions | 159 } // namespace extensions |
| 101 | 160 |
| 102 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ | 161 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ |
| 103 | |
| OLD | NEW |