OLD | NEW |
(Empty) | |
| 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 |
| 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/browser/extensions/activity_log/activity_database.h" |
| 10 #include "chrome/browser/extensions/activity_log/api_actions.h" |
| 11 #include "chrome/browser/extensions/activity_log/blocked_actions.h" |
| 12 #include "chrome/browser/extensions/activity_log/dom_actions.h" |
| 13 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" |
| 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/extensions/dom_action_types.h" |
| 17 #include "chrome/common/extensions/extension.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 #include "sql/error_delegate_util.h" |
| 20 |
| 21 using base::Callback; |
| 22 using base::FilePath; |
| 23 using base::Time; |
| 24 using base::Unretained; |
| 25 using content::BrowserThread; |
| 26 |
| 27 namespace { |
| 28 |
| 29 // Key strings for passing parameters to the ProcessAction member function. |
| 30 const char kKeyReason[] = "fsuip.reason"; |
| 31 const char kKeyDomainAction[] = "fsuip.domact"; |
| 32 const char kKeyURLTitle[] = "fsuip.urltitle"; |
| 33 const char kKeyDetailsString[] = "fsuip.details"; |
| 34 |
| 35 } // namespace |
| 36 |
| 37 namespace extensions { |
| 38 |
| 39 // TODO(dbabic) This would be a fine error handler for all sql-based policies, |
| 40 // so it would make sense to introduce another class in the hierarchy, |
| 41 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this |
| 42 // error handler (as well as other SQLite-related functionality) there. |
| 43 |
| 44 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile) |
| 45 : ActivityLogPolicy(profile) { |
| 46 db_ = new ActivityDatabase(); |
| 47 FilePath database_name = profile_base_path_.Append( |
| 48 chrome::kExtensionActivityLogFilename); |
| 49 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); |
| 50 } |
| 51 |
| 52 FullStreamUIPolicy::~FullStreamUIPolicy() { |
| 53 // The policy object should have never been created if there's no DB thread. |
| 54 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); |
| 55 ScheduleAndForget(db_, &ActivityDatabase::Close); |
| 56 } |
| 57 |
| 58 // Get data as a set of key-value pairs. The keys are policy-specific. |
| 59 void FullStreamUIPolicy::ReadData( |
| 60 const std::string& extension_id, |
| 61 const int day, |
| 62 const Callback |
| 63 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback) |
| 64 const { |
| 65 BrowserThread::PostTaskAndReplyWithResult( |
| 66 BrowserThread::DB, |
| 67 FROM_HERE, |
| 68 base::Bind(&ActivityDatabase::GetActions, Unretained(db_), |
| 69 extension_id, day), |
| 70 callback); |
| 71 } |
| 72 |
| 73 void FullStreamUIPolicy::SetSaveStateOnRequestOnly() { |
| 74 ScheduleAndForget(db_, &ActivityDatabase::SetBatchModeForTesting, false); |
| 75 ActivityLogPolicy::SetSaveStateOnRequestOnly(); |
| 76 } |
| 77 |
| 78 std::string FullStreamUIPolicy::GetKey(ActivityLogPolicy::KeyType key_ty) const |
| 79 { |
| 80 switch (key_ty) { |
| 81 case PARAM_KEY_REASON: |
| 82 return std::string(kKeyReason); |
| 83 case PARAM_KEY_DOM_ACTION: |
| 84 return std::string(kKeyDomainAction); |
| 85 case PARAM_KEY_URL_TITLE: |
| 86 return std::string(kKeyURLTitle); |
| 87 case PARAM_KEY_DETAILS_STRING: |
| 88 return std::string(kKeyDetailsString); |
| 89 default: |
| 90 return std::string(); |
| 91 } |
| 92 } |
| 93 |
| 94 std::string FullStreamUIPolicy::ProcessArguments( |
| 95 ActionType action_type, |
| 96 const std::string& name, |
| 97 const ListValue* args) const { |
| 98 std::string processed_args; |
| 99 if (args) { |
| 100 ListValue::const_iterator it = args->begin(); |
| 101 // TODO(felt,dbabic) Think about replacing the loop with a single |
| 102 // call to SerializeAndOmitBinaryValues. |
| 103 for (; it != args->end(); ++it) { |
| 104 std::string arg; |
| 105 JSONStringValueSerializer serializer(&arg); |
| 106 if (serializer.SerializeAndOmitBinaryValues(**it)) { |
| 107 if (it != args->begin()) { |
| 108 processed_args.append(", "); |
| 109 } |
| 110 processed_args.append(arg); |
| 111 } |
| 112 } |
| 113 } |
| 114 return processed_args; |
| 115 } |
| 116 |
| 117 void FullStreamUIPolicy::ProcessWebRequestModifications( |
| 118 DictionaryValue& details, |
| 119 std::string& details_string) const { |
| 120 JSONStringValueSerializer serializer(&details_string); |
| 121 serializer.Serialize(details); |
| 122 } |
| 123 |
| 124 void FullStreamUIPolicy::ProcessAction( |
| 125 ActionType action_type, |
| 126 const std::string& extension_id, |
| 127 const std::string& name, |
| 128 const GURL& url_param, |
| 129 const ListValue* args, |
| 130 const DictionaryValue* details) { |
| 131 std::string concatenated_args = ProcessArguments(action_type, name, args); |
| 132 const Time now = Time::Now(); |
| 133 scoped_refptr<Action> action; |
| 134 std::string extra; |
| 135 if (details) { |
| 136 details->GetString(GetKey(PARAM_KEY_EXTRA), &extra); |
| 137 } |
| 138 |
| 139 switch (action_type) { |
| 140 case ACTION_API: { |
| 141 action = new APIAction( |
| 142 extension_id, |
| 143 now, |
| 144 APIAction::CALL, |
| 145 name, |
| 146 concatenated_args, |
| 147 extra); |
| 148 break; |
| 149 } |
| 150 case ACTION_EVENT: { |
| 151 action = new APIAction( |
| 152 extension_id, |
| 153 now, |
| 154 APIAction::EVENT_CALLBACK, |
| 155 name, |
| 156 concatenated_args, |
| 157 extra); |
| 158 break; |
| 159 } |
| 160 case ACTION_BLOCKED: { |
| 161 int reason = 0; |
| 162 if (details) { |
| 163 details->GetInteger(GetKey(PARAM_KEY_REASON), &reason); |
| 164 } |
| 165 |
| 166 action = new BlockedAction( |
| 167 extension_id, |
| 168 now, |
| 169 name, |
| 170 concatenated_args, |
| 171 static_cast<BlockedAction::Reason>(reason), |
| 172 extra); |
| 173 break; |
| 174 } |
| 175 case ACTION_DOM: { |
| 176 string16 value; |
| 177 DomActionType::Type action_type = DomActionType::MODIFIED; |
| 178 |
| 179 if (details) { |
| 180 int action_id = 0; |
| 181 details->GetInteger(GetKey(PARAM_KEY_DOM_ACTION), &action_id); |
| 182 action_type = static_cast<DomActionType::Type>(action_id); |
| 183 details->GetString(GetKey(PARAM_KEY_URL_TITLE), &value); |
| 184 } |
| 185 |
| 186 action = new DOMAction( |
| 187 extension_id, |
| 188 now, |
| 189 action_type, |
| 190 url_param, |
| 191 value, |
| 192 name, |
| 193 concatenated_args, |
| 194 extra); |
| 195 break; |
| 196 } |
| 197 case ACTION_WEB_REQUEST: { |
| 198 std::string details_string; |
| 199 if (details) { |
| 200 scoped_ptr<DictionaryValue> copy_of_details(details->DeepCopy()); |
| 201 ProcessWebRequestModifications(*copy_of_details.get(), details_string); |
| 202 } |
| 203 |
| 204 action = new DOMAction( |
| 205 extension_id, |
| 206 now, |
| 207 DomActionType::WEBREQUEST, |
| 208 url_param, |
| 209 string16(), |
| 210 name, |
| 211 details_string, |
| 212 extra); |
| 213 break; |
| 214 } |
| 215 default: |
| 216 NOTREACHED(); |
| 217 } |
| 218 |
| 219 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); |
| 220 } |
| 221 |
| 222 } // namespace extensions |
OLD | NEW |