activity_database.h revision 90dce4d38c5ff5333bea97d859d4e484e27edf0c
1// Copyright (c) 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#ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_
6#define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_
7
8#include <string>
9#include <vector>
10#include "base/basictypes.h"
11#include "base/files/file_path.h"
12#include "base/memory/ref_counted_memory.h"
13#include "base/synchronization/lock.h"
14#include "base/timer.h"
15#include "chrome/browser/extensions/activity_log/api_actions.h"
16#include "chrome/browser/extensions/activity_log/blocked_actions.h"
17#include "chrome/browser/extensions/activity_log/dom_actions.h"
18#include "chrome/common/extensions/extension.h"
19#include "content/public/browser/browser_thread.h"
20#include "sql/connection.h"
21#include "sql/init_status.h"
22
23namespace base {
24class Clock;
25class FilePath;
26}
27
28namespace extensions {
29
30// Encapsulates the SQL connection for the activity log database.
31// This class holds the database connection and has methods for writing.
32// All of the methods except constructor and SetErrorDelegate need to be
33// called on the DB thread. For this reason, the ActivityLog calls Close from
34// its destructor instead of destructing its ActivityDatabase object.
35class ActivityDatabase {
36 public:
37  // Need to call Init to actually use the ActivityDatabase.
38  ActivityDatabase();
39
40  // Sets up an optional error delegate.
41  // Should be the only thing done before Init.
42  void SetErrorDelegate(sql::ErrorDelegate* error_delegate);
43
44  // Opens the DB and creates tables as necessary.
45  void Init(const base::FilePath& db_name);
46
47  // The ActivityLog should call this to kill the ActivityDatabase.
48  void Close();
49
50  void LogInitFailure();
51
52  // Record a DOMction in the database.
53  void RecordDOMAction(scoped_refptr<DOMAction> action);
54
55  // Record a APIAction in the database.
56  void RecordAPIAction(scoped_refptr<APIAction> action);
57
58  // Record a BlockedAction in the database.
59  void RecordBlockedAction(scoped_refptr<BlockedAction> action);
60
61  // Record an Action in the database.
62  void RecordAction(scoped_refptr<Action> action);
63
64  // Gets all actions for a given extension for the specified day. 0 = today,
65  // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from
66  // newest to oldest.
67  scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions(
68      const std::string& extension_id, const int days_ago);
69
70  // Break any outstanding transactions, raze the database, and close
71  // it.  Future calls on the current database handle will fail, when
72  // next opened the database will be empty. This is the ugly version of Close.
73  void KillDatabase();
74
75  bool initialized() const { return initialized_; }
76
77  // Standard db operation wrappers.
78  void BeginTransaction();
79  void CommitTransaction();
80  void RollbackTransaction();
81  bool Raze();
82
83  // For unit testing only.
84  void SetBatchModeForTesting(bool batch_mode);
85  void SetClockForTesting(base::Clock* clock);
86  void SetTimerForTesting(int milliseconds);
87
88 private:
89  // This should never be invoked by another class. Use Close() to order a
90  // suicide.
91  virtual ~ActivityDatabase();
92
93  sql::InitStatus InitializeTable(const char* table_name,
94                                  const char* table_structure);
95
96  // When we're in batched mode (which is on by default), we write to the db
97  // every X minutes instead of on every API call. This prevents the annoyance
98  // of writing to disk multiple times a second.
99  void StartTimer();
100  void RecordBatchedActions();
101
102  // For unit testing only.
103  void RecordBatchedActionsWhileTesting();
104
105  base::Clock* testing_clock_;
106  sql::Connection db_;
107  bool initialized_;
108  bool batch_mode_;
109  std::vector<scoped_refptr<Action> > batched_actions_;
110  base::RepeatingTimer<ActivityDatabase> timer_;
111
112  DISALLOW_COPY_AND_ASSIGN(ActivityDatabase);
113};
114
115}  // namespace extensions
116#endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_DATABASE_H_
117