summaryrefslogtreecommitdiffstats
path: root/chrome/browser/password_manager/native_backend_libsecret.h
blob: 76574a98bd87bb2889011307909972a4888a1d12 (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
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
// Copyright (c) 2015 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_PASSWORD_MANAGER_NATIVE_BACKEND_LIBSECRET_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_LIBSECRET_H_

#include <libsecret/secret.h>

#include <string>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_vector.h"
#include "base/time/time.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/password_manager/password_store_x.h"
#include "chrome/browser/profiles/profile.h"

namespace autofill {
struct PasswordForm;
}

class LibsecretLoader {
 public:
  static decltype(&::secret_password_store_sync) secret_password_store_sync;
  static decltype(&::secret_service_search_sync) secret_service_search_sync;
  static decltype(&::secret_password_clear_sync) secret_password_clear_sync;
  static decltype(&::secret_item_get_secret) secret_item_get_secret;
  static decltype(&::secret_value_get_text) secret_value_get_text;
  static decltype(&::secret_item_get_attributes) secret_item_get_attributes;
  static decltype(&::secret_item_load_secret_sync) secret_item_load_secret_sync;
  static decltype(&::secret_value_unref) secret_value_unref;

 protected:
  static bool LoadLibsecret();
  static bool LibsecretIsAvailable();

  static bool libsecret_loaded;

 private:
  struct FunctionInfo {
    const char* name;
    void** pointer;
  };

  static const FunctionInfo functions[];
};

class NativeBackendLibsecret : public PasswordStoreX::NativeBackend,
                               public LibsecretLoader {
 public:
  explicit NativeBackendLibsecret(LocalProfileId id);

  ~NativeBackendLibsecret() override;

  bool Init() override;

  // Implements NativeBackend interface.
  password_manager::PasswordStoreChangeList AddLogin(
      const autofill::PasswordForm& form) override;
  bool UpdateLogin(const autofill::PasswordForm& form,
                   password_manager::PasswordStoreChangeList* changes) override;
  bool RemoveLogin(const autofill::PasswordForm& form) override;
  bool RemoveLoginsCreatedBetween(
      base::Time delete_begin,
      base::Time delete_end,
      password_manager::PasswordStoreChangeList* changes) override;
  bool RemoveLoginsSyncedBetween(
      base::Time delete_begin,
      base::Time delete_end,
      password_manager::PasswordStoreChangeList* changes) override;
  bool GetLogins(const autofill::PasswordForm& form,
                 ScopedVector<autofill::PasswordForm>* forms) override;
  bool GetAutofillableLogins(
      ScopedVector<autofill::PasswordForm>* forms) override;
  bool GetBlacklistLogins(ScopedVector<autofill::PasswordForm>* forms) override;

 private:
  enum TimestampToCompare {
    CREATION_TIMESTAMP,
    SYNC_TIMESTAMP,
  };

  enum AddUpdateLoginSearchOptions {
    SEARCH_USE_SUBMIT,
    SEARCH_IGNORE_SUBMIT,
  };

  // Returns credentials matching |lookup_form| and |options|.
  ScopedVector<autofill::PasswordForm> AddUpdateLoginSearch(
      const autofill::PasswordForm& lookup_form,
      AddUpdateLoginSearchOptions options);

  // Adds a login form without checking for one to replace first.
  bool RawAddLogin(const autofill::PasswordForm& form);

  enum GetLoginsListOptions {
    ALL_LOGINS,
    AUTOFILLABLE_LOGINS,
    BLACKLISTED_LOGINS,
  };

  // Retrieves credentials matching |options| from the keyring into |forms|,
  // overwriting the original contents of |forms|. If |lookup_form| is not NULL,
  // only retrieves credentials PSL-matching it. Returns true on success.
  bool GetLoginsList(const autofill::PasswordForm* lookup_form,
                     GetLoginsListOptions options,
                     ScopedVector<autofill::PasswordForm>* forms)
      WARN_UNUSED_RESULT;

  // Retrieves password created/synced in the time interval into |forms|,
  // overwriting the original contents of |forms|. Returns true on success.
  bool GetLoginsBetween(base::Time get_begin,
                        base::Time get_end,
                        TimestampToCompare date_to_compare,
                        ScopedVector<autofill::PasswordForm>* forms)
      WARN_UNUSED_RESULT;

  // Removes password created/synced in the time interval. Returns |true| if the
  // operation succeeded. |changes| will contain the changes applied.
  bool RemoveLoginsBetween(base::Time get_begin,
                           base::Time get_end,
                           TimestampToCompare date_to_compare,
                           password_manager::PasswordStoreChangeList* changes);

  // convert data get from Libsecret to Passwordform
  ScopedVector<autofill::PasswordForm> ConvertFormList(
      GList* found,
      const autofill::PasswordForm* lookup_form);

  // The app string, possibly based on the local profile id.
  std::string app_string_;

  DISALLOW_COPY_AND_ASSIGN(NativeBackendLibsecret);
};

#endif  // CHROME_BROWSER_PASSWORD_MANAGER_NATIVE_BACKEND_LIBSECRET_H_