 Chromium Code Reviews
 Chromium Code Reviews Issue 21646004:
  Compressed activity log database storage  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@refactor-cleanups
    
  
    Issue 21646004:
  Compressed activity log database storage  (Closed) 
  Base URL: http://git.chromium.org/chromium/src.git@refactor-cleanups| 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 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | 5 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | 
| 6 | 6 | 
| 7 #include <stdint.h> | 7 #include <stdint.h> | 
| 8 | 8 | 
| 9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" | 
| 10 #include "base/json/json_string_value_serializer.h" | |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" | 
| 12 #include "base/strings/stringprintf.h" | |
| 11 #include "base/time/clock.h" | 13 #include "base/time/clock.h" | 
| 12 #include "base/time/time.h" | 14 #include "base/time/time.h" | 
| 15 #include "chrome/browser/extensions/activity_log/activity_action_constants.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" | 
| 14 #include "chrome/common/extensions/extension.h" | 17 #include "chrome/common/extensions/extension.h" | 
| 15 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" | 
| 16 #include "url/gurl.h" | 19 #include "url/gurl.h" | 
| 17 | 20 | 
| 18 using content::BrowserThread; | 21 using content::BrowserThread; | 
| 19 | 22 | 
| 23 namespace constants = activity_log_constants; | |
| 24 | |
| 25 namespace { | |
| 26 // Obsolete database tables: these should be dropped from the database if | |
| 27 // found. | |
| 28 const char* kObsoleteTables[] = {"activitylog_apis", "activitylog_blocked", | |
| 29 "activitylog_urls"}; | |
| 30 } // namespace | |
| 31 | |
| 20 namespace extensions { | 32 namespace extensions { | 
| 21 | 33 | 
| 22 ActivityLogPolicy::ActivityLogPolicy(Profile* profile) : testing_clock_(NULL) {} | 34 ActivityLogPolicy::ActivityLogPolicy(Profile* profile) : testing_clock_(NULL) {} | 
| 23 | 35 | 
| 24 ActivityLogPolicy::~ActivityLogPolicy() {} | 36 ActivityLogPolicy::~ActivityLogPolicy() {} | 
| 25 | 37 | 
| 26 base::Time ActivityLogPolicy::Now() const { | 38 base::Time ActivityLogPolicy::Now() const { | 
| 27 if (testing_clock_) | 39 if (testing_clock_) | 
| 28 return testing_clock_->Now(); | 40 return testing_clock_->Now(); | 
| 29 else | 41 else | 
| 30 return base::Time::Now(); | 42 return base::Time::Now(); | 
| 31 } | 43 } | 
| 32 | 44 | 
| 33 std::string ActivityLogPolicy::GetKey(KeyType) const { | |
| 34 return std::string(); | |
| 35 } | |
| 36 | |
| 37 ActivityLogDatabasePolicy::ActivityLogDatabasePolicy( | 45 ActivityLogDatabasePolicy::ActivityLogDatabasePolicy( | 
| 38 Profile* profile, | 46 Profile* profile, | 
| 39 const base::FilePath& database_name) | 47 const base::FilePath& database_name) | 
| 40 : ActivityLogPolicy(profile) { | 48 : ActivityLogPolicy(profile) { | 
| 41 CHECK(profile); | 49 CHECK(profile); | 
| 42 base::FilePath profile_base_path = profile->GetPath(); | 50 base::FilePath profile_base_path = profile->GetPath(); | 
| 43 db_ = new ActivityDatabase(this); | 51 db_ = new ActivityDatabase(this); | 
| 44 base::FilePath database_path = profile_base_path.Append(database_name); | 52 base::FilePath database_path = profile_base_path.Append(database_name); | 
| 45 ScheduleAndForget(db_, &ActivityDatabase::Init, database_path); | 53 ScheduleAndForget(db_, &ActivityDatabase::Init, database_path); | 
| 46 } | 54 } | 
| 47 | 55 | 
| 48 sql::Connection* ActivityLogDatabasePolicy::GetDatabaseConnection() const { | 56 sql::Connection* ActivityLogDatabasePolicy::GetDatabaseConnection() const { | 
| 49 return db_->GetSqlConnection(); | 57 return db_->GetSqlConnection(); | 
| 50 } | 58 } | 
| 51 | 59 | 
| 60 // static | |
| 61 std::string ActivityLogPolicy::Util::Serialize(const base::Value* value) { | |
| 62 std::string value_as_text; | |
| 63 if (!value) { | |
| 64 value_as_text = ""; | |
| 65 } else { | |
| 66 JSONStringValueSerializer serializer(&value_as_text); | |
| 67 serializer.SerializeAndOmitBinaryValues(*value); | |
| 68 } | |
| 69 return value_as_text; | |
| 70 } | |
| 71 | |
| 72 // static | |
| 73 void ActivityLogPolicy::Util::SanitizeAction(scoped_refptr<Action> action) { | |
| 
felt
2013/08/07 01:09:42
Now that these are all pulled out into their own m
 
mvrable
2013/08/07 17:01:19
Yes, I should have written unit tests.  I'll add s
 | |
| 74 // Clear incognito URLs/titles. | |
| 75 if (action->page_incognito()) { | |
| 76 action->set_page_url(GURL()); | |
| 77 action->set_page_title(""); | |
| 78 } | |
| 79 if (action->arg_incognito()) { | |
| 80 action->set_arg_url(GURL()); | |
| 81 } | |
| 82 | |
| 83 // Strip query parameters, username/password, etc., from URLs. | |
| 84 if (action->page_url().is_valid() || action->arg_url().is_valid()) { | |
| 85 url_canon::Replacements<char> url_sanitizer; | |
| 86 url_sanitizer.ClearUsername(); | |
| 87 url_sanitizer.ClearPassword(); | |
| 88 url_sanitizer.ClearQuery(); | |
| 89 url_sanitizer.ClearRef(); | |
| 90 | |
| 91 if (action->page_url().is_valid()) | |
| 
felt
2013/08/07 01:09:42
Can they be set if they aren't valid? Is this a re
 | |
| 92 action->set_page_url(action->page_url().ReplaceComponents(url_sanitizer)); | |
| 93 if (action->arg_url().is_valid()) | |
| 94 action->set_arg_url(action->arg_url().ReplaceComponents(url_sanitizer)); | |
| 95 } | |
| 96 | |
| 97 // Clear WebRequest details; only keep a record of which types of | |
| 98 // modifications were performed. | |
| 99 if (action->action_type() == Action::ACTION_WEB_REQUEST) { | |
| 100 DictionaryValue* details = NULL; | |
| 101 if (action->mutable_other()->GetDictionary(constants::kActionWebRequest, | |
| 102 &details)) { | |
| 103 DictionaryValue::Iterator details_iterator(*details); | |
| 104 while (!details_iterator.IsAtEnd()) { | |
| 105 details->SetBoolean(details_iterator.key(), true); | |
| 106 details_iterator.Advance(); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // static | |
| 113 void ActivityLogPolicy::Util::StripArguments( | |
| 114 const std::set<std::string>& api_whitelist, | |
| 115 scoped_refptr<Action> action) { | |
| 116 if (action->action_type() != Action::ACTION_API_CALL && | |
| 117 action->action_type() != Action::ACTION_API_EVENT && | |
| 118 action->action_type() != Action::ACTION_API_BLOCKED) | |
| 119 return; | |
| 120 | |
| 121 if (api_whitelist.find(action->api_name()) == api_whitelist.end()) | |
| 122 action->set_args(scoped_ptr<ListValue>()); | |
| 123 } | |
| 124 | |
| 125 // static | |
| 126 std::string ActivityLogPolicy::Util::UrlToString(const GURL& url, | |
| 127 bool incognito_flag) { | |
| 128 return (incognito_flag ? constants::kIncognitoUrl : "") + url.spec(); | |
| 
felt
2013/08/07 01:09:42
Sorry, I'm not sure what the point of this is. Cou
 
mvrable
2013/08/07 17:01:19
See my later comment on the formatting.
I ended u
 | |
| 129 } | |
| 130 | |
| 131 // static | |
| 132 base::Time ActivityLogPolicy::Util::AddDays(const base::Time& base_date, | |
| 133 int days) { | |
| 134 // To allow for time zone changes, add an additional partial day then round | |
| 135 // down to midnight. | |
| 136 return (base_date + base::TimeDelta::FromDays(days) + | |
| 137 base::TimeDelta::FromHours(4)).LocalMidnight(); | |
| 138 } | |
| 139 | |
| 140 // static | |
| 141 void ActivityLogPolicy::Util::ComputeDatabaseTimeBounds(const base::Time& now, | |
| 142 int days_ago, | |
| 143 int64* early_bound, | |
| 144 int64* late_bound) { | |
| 145 base::Time morning_midnight = now.LocalMidnight(); | |
| 146 if (days_ago == 0) { | |
| 147 *early_bound = morning_midnight.ToInternalValue(); | |
| 148 *late_bound = base::Time::Max().ToInternalValue(); | |
| 149 } else { | |
| 150 base::Time early_time = Util::AddDays(morning_midnight, -days_ago); | |
| 151 base::Time late_time = Util::AddDays(early_time, 1); | |
| 152 *early_bound = early_time.ToInternalValue(); | |
| 153 *late_bound = late_time.ToInternalValue(); | |
| 154 } | |
| 155 } | |
| 156 | |
| 157 // static | |
| 158 bool ActivityLogPolicy::Util::DropObsoleteTables(sql::Connection* db) { | |
| 159 for (size_t i = 0; i < arraysize(kObsoleteTables); i++) { | |
| 160 const char* table_name = kObsoleteTables[i]; | |
| 161 if (db->DoesTableExist(table_name)) { | |
| 162 std::string drop_statement = | |
| 163 base::StringPrintf("DROP TABLE %s", table_name); | |
| 164 if (!db->Execute(drop_statement.c_str())) { | |
| 165 return false; | |
| 166 } | |
| 167 } | |
| 168 } | |
| 169 return true; | |
| 170 } | |
| 171 | |
| 52 } // namespace extensions | 172 } // namespace extensions | 
| OLD | NEW |