1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/activity_log/activity_log_policy.h"
#include <stdint.h>
#include "base/files/file_path.h"
#include "base/json/json_string_value_serializer.h"
#include "base/logging.h"
#include "base/strings/stringprintf.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "chrome/browser/extensions/activity_log/activity_action_constants.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/extension.h"
#include "url/gurl.h"
using content::BrowserThread;
namespace constants = activity_log_constants;
namespace {
// Obsolete database tables: these should be dropped from the database if
// found.
const char* const kObsoleteTables[] = {"activitylog_apis",
"activitylog_blocked",
"activitylog_urls"};
} // namespace
namespace extensions {
ActivityLogPolicy::ActivityLogPolicy(Profile* profile) {}
ActivityLogPolicy::~ActivityLogPolicy() {}
void ActivityLogPolicy::SetClockForTesting(scoped_ptr<base::Clock> clock) {
testing_clock_.reset(clock.release());
}
base::Time ActivityLogPolicy::Now() const {
if (testing_clock_)
return testing_clock_->Now();
else
return base::Time::Now();
}
ActivityLogDatabasePolicy::ActivityLogDatabasePolicy(
Profile* profile,
const base::FilePath& database_name)
: ActivityLogPolicy(profile) {
CHECK(profile);
base::FilePath profile_base_path = profile->GetPath();
db_ = new ActivityDatabase(this);
database_path_ = profile_base_path.Append(database_name);
}
void ActivityLogDatabasePolicy::Init() {
ScheduleAndForget(db_, &ActivityDatabase::Init, database_path_);
}
void ActivityLogDatabasePolicy::Flush() {
ScheduleAndForget(activity_database(),
&ActivityDatabase::AdviseFlush,
ActivityDatabase::kFlushImmediately);
}
sql::Connection* ActivityLogDatabasePolicy::GetDatabaseConnection() const {
return db_->GetSqlConnection();
}
// static
std::string ActivityLogPolicy::Util::Serialize(const base::Value* value) {
std::string value_as_text;
if (!value) {
value_as_text = "";
} else {
JSONStringValueSerializer serializer(&value_as_text);
serializer.SerializeAndOmitBinaryValues(*value);
}
return value_as_text;
}
// static
void ActivityLogPolicy::Util::StripPrivacySensitiveFields(
scoped_refptr<Action> action) {
// Clear incognito URLs/titles.
if (action->page_incognito()) {
action->set_page_url(GURL());
action->set_page_title("");
action->set_page_incognito(false);
}
if (action->arg_incognito()) {
action->set_arg_url(GURL());
action->set_arg_incognito(false);
}
// Strip query parameters, username/password, etc., from URLs.
if (action->page_url().is_valid() || action->arg_url().is_valid()) {
url::Replacements<char> url_sanitizer;
url_sanitizer.ClearUsername();
url_sanitizer.ClearPassword();
url_sanitizer.ClearQuery();
url_sanitizer.ClearRef();
if (action->page_url().is_valid())
action->set_page_url(action->page_url().ReplaceComponents(url_sanitizer));
if (action->arg_url().is_valid())
action->set_arg_url(action->arg_url().ReplaceComponents(url_sanitizer));
}
// Clear WebRequest details; only keep a record of which types of
// modifications were performed.
if (action->action_type() == Action::ACTION_WEB_REQUEST) {
base::DictionaryValue* details = NULL;
if (action->mutable_other()->GetDictionary(constants::kActionWebRequest,
&details)) {
base::DictionaryValue::Iterator details_iterator(*details);
while (!details_iterator.IsAtEnd()) {
details->SetBoolean(details_iterator.key(), true);
details_iterator.Advance();
}
}
}
}
// static
void ActivityLogPolicy::Util::StripArguments(const ApiSet& api_whitelist,
scoped_refptr<Action> action) {
if (api_whitelist.find(
std::make_pair(action->action_type(), action->api_name())) ==
api_whitelist.end()) {
action->set_args(scoped_ptr<base::ListValue>());
}
}
// static
base::Time ActivityLogPolicy::Util::AddDays(const base::Time& base_date,
int days) {
// To allow for time zone changes, add an additional partial day then round
// down to midnight.
return (base_date + base::TimeDelta::FromDays(days) +
base::TimeDelta::FromHours(4)).LocalMidnight();
}
// static
void ActivityLogPolicy::Util::ComputeDatabaseTimeBounds(const base::Time& now,
int days_ago,
int64* early_bound,
int64* late_bound) {
base::Time morning_midnight = now.LocalMidnight();
if (days_ago == 0) {
*early_bound = morning_midnight.ToInternalValue();
*late_bound = base::Time::Max().ToInternalValue();
} else {
base::Time early_time = Util::AddDays(morning_midnight, -days_ago);
base::Time late_time = Util::AddDays(early_time, 1);
*early_bound = early_time.ToInternalValue();
*late_bound = late_time.ToInternalValue();
}
}
// static
bool ActivityLogPolicy::Util::DropObsoleteTables(sql::Connection* db) {
for (size_t i = 0; i < arraysize(kObsoleteTables); i++) {
const char* table_name = kObsoleteTables[i];
if (db->DoesTableExist(table_name)) {
std::string drop_statement =
base::StringPrintf("DROP TABLE %s", table_name);
if (!db->Execute(drop_statement.c_str())) {
return false;
}
}
}
return true;
}
} // namespace extensions
|