Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright $YEAR 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. | |
| 4 | |
| 5 #include "base/files/file_path.h" | |
| 6 #include "base/json/json_string_value_serializer.h" | |
| 7 #include "base/logging.h" | |
| 8 #include "base/string16.h" | |
| 9 #include "chrome/common/extensions/extension.h" | |
| 10 #include "chrome/browser/extensions/activity_log/activity_database.h" | |
| 11 #include "chrome/browser/extensions/activity_log/api_actions.h" | |
| 12 #include "chrome/browser/extensions/activity_log/blocked_actions.h" | |
| 13 #include "chrome/browser/extensions/activity_log/dom_actions.h" | |
| 14 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
| 15 #include "chrome/browser/profiles/profile.h" | |
| 16 #include "chrome/common/chrome_constants.h" | |
| 17 #include "googleurl/src/gurl.h" | |
| 18 #include "sql/error_delegate_util.h" | |
| 19 | |
| 20 using namespace base; | |
| 21 using namespace content; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 // TODO(dbabic) This would be a fine error handler for all sql-based policies, | |
| 26 // so it would make sense to introduce another class in the hierarchy, | |
| 27 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this | |
| 28 // error handler (as well as other SQLite-related functionality) there. | |
| 29 | |
| 30 // This handles errors from the database. | |
| 31 class KillActivityDatabaseErrorDelegate : public sql::ErrorDelegate { | |
| 32 public: | |
| 33 explicit KillActivityDatabaseErrorDelegate(FullStreamUIPolicy* backend) | |
| 34 : backend_(backend), | |
| 35 scheduled_death_(false) {} | |
| 36 | |
| 37 virtual int OnError(int error, | |
| 38 sql::Connection* connection, | |
| 39 sql::Statement* stmt) OVERRIDE { | |
| 40 if (!scheduled_death_ && sql::IsErrorCatastrophic(error)) { | |
| 41 ScheduleDeath(); | |
| 42 } | |
| 43 return error; | |
| 44 } | |
| 45 | |
| 46 // Schedules death if an error wasn't already reported. | |
| 47 void ScheduleDeath() { | |
| 48 if (!scheduled_death_) { | |
| 49 scheduled_death_ = true; | |
| 50 backend_->KillActivityLogDatabase(); | |
| 51 } | |
| 52 } | |
| 53 | |
| 54 bool scheduled_death() const { | |
| 55 return scheduled_death_; | |
| 56 } | |
| 57 | |
| 58 private: | |
| 59 FullStreamUIPolicy* backend_; | |
| 60 bool scheduled_death_; | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(KillActivityDatabaseErrorDelegate); | |
| 63 }; | |
| 64 | |
| 65 FullStreamUIPolicy::FullStreamUIPolicy( | |
| 66 Profile* profile, | |
| 67 content::BrowserThread::ID thread_id) | |
| 68 : ActivityLogPolicy(profile, thread_id) { | |
| 69 // We normally dispatch DB requests to the DB thread, but the thread might | |
| 70 // not exist if we are under test conditions. Substitute the UI thread for | |
| 71 // this case. | |
| 72 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) { | |
| 73 dispatch_thread_ = BrowserThread::DB; | |
| 74 } else { | |
| 75 LOG(ERROR) << "BrowserThread::DB does not exist, running on UI thread!"; | |
| 76 dispatch_thread_ = BrowserThread::UI; | |
| 77 } | |
| 78 | |
| 79 db_ = new ActivityDatabase(); | |
| 80 FilePath database_name = profile_base_path_.Append( | |
| 81 chrome::kExtensionActivityLogFilename); | |
| 82 KillActivityDatabaseErrorDelegate* error_delegate = | |
| 83 new KillActivityDatabaseErrorDelegate(this); | |
| 84 db_->SetErrorDelegate(error_delegate); | |
| 85 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); | |
| 86 } | |
| 87 | |
| 88 FullStreamUIPolicy::~FullStreamUIPolicy() { | |
| 89 ScheduleAndForget(db_, &ActivityDatabase::Close); | |
| 90 } | |
| 91 | |
| 92 // Get data as a set of key-value pairs. The keys are policy-specific. | |
| 93 void FullStreamUIPolicy::ReadData(const std::string& extension_id, | |
| 94 const int day, | |
| 95 const | |
| 96 Callback<void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback) | |
|
felt
2013/05/24 18:43:38
Is there enough room to indent the callback anothe
dbabic
2013/05/28 21:11:49
Done (coppied your formatting).
| |
| 97 const { | |
| 98 BrowserThread::PostTaskAndReplyWithResult( | |
| 99 dispatch_thread_, | |
| 100 FROM_HERE, | |
| 101 base::Bind(&ActivityDatabase::GetActions, Unretained(db_), | |
| 102 extension_id, day), | |
| 103 callback); | |
| 104 } | |
| 105 | |
| 106 void FullStreamUIPolicy::GetKey(ActivityLogPolicy::KeyType key_ty, | |
| 107 std::string& key) const { | |
| 108 switch (key_ty) { | |
| 109 case PARAM_KEY_REASON: | |
| 110 key = "fsuip.reason"; | |
|
felt
2013/05/24 18:43:38
Can you make these k constants? Since they're only
dbabic
2013/05/28 21:11:49
Done.
| |
| 111 break; | |
| 112 case PARAM_KEY_DOM_ACTION: | |
| 113 key = "fsuip.domact"; | |
| 114 break; | |
| 115 case PARAM_KEY_URL_TITLE: | |
| 116 key = "fsuip.urltitle"; | |
| 117 break; | |
| 118 case PARAM_KEY_DETAILS_STRING: | |
| 119 key = "fsuip.details"; | |
| 120 break; | |
| 121 default: | |
| 122 key = ""; | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void FullStreamUIPolicy::KillActivityLogDatabase() { | |
| 127 ScheduleAndForget(db_, &ActivityDatabase::KillDatabase); | |
| 128 } | |
| 129 | |
| 130 void FullStreamUIPolicy::ProcessArguments( | |
|
felt
2013/05/24 18:43:38
Try checking out base/strings classes to see if yo
dbabic
2013/05/28 21:11:49
I checked all the classes and macros, there doesn'
| |
| 131 ActionType action_type, | |
| 132 const std::string& name, | |
| 133 const ListValue* args, | |
| 134 std::stringstream& processed_args) const { | |
| 135 if (args) { | |
| 136 ListValue::const_iterator it = args->begin(); | |
| 137 for (; it != args->end(); ++it) { | |
| 138 std::string arg; | |
| 139 JSONStringValueSerializer serializer(&arg); | |
| 140 if (serializer.SerializeAndOmitBinaryValues(**it)) { | |
| 141 if (it != args->begin()) { | |
| 142 processed_args << ", "; | |
| 143 } | |
| 144 processed_args << arg; | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void FullStreamUIPolicy::ProcessWebRequestModifications( | |
| 151 DictionaryValue& details, | |
| 152 std::string& details_string) const { | |
| 153 JSONStringValueSerializer serializer(&details_string); | |
| 154 serializer.Serialize(details); | |
| 155 } | |
| 156 | |
| 157 void FullStreamUIPolicy::ProcessAction( | |
| 158 ActionType action_type, | |
| 159 const Extension& extension, | |
| 160 const std::string& name, | |
| 161 const GURL* url_param, | |
| 162 const ListValue* args, | |
| 163 const DictionaryValue* details) { | |
| 164 std::stringstream concatenated_args; | |
| 165 ProcessArguments(action_type, name, args, concatenated_args); | |
| 166 const Time now = Time::Now(); | |
| 167 // TODO(dbabic,felt) Drop the dummy string in the next revision | |
| 168 const std::string dummy; | |
| 169 GURL url_obj; | |
| 170 if (url_param) { | |
| 171 url_obj = *url_param; | |
| 172 } | |
| 173 scoped_refptr<Action> action; | |
| 174 | |
| 175 switch (action_type) { | |
| 176 case ACTION_API: { | |
| 177 action = new APIAction( | |
| 178 extension.id(), | |
| 179 now, | |
| 180 APIAction::CALL, | |
| 181 name, | |
| 182 concatenated_args.str(), | |
| 183 dummy /* TODO(dbabic,felt) Drop in the next revision */); | |
|
felt
2013/05/24 18:43:38
use // for consistency, here and in the other part
dbabic
2013/05/28 21:11:49
Done.
| |
| 184 break; | |
| 185 } | |
| 186 case ACTION_EVENT: { | |
| 187 action = new APIAction( | |
| 188 extension.id(), | |
| 189 now, | |
| 190 APIAction::EVENT_CALLBACK, | |
| 191 name, | |
| 192 concatenated_args.str(), | |
| 193 dummy /* TODO(dbabic,felt) Drop in the next revision */); | |
| 194 break; | |
| 195 } | |
| 196 case ACTION_BLOCKED: { | |
| 197 std::string key; | |
| 198 int reason = 0; | |
| 199 if (details) { | |
| 200 GetKey(PARAM_KEY_REASON, key); | |
| 201 details->GetInteger(key, &reason); | |
| 202 } | |
| 203 | |
| 204 action = new BlockedAction( | |
| 205 extension.id(), | |
| 206 now, | |
| 207 name, | |
| 208 concatenated_args.str(), | |
| 209 static_cast<BlockedAction::Reason>(reason), | |
| 210 dummy /* TODO(dbabic,felt) Drop in the next revision */); | |
| 211 break; | |
| 212 } | |
| 213 case ACTION_DOM: { | |
| 214 std::string key; | |
| 215 string16 value; | |
| 216 DOMAction::DOMActionType action_type = DOMAction::MODIFIED; | |
| 217 | |
| 218 if (details) { | |
| 219 int action_id = 0; | |
| 220 GetKey(PARAM_KEY_DOM_ACTION, key); | |
| 221 details->GetInteger(key, &action_id); | |
| 222 action_type = static_cast<DOMAction::DOMActionType>(action_id); | |
| 223 GetKey(PARAM_KEY_URL_TITLE, key); | |
| 224 details->GetString(key, &value); | |
| 225 } | |
| 226 | |
| 227 action = new DOMAction( | |
| 228 extension.id(), | |
| 229 now, | |
| 230 action_type, | |
| 231 url_obj, | |
| 232 value, | |
| 233 name, | |
| 234 concatenated_args.str(), | |
| 235 dummy /* TODO(dbabic,felt) Drop in the next revision */); | |
| 236 break; | |
| 237 } | |
| 238 case ACTION_WEB_REQUEST: { | |
| 239 std::string key; | |
| 240 std::string details_string; | |
| 241 if (details) { | |
| 242 scoped_ptr<DictionaryValue> copy_of_details(details->DeepCopy()); | |
| 243 GetKey(PARAM_KEY_DETAILS_STRING, key); | |
| 244 ProcessWebRequestModifications(*copy_of_details.get(), details_string); | |
| 245 } | |
| 246 | |
| 247 action = new DOMAction( | |
| 248 extension.id(), | |
| 249 now, | |
| 250 DOMAction::WEBREQUEST, | |
| 251 url_obj, | |
| 252 string16(), | |
| 253 name, | |
| 254 details_string, | |
| 255 dummy /* TODO(dbabic,felt) Drop in the next revision */); | |
| 256 break; | |
| 257 } | |
| 258 default: | |
| 259 NOTREACHED(); | |
| 260 } | |
| 261 | |
| 262 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); | |
| 263 } | |
| 264 | |
| 265 } // End of the extensions namespace | |
|
felt
2013/05/24 18:43:38
"namespace extensions"
dbabic
2013/05/28 21:11:49
Done.
| |
| OLD | NEW |