| 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 const std::string& api_name); |
| 45 |
| 46 // The extension which caused this record to be generated. |
| 47 const std::string& extension_id() const { return extension_id_; } |
| 48 |
| 49 // The time the record was generated (or some approximation). |
| 50 const base::Time& time() const { return time_; } |
| 51 |
| 52 // The ActionType distinguishes different classes of actions that can be |
| 53 // logged, and determines which other fields are expected to be filled in. |
| 54 ActionType action_type() const { return action_type_; } |
| 55 |
| 56 // The specific API call used or accessed, for example "chrome.tabs.get". |
| 57 const std::string& api_name() const { return api_name_; } |
| 58 void set_api_name(const std::string api_name) { api_name_ = api_name; } |
| 59 |
| 60 // Any applicable arguments. This might be null to indicate no data |
| 61 // available (a distinct condition from an empty argument list). |
| 62 // mutable_args() returns a pointer to the list stored in the Action which |
| 63 // can be modified in place; if the list was null an empty list is created |
| 64 // first. |
| 65 const ListValue* args() const { return args_.get(); } |
| 66 void set_args(scoped_ptr<ListValue> args); |
| 67 ListValue* mutable_args(); |
| 68 |
| 69 // The URL of the page which was modified or accessed. |
| 70 const GURL& page_url() const { return page_url_; } |
| 71 void set_page_url(const GURL& page_url); |
| 72 |
| 73 // The title of the above page if available. |
| 74 const std::string& page_title() const { return page_title_; } |
| 75 void set_page_title(const std::string& title) { page_title_ = title; } |
| 76 |
| 77 // A URL which appears in the arguments of the API call, if present. |
| 78 const GURL& arg_url() const { return arg_url_; } |
| 79 void set_arg_url(const GURL& arg_url); |
| 80 |
| 81 // A dictionary where any additional data can be stored. |
| 82 const DictionaryValue* other() const { return other_.get(); } |
| 83 void set_other(scoped_ptr<DictionaryValue> other); |
| 84 DictionaryValue* mutable_other(); |
| 85 |
| 37 // Record the action in the database. | 86 // Record the action in the database. |
| 38 virtual bool Record(sql::Connection* db) = 0; | 87 bool Record(sql::Connection* db); |
| 39 | 88 |
| 40 // Flatten the activity's type-specific fields into an ExtensionActivity. | 89 // Flatten the activity's type-specific fields into an ExtensionActivity. |
| 41 virtual scoped_ptr<api::activity_log_private::ExtensionActivity> | 90 scoped_ptr<api::activity_log_private::ExtensionActivity> |
| 42 ConvertToExtensionActivity() = 0; | 91 ConvertToExtensionActivity(); |
| 43 | 92 |
| 44 // Print an action as a regular string for debugging purposes. | 93 // Print an action as a regular string for debugging purposes. |
| 45 virtual std::string PrintForDebug() = 0; | 94 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 | 95 |
| 52 protected: | 96 protected: |
| 53 Action(const std::string& extension_id, | 97 virtual ~Action(); |
| 54 const base::Time& time, | |
| 55 api::activity_log_private::ExtensionActivity::ActivityType type); | |
| 56 virtual ~Action() {} | |
| 57 | 98 |
| 58 private: | 99 private: |
| 59 friend class base::RefCountedThreadSafe<Action>; | 100 friend class base::RefCountedThreadSafe<Action>; |
| 60 | 101 |
| 61 std::string extension_id_; | 102 std::string extension_id_; |
| 62 base::Time time_; | 103 base::Time time_; |
| 63 api::activity_log_private::ExtensionActivity::ActivityType activity_type_; | 104 api::activity_log_private::ExtensionActivity::ActivityType activity_type_; |
| 105 ActionType action_type_; |
| 106 std::string api_name_; |
| 107 scoped_ptr<ListValue> args_; |
| 108 GURL page_url_; |
| 109 std::string page_title_; |
| 110 GURL arg_url_; |
| 111 scoped_ptr<DictionaryValue> other_; |
| 64 | 112 |
| 65 DISALLOW_COPY_AND_ASSIGN(Action); | 113 DISALLOW_COPY_AND_ASSIGN(Action); |
| 66 }; | 114 }; |
| 67 | 115 |
| 68 // TODO(mvrable): This is a temporary class used to represent Actions which | 116 // Constants defined for various action types. |
| 69 // have been loaded from the SQLite database. Soon the entire Action hierarchy | 117 // 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 | 118 // elsewhere as cleanup progresses. |
| 71 // time some of the logic here will be moved. | 119 class APIAction { |
| 72 class WatchdogAction : public Action { | |
| 73 public: | 120 public: |
| 74 WatchdogAction(const std::string& extension_id, | 121 // These values should not be changed. Append any additional values to the |
| 75 const base::Time& time, | 122 // end with sequential numbers. |
| 76 const ActionType action_type, | 123 enum Type { |
| 77 const std::string& api_name, // full method name | 124 CALL = 0, |
| 78 scoped_ptr<ListValue> args, // the argument list | 125 EVENT_CALLBACK = 1, |
| 79 const GURL& page_url, // page the action occurred on | 126 UNKNOWN_TYPE = 2, |
| 80 const GURL& arg_url, // URL extracted from the argument list | 127 }; |
| 81 scoped_ptr<DictionaryValue> other); // any extra logging info | |
| 82 | 128 |
| 83 virtual bool Record(sql::Connection* db) OVERRIDE; | 129 static const char* kAlwaysLog[]; |
| 84 virtual scoped_ptr<api::activity_log_private::ExtensionActivity> | 130 static const int kSizeAlwaysLog; |
| 85 ConvertToExtensionActivity() OVERRIDE; | |
| 86 virtual std::string PrintForDebug() OVERRIDE; | |
| 87 | 131 |
| 88 protected: | 132 static const char* kIncognitoUrl; |
| 89 virtual ~WatchdogAction(); | 133 |
| 134 // Used to associate tab IDs with URLs. It will swap out the int in args with |
| 135 // a URL as a string. If the tab is in incognito mode, we leave it alone as |
| 136 // the original int. There is a small chance that the URL translation could |
| 137 // be wrong, if the tab has already been navigated by the time of invocation. |
| 138 static void LookupTabId(const std::string& api_call, |
| 139 base::ListValue* args, |
| 140 Profile* profile); |
| 90 | 141 |
| 91 private: | 142 private: |
| 92 ActionType action_type_; | 143 DISALLOW_IMPLICIT_CONSTRUCTORS(APIAction); |
| 93 std::string api_name_; | 144 }; |
| 94 scoped_ptr<ListValue> args_; | 145 |
| 95 GURL page_url_; | 146 class BlockedAction { |
| 96 GURL arg_url_; | 147 public: |
| 97 scoped_ptr<DictionaryValue> other_; | 148 // These values should not be changed. Append any additional values to the |
| 149 // end with sequential numbers. |
| 150 enum Reason { |
| 151 UNKNOWN = 0, |
| 152 ACCESS_DENIED = 1, |
| 153 QUOTA_EXCEEDED = 2, |
| 154 }; |
| 155 |
| 156 private: |
| 157 DISALLOW_IMPLICIT_CONSTRUCTORS(BlockedAction); |
| 98 }; | 158 }; |
| 99 | 159 |
| 100 } // namespace extensions | 160 } // namespace extensions |
| 101 | 161 |
| 102 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ | 162 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_ACTIONS_H_ |
| 103 | |
| OLD | NEW |