| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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_LOG_POLICY_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ |
| 7 | 7 |
| 8 #include <set> |
| 8 #include <string> | 9 #include <string> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
| 12 #include "base/callback.h" | 13 #include "base/callback.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 16 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
| 16 #include "chrome/browser/extensions/activity_log/activity_database.h" | 17 #include "chrome/browser/extensions/activity_log/activity_database.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 44 // is responsible for queueing) be flushed to storage. | 45 // is responsible for queueing) be flushed to storage. |
| 45 // | 46 // |
| 46 // Since every policy implementation might summarize data differently, the | 47 // Since every policy implementation might summarize data differently, the |
| 47 // database implementation is policy-specific and therefore completely | 48 // database implementation is policy-specific and therefore completely |
| 48 // encapsulated in the policy class. All the member functions can be called | 49 // encapsulated in the policy class. All the member functions can be called |
| 49 // on the UI thread. | 50 // on the UI thread. |
| 50 class ActivityLogPolicy { | 51 class ActivityLogPolicy { |
| 51 public: | 52 public: |
| 52 enum PolicyType { | 53 enum PolicyType { |
| 53 POLICY_FULLSTREAM, | 54 POLICY_FULLSTREAM, |
| 54 POLICY_NOARGS, | 55 POLICY_COUNTS, |
| 55 POLICY_INVALID, | 56 POLICY_INVALID, |
| 56 }; | 57 }; |
| 57 | 58 |
| 58 // For all subclasses, add all the key types they might support here. | |
| 59 // The actual key is returned by calling GetKey(KeyType). The subclasses | |
| 60 // are free to return an empty string for keys they don't support. | |
| 61 // For every key added here, you should update the GetKey member function | |
| 62 // for at least one policy. | |
| 63 enum KeyType { | |
| 64 PARAM_KEY_REASON, // Why an action was blocked | |
| 65 PARAM_KEY_DOM_ACTION, // Getter, Setter, Method,... | |
| 66 PARAM_KEY_URL_TITLE, | |
| 67 PARAM_KEY_DETAILS_STRING, | |
| 68 PARAM_KEY_EXTRA, | |
| 69 }; | |
| 70 | |
| 71 // Parameters are the profile and the thread that will be used to execute | 59 // Parameters are the profile and the thread that will be used to execute |
| 72 // the callback when ReadData is called. | 60 // the callback when ReadData is called. |
| 73 // TODO(felt,dbabic) Since only ReadData uses thread_id, it would be | 61 // TODO(felt,dbabic) Since only ReadData uses thread_id, it would be |
| 74 // cleaner to pass thread_id as a param of ReadData directly. | 62 // cleaner to pass thread_id as a param of ReadData directly. |
| 75 explicit ActivityLogPolicy(Profile* profile); | 63 explicit ActivityLogPolicy(Profile* profile); |
| 76 | 64 |
| 77 // Instead of a public destructor, ActivityLogPolicy objects have a Close() | 65 // Instead of a public destructor, ActivityLogPolicy objects have a Close() |
| 78 // method which will cause the object to be deleted (but may do so on another | 66 // method which will cause the object to be deleted (but may do so on another |
| 79 // thread or in a deferred fashion). | 67 // thread or in a deferred fashion). |
| 80 virtual void Close() = 0; | 68 virtual void Close() = 0; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 92 // TODO(felt,dbabic) This is overly specific to the current implementation of | 80 // TODO(felt,dbabic) This is overly specific to the current implementation of |
| 93 // the FullStreamUIPolicy. We should refactor it to use a more generic read | 81 // the FullStreamUIPolicy. We should refactor it to use a more generic read |
| 94 // function, for example one that takes a dictionary of query parameters | 82 // function, for example one that takes a dictionary of query parameters |
| 95 // (extension_id, time range, etc.). | 83 // (extension_id, time range, etc.). |
| 96 virtual void ReadData( | 84 virtual void ReadData( |
| 97 const std::string& extension_id, | 85 const std::string& extension_id, |
| 98 const int day, | 86 const int day, |
| 99 const base::Callback | 87 const base::Callback |
| 100 <void(scoped_ptr<Action::ActionVector>)>& callback) = 0; | 88 <void(scoped_ptr<Action::ActionVector>)>& callback) = 0; |
| 101 | 89 |
| 102 virtual std::string GetKey(KeyType key_id) const; | 90 // For unit testing only. |
| 91 void SetClockForTesting(scoped_ptr<base::Clock> clock); |
| 103 | 92 |
| 104 // For unit testing only. | 93 // A collection of methods that are useful for implementing policies. These |
| 105 void SetClockForTesting(base::Clock* clock) { testing_clock_ = clock; } | 94 // are all static methods; the ActivityLogPolicy::Util class cannot be |
| 95 // instantiated. This is nested within ActivityLogPolicy to make calling |
| 96 // these methods more convenient from within a policy, but they are public. |
| 97 class Util { |
| 98 public: |
| 99 // Serialize a Value as a JSON string. Returns an empty string if value is |
| 100 // null. |
| 101 static std::string Serialize(const base::Value* value); |
| 102 |
| 103 // Removes potentially privacy-sensitive data that should not be logged. |
| 104 // This should generally be called on an Action before logging, unless |
| 105 // debugging flags are enabled. Modifies the Action object in place; if |
| 106 // the action might be shared with other users, it is up to the caller to |
| 107 // call ->Clone() first. |
| 108 static void StripPrivacySensitiveFields(scoped_refptr<Action> action); |
| 109 |
| 110 // Strip arguments from most API actions, preserving actions only for a |
| 111 // whitelisted set. Modifies the Action object in-place. |
| 112 static void StripArguments(const std::set<std::string>& api_whitelist, |
| 113 scoped_refptr<Action> action); |
| 114 |
| 115 // Given a base day (timestamp at local midnight), computes the timestamp |
| 116 // at midnight the given number of days before or after. |
| 117 static base::Time AddDays(const base::Time& base_date, int days); |
| 118 |
| 119 // Compute the time bounds that should be used for a database query to |
| 120 // cover a time range days_ago days in the past, relative to the specified |
| 121 // time. |
| 122 static void ComputeDatabaseTimeBounds(const base::Time& now, |
| 123 int days_ago, |
| 124 int64* early_bound, |
| 125 int64* late_bound); |
| 126 |
| 127 // Deletes obsolete database tables from an activity log database. This |
| 128 // can be used in InitDatabase() methods of ActivityLogDatabasePolicy |
| 129 // subclasses to clean up data from old versions of the activity logging |
| 130 // code. Returns true on success, false on database error. |
| 131 static bool DropObsoleteTables(sql::Connection* db); |
| 132 |
| 133 private: |
| 134 DISALLOW_IMPLICIT_CONSTRUCTORS(Util); |
| 135 }; |
| 106 | 136 |
| 107 protected: | 137 protected: |
| 108 // An ActivityLogPolicy is not directly destroyed. Instead, call Close() | 138 // An ActivityLogPolicy is not directly destroyed. Instead, call Close() |
| 109 // which will cause the object to be deleted when it is safe. | 139 // which will cause the object to be deleted when it is safe. |
| 110 virtual ~ActivityLogPolicy(); | 140 virtual ~ActivityLogPolicy(); |
| 111 | 141 |
| 112 // Returns Time::Now() unless a mock clock has been installed with | 142 // Returns Time::Now() unless a mock clock has been installed with |
| 113 // SetClockForTesting, in which case the time according to that clock is used | 143 // SetClockForTesting, in which case the time according to that clock is used |
| 114 // instead. | 144 // instead. |
| 115 base::Time Now() const; | 145 base::Time Now() const; |
| 116 | 146 |
| 117 private: | 147 private: |
| 118 // Support for a mock clock for testing purposes. This is used by ReadData | 148 // Support for a mock clock for testing purposes. This is used by ReadData |
| 119 // to determine the date for "today" when when interpreting date ranges to | 149 // to determine the date for "today" when when interpreting date ranges to |
| 120 // fetch. This has no effect on batching of writes to the database. | 150 // fetch. This has no effect on batching of writes to the database. |
| 121 base::Clock* testing_clock_; | 151 scoped_ptr<base::Clock> testing_clock_; |
| 122 | 152 |
| 123 DISALLOW_COPY_AND_ASSIGN(ActivityLogPolicy); | 153 DISALLOW_COPY_AND_ASSIGN(ActivityLogPolicy); |
| 124 }; | 154 }; |
| 125 | 155 |
| 126 // A subclass of ActivityLogPolicy which is designed for policies that use | 156 // A subclass of ActivityLogPolicy which is designed for policies that use |
| 127 // database storage; it contains several useful helper methods. | 157 // database storage; it contains several useful helper methods. |
| 128 class ActivityLogDatabasePolicy : public ActivityLogPolicy, | 158 class ActivityLogDatabasePolicy : public ActivityLogPolicy, |
| 129 public ActivityDatabase::Delegate { | 159 public ActivityDatabase::Delegate { |
| 130 public: | 160 public: |
| 131 ActivityLogDatabasePolicy(Profile* profile, | 161 ActivityLogDatabasePolicy(Profile* profile, |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 | 199 |
| 170 private: | 200 private: |
| 171 // See the comments for the ActivityDatabase class for a discussion of how | 201 // See the comments for the ActivityDatabase class for a discussion of how |
| 172 // database cleanup runs. | 202 // database cleanup runs. |
| 173 ActivityDatabase* db_; | 203 ActivityDatabase* db_; |
| 174 }; | 204 }; |
| 175 | 205 |
| 176 } // namespace extensions | 206 } // namespace extensions |
| 177 | 207 |
| 178 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ | 208 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ |
| OLD | NEW |