summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/activity_database.h
blob: adcd3023593207ced7198079aec462148509d7c8 (plain)
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
// 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.

#ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_DATABASE_H_
#define CHROME_BROWSER_EXTENSIONS_ACTIVITY_DATABASE_H_

#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/memory/ref_counted_memory.h"
#include "chrome/browser/extensions/api_actions.h"
#include "chrome/browser/extensions/blocked_actions.h"
#include "chrome/browser/extensions/dom_actions.h"
#include "chrome/common/extensions/extension.h"
#include "sql/connection.h"
#include "sql/init_status.h"

namespace base {
class Clock;
class FilePath;
}

namespace extensions {

// Encapsulates the SQL connection for the activity log database.
// This class holds the database connection and has methods for writing.
class ActivityDatabase : public base::RefCountedThreadSafe<ActivityDatabase> {
 public:
  // Need to call Init to actually use the ActivityDatabase.
  ActivityDatabase();

  // Sets up an optional error delegate.
  // Should be the only thing done before Init.
  void SetErrorDelegate(sql::ErrorDelegate* error_delegate);

  // Opens the DB and creates tables as necessary.
  void Init(const base::FilePath& db_name);
  void LogInitFailure();

  // Record a DOMction in the database.
  void RecordDOMAction(scoped_refptr<DOMAction> action);

  // Record a APIAction in the database.
  void RecordAPIAction(scoped_refptr<APIAction> action);

  // Record a BlockedAction in the database.
  void RecordBlockedAction(scoped_refptr<BlockedAction> action);

  // Record an Action in the database.
  void RecordAction(scoped_refptr<Action> action);

  // Gets all actions for a given extension for the specified day. 0 = today,
  // 1 = yesterday, etc. Only returns 1 day at a time. Actions are sorted from
  // newest to oldest.
  scoped_ptr<std::vector<scoped_refptr<Action> > > GetActions(
      const std::string& extension_id, const int days_ago);

  // Break any outstanding transactions, raze the database, and close
  // it.  Future calls on the current database handle will fail, when
  // next opened the database will be empty.
  void KillDatabase();

  bool initialized() const { return initialized_; }

  // Standard db operation wrappers.
  void BeginTransaction();
  void CommitTransaction();
  void RollbackTransaction();
  bool Raze();
  void Close();

  void SetClockForTesting(base::Clock* clock);

 private:
  friend class base::RefCountedThreadSafe<ActivityDatabase>;

  virtual ~ActivityDatabase();

  sql::InitStatus InitializeTable(const char* table_name,
                                  const char* table_structure);

  base::Clock* testing_clock_;
  sql::Connection db_;
  bool initialized_;

  DISALLOW_COPY_AND_ASSIGN(ActivityDatabase);
};

}  // namespace extensions
#endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_DATABASE_H_