Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4527)

Unified Diff: chrome/browser/extensions/activity_log/fullstream_ui_policy.cc

Issue 15573003: New architecture of the activity logging: Policies for summarization (and compression) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed compiler errors on different platforms (at least I hope so). Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/activity_log/fullstream_ui_policy.cc
diff --git a/chrome/browser/extensions/activity_log/fullstream_ui_policy.cc b/chrome/browser/extensions/activity_log/fullstream_ui_policy.cc
new file mode 100644
index 0000000000000000000000000000000000000000..61f4d67674f4c50ad10db29ec7b7352ea449ec1a
--- /dev/null
+++ b/chrome/browser/extensions/activity_log/fullstream_ui_policy.cc
@@ -0,0 +1,277 @@
+// Copyright (c) 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 "base/files/file_path.h"
+#include "base/json/json_string_value_serializer.h"
+#include "base/logging.h"
+#include "base/string16.h"
+#include "chrome/common/extensions/extension.h"
+#include "chrome/browser/extensions/activity_log/activity_database.h"
+#include "chrome/browser/extensions/activity_log/api_actions.h"
+#include "chrome/browser/extensions/activity_log/blocked_actions.h"
+#include "chrome/browser/extensions/activity_log/dom_actions.h"
+#include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
+#include "chrome/browser/profiles/profile.h"
+#include "chrome/common/chrome_constants.h"
+#include "googleurl/src/gurl.h"
+#include "sql/error_delegate_util.h"
+
+using namespace base;
+using namespace content;
+
+namespace extensions {
+
+// TODO(dbabic) This would be a fine error handler for all sql-based policies,
+// so it would make sense to introduce another class in the hierarchy,
+// SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this
+// error handler (as well as other SQLite-related functionality) there.
+
+// This handles errors from the database.
+class KillActivityDatabaseErrorDelegate : public sql::ErrorDelegate {
+ public:
+ explicit KillActivityDatabaseErrorDelegate(FullStreamUIPolicy* backend)
+ : backend_(backend),
+ scheduled_death_(false) {}
+
+ virtual int OnError(int error,
+ sql::Connection* connection,
+ sql::Statement* stmt) OVERRIDE {
+ if (!scheduled_death_ && sql::IsErrorCatastrophic(error)) {
+ ScheduleDeath();
+ }
+ return error;
+ }
+
+ // Schedules death if an error wasn't already reported.
+ void ScheduleDeath() {
+ if (!scheduled_death_) {
+ scheduled_death_ = true;
+ backend_->KillActivityLogDatabase();
+ }
+ }
+
+ bool scheduled_death() const {
+ return scheduled_death_;
+ }
+
+ private:
+ FullStreamUIPolicy* backend_;
+ bool scheduled_death_;
+
+ DISALLOW_COPY_AND_ASSIGN(KillActivityDatabaseErrorDelegate);
+};
+
+FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile)
+ : ActivityLogPolicy(profile) {
+ // We normally dispatch DB requests to the DB thread, but the thread might
+ // not exist if we are under test conditions. Substitute the UI thread for
+ // this case.
+ if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) {
+ dispatch_thread_ = BrowserThread::DB;
+ } else {
+ LOG(ERROR) << "BrowserThread::DB does not exist, running on UI thread!";
+ dispatch_thread_ = BrowserThread::UI;
+ }
+
+ db_ = new ActivityDatabase();
+ FilePath base_dir = profile->GetPath();
felt 2013/05/22 21:17:59 Isn't this redundant with base::FilePath profile_b
dbabic 2013/05/23 01:35:04 Done. Good catch!
+ FilePath database_name = base_dir.Append(
+ chrome::kExtensionActivityLogFilename);
+ KillActivityDatabaseErrorDelegate* error_delegate =
+ new KillActivityDatabaseErrorDelegate(this);
+ db_->SetErrorDelegate(error_delegate);
+ ScheduleAndForget(&ActivityDatabase::Init, database_name);
+}
+
+FullStreamUIPolicy::~FullStreamUIPolicy() {
+ ScheduleAndForget(&ActivityDatabase::Close);
+}
+
+// Get data as a set of key-value pairs. The keys are policy-specific.
+void FullStreamUIPolicy::ReadData(const std::string& extension_id,
+ const int day,
+ const
+ Callback<void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback)
+ const {
+
+ BrowserThread::PostTaskAndReplyWithResult(
+ dispatch_thread_,
+ FROM_HERE,
+ base::Bind(&ActivityDatabase::GetActions,
+ base::Unretained(db_),
+ extension_id,
+ day),
+ callback);
+}
+
+void FullStreamUIPolicy::GetKey(ActivityLogPolicy::KeyType key_ty,
+ std::string& key) const {
+ switch (key_ty) {
+ case PARAM_KEY_REASON:
+ key = "fsuip.reason";
+ break;
+ case PARAM_KEY_DOM_ACTION:
+ key = "fsuip.domact";
+ break;
+ case PARAM_KEY_URL_TITLE:
+ key = "fsuip.urltitle";
+ break;
+ case PARAM_KEY_DETAILS_STRING:
+ key = "fsuip.details";
+ break;
+ default:
+ key = "";
+ }
+}
+
+void FullStreamUIPolicy::KillActivityLogDatabase() {
+ ScheduleAndForget(&ActivityDatabase::KillDatabase);
+}
+
+void FullStreamUIPolicy::ProcessArguments(
+ const base::ListValue* args,
+ std::stringstream& processed_args) const {
felt 2013/05/22 21:17:59 Style guide says not to use stringstreams. Can you
dbabic 2013/05/23 01:35:04 String concatenation, as implemented in MakeArgLis
felt 2013/05/23 04:20:01 Why is MakeArgList quadratic in the size of the st
+ if (args) {
+ ListValue::const_iterator it = args->begin();
+ for (; it != args->end(); ++it) {
+ std::string arg;
+ JSONStringValueSerializer serializer(&arg);
+ if (serializer.SerializeAndOmitBinaryValues(**it)) {
+ if (it != args->begin()) {
+ processed_args << ", ";
+ }
+ processed_args << arg;
+ }
+ }
+ }
+}
+
+void FullStreamUIPolicy::DoProcessAction(
+ ActionType action_ty,
+ const Extension& extension,
+ const std::string& name,
+ const GURL* url_param,
+ const base::ListValue* args,
+ const base::DictionaryValue* details) {
+
+ std::stringstream concatenated_args;
+ ProcessArguments(args, concatenated_args);
+ const Time now = Time::Now();
+ // TODO(dbabic,felt) Drop the dummy string in the next revision
+ const std::string dummy;
+ GURL url_obj;
+ if (url_param) {
+ url_obj = *url_param;
+ }
+ scoped_refptr<Action> action;
+
+ switch (action_ty) {
+ case ACTION_API: {
+ action = new APIAction(
+ extension.id(),
+ now,
+ APIAction::CALL,
+ name,
+ concatenated_args.str(),
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ case ACTION_EVENT: {
+ action = new APIAction(
+ extension.id(),
+ now,
+ APIAction::EVENT_CALLBACK,
+ name,
+ concatenated_args.str(),
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ case ACTION_BLOCKED: {
+ std::string key;
+ int reason = 0;
+ if (details) {
+ GetKey(PARAM_KEY_REASON, key);
+ details->GetInteger(key, &reason);
+ }
+
+ action = new BlockedAction(
+ extension.id(),
+ now,
+ name,
+ concatenated_args.str(),
+ static_cast<BlockedAction::Reason>(reason),
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ case ACTION_DOM: {
+ std::string key;
+ string16 value;
+ DOMAction::DOMActionType action_ty = DOMAction::MODIFIED;
+
+ if (details) {
+ int action_id = 0;
+ GetKey(PARAM_KEY_DOM_ACTION, key);
+ details->GetInteger(key, &action_id);
+ action_ty = static_cast<DOMAction::DOMActionType>(action_id);
+ GetKey(PARAM_KEY_URL_TITLE, key);
+ details->GetString(key, &value);
+ }
+
+ action = new DOMAction(
+ extension.id(),
+ now,
+ action_ty,
+ url_obj,
+ value,
+ name,
+ concatenated_args.str(),
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ case ACTION_WEB_REQUEST: {
+ std::string key;
+ std::string details_string;
+ if (details) {
+ GetKey(PARAM_KEY_DETAILS_STRING, key);
+ details->GetString(key, &details_string);
+ }
+
+ action = new DOMAction(
+ extension.id(),
+ now,
+ DOMAction::WEBREQUEST,
+ url_obj,
+ string16(),
+ name,
+ details_string,
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ case ACTION_ON_SCRIPTS: {
+ std::string key;
+ string16 value;
+ if (details) {
+ GetKey(PARAM_KEY_URL_TITLE, key);
+ details->GetString(key, &value);
+ }
+
+ action = new DOMAction(
+ extension.id(),
+ now,
+ DOMAction::INSERTED,
+ url_obj,
+ value,
+ name,
+ concatenated_args.str(),
+ dummy /* TODO(dbabic,felt) Drop in the next revision */);
+ break;
+ }
+ default:
+ CHECK(false && "Unknown action type.");
+ }
+
+ ScheduleAndForget(&ActivityDatabase::RecordAction, action);
+}
+
+} // End of the extensions namespace

Powered by Google App Engine
This is Rietveld 408576698