summaryrefslogtreecommitdiffstats
path: root/toolkit/mozapps/defaultagent/Cache.cpp
blob: 439fdf57f8b31fc3612b69dcb718bf6789afa04b (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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Cache.h"

#include <algorithm>

#include "common.h"
#include "EventLog.h"
#include "mozilla/Unused.h"

// Cache entry version documentation:
//   Version 1:
//     The version number is written explicitly when version 1 cache entries are
//     migrated, but in their original location there is no version key.
//     Required Keys:
//       CacheEntryVersion: <DWORD>
//       NotificationType: <string>
//       NotificationShown: <string>
//       NotificationAction: <string>
//   Version 2:
//     Required Keys:
//       CacheEntryVersion: <DWORD>
//       NotificationType: <string>
//       NotificationShown: <string>
//       NotificationAction: <string>
//       PrevNotificationAction: <string>

static std::wstring MakeVersionedRegSubKey(const wchar_t* baseKey) {
  std::wstring key;
  if (baseKey) {
    key = baseKey;
  } else {
    key = Cache::kDefaultPingCacheRegKey;
  }
  key += L"\\version";
  key += std::to_wstring(Cache::kVersion);
  return key;
}

Cache::Cache(const wchar_t* cacheRegKey /* = nullptr */)
    : mCacheRegKey(MakeVersionedRegSubKey(cacheRegKey)),
      mInitializeResult(mozilla::Nothing()),
      mCapacity(Cache::kDefaultCapacity),
      mFront(0),
      mSize(0) {}

Cache::~Cache() {}

VoidResult Cache::Init() {
  if (mInitializeResult.isSome()) {
    HRESULT hr = mInitializeResult.value();
    if (FAILED(hr)) {
      return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
    } else {
      return mozilla::Ok();
    }
  }

  VoidResult result = SetupCache();
  if (result.isErr()) {
    HRESULT hr = result.inspectErr().AsHResult();
    mInitializeResult = mozilla::Some(hr);
    return result;
  }

  // At this point, the cache is ready to use, so mark the initialization as
  // complete. This is important so that when we attempt migration, below,
  // the migration's attempts to write to the cache don't try to initialize
  // the cache again.
  mInitializeResult = mozilla::Some(S_OK);

  // Ignore the result of the migration. If we failed to migrate, there may be
  // some data loss. But that's better than failing to ever use the new cache
  // just because there's something wrong with the old one.
  mozilla::Unused << MaybeMigrateVersion1();

  return mozilla::Ok();
}

// If the setting does not exist, the default value is written and returned.
DwordResult Cache::EnsureDwordSetting(const wchar_t* regName,
                                      uint32_t defaultValue) {
  MaybeDwordResult readResult = RegistryGetValueDword(
      IsPrefixed::Unprefixed, regName, mCacheRegKey.c_str());
  if (readResult.isErr()) {
    HRESULT hr = readResult.unwrapErr().AsHResult();
    LOG_ERROR_MESSAGE(L"Failed to read setting \"%s\": %#X", regName, hr);
    return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
  }
  mozilla::Maybe<uint32_t> maybeValue = readResult.unwrap();
  if (maybeValue.isSome()) {
    return maybeValue.value();
  }

  VoidResult writeResult = RegistrySetValueDword(
      IsPrefixed::Unprefixed, regName, defaultValue, mCacheRegKey.c_str());
  if (writeResult.isErr()) {
    HRESULT hr = writeResult.unwrapErr().AsHResult();
    LOG_ERROR_MESSAGE(L"Failed to write setting \"%s\": %#X", regName, hr);
    return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
  }
  return defaultValue;
}

// This function does two things:
//  1. It creates and sets the registry values used by the cache, if they don't
//     already exist.
//  2. If the the values already existed, it reads the settings of the cache
//     into their member variables.
VoidResult Cache::SetupCache() {
  DwordResult result =
      EnsureDwordSetting(Cache::kCapacityRegName, Cache::kDefaultCapacity);
  if (result.isErr()) {
    return mozilla::Err(result.unwrapErr());
  }
  mCapacity = std::min(result.unwrap(), Cache::kMaxCapacity);

  result = EnsureDwordSetting(Cache::kFrontRegName, 0);
  if (result.isErr()) {
    return mozilla::Err(result.unwrapErr());
  }
  mFront = std::min(result.unwrap(), Cache::kMaxCapacity - 1);

  result = EnsureDwordSetting(Cache::kSizeRegName, 0);
  if (result.isErr()) {
    return mozilla::Err(result.unwrapErr());
  }
  mSize = std::min(result.unwrap(), mCapacity);

  return mozilla::Ok();
}

static MaybeStringResult ReadVersion1CacheKey(const wchar_t* baseRegKeyName,
                                              uint32_t index) {
  std::wstring regName = Cache::kVersion1KeyPrefix;
  regName += baseRegKeyName;
  regName += std::to_wstring(index);

  MaybeStringResult result =
      RegistryGetValueString(IsPrefixed::Unprefixed, regName.c_str());
  if (result.isErr()) {
    HRESULT hr = result.inspectErr().AsHResult();
    LOG_ERROR_MESSAGE(L"Failed to read \"%s\": %#X", regName.c_str(), hr);
  }
  return result;
}

static VoidResult DeleteVersion1CacheKey(const wchar_t* baseRegKeyName,
                                         uint32_t index) {
  std::wstring regName = Cache::kVersion1KeyPrefix;
  regName += baseRegKeyName;
  regName += std::to_wstring(index);

  VoidResult result =
      RegistryDeleteValue(IsPrefixed::Unprefixed, regName.c_str());
  if (result.isErr()) {
    HRESULT hr = result.inspectErr().AsHResult();
    LOG_ERROR_MESSAGE(L"Failed to delete \"%s\": %#X", regName.c_str(), hr);
  }
  return result;
}

static VoidResult DeleteVersion1CacheEntry(uint32_t index) {
  VoidResult typeResult =
      DeleteVersion1CacheKey(Cache::kNotificationTypeKey, index);
  VoidResult shownResult =
      DeleteVersion1CacheKey(Cache::kNotificationShownKey, index);
  VoidResult actionResult =
      DeleteVersion1CacheKey(Cache::kNotificationActionKey, index);

  if (typeResult.isErr()) {
    return typeResult;
  }
  if (shownResult.isErr()) {
    return shownResult;
  }
  return actionResult;
}

VoidResult Cache::MaybeMigrateVersion1() {
  for (uint32_t index = 0; index < Cache::kVersion1MaxSize; ++index) {
    MaybeStringResult typeResult =
        ReadVersion1CacheKey(Cache::kNotificationTypeKey, index);
    if (typeResult.isErr()) {
      return mozilla::Err(typeResult.unwrapErr());
    }
    MaybeString maybeType = typeResult.unwrap();

    MaybeStringResult shownResult =
        ReadVersion1CacheKey(Cache::kNotificationShownKey, index);
    if (shownResult.isErr()) {
      return mozilla::Err(shownResult.unwrapErr());
    }
    MaybeString maybeShown = shownResult.unwrap();

    MaybeStringResult actionResult =
        ReadVersion1CacheKey(Cache::kNotificationActionKey, index);
    if (actionResult.isErr()) {
      return mozilla::Err(actionResult.unwrapErr());
    }
    MaybeString maybeAction = actionResult.unwrap();

    if (maybeType.isSome() && maybeShown.isSome() && maybeAction.isSome()) {
      // If something goes wrong, we'd rather lose a little data than migrate
      // over and over again. So delete the old entry before we add the new one.
      VoidResult result = DeleteVersion1CacheEntry(index);
      if (result.isErr()) {
        return result;
      }

      VersionedEntry entry = VersionedEntry{
          .entryVersion = 1,
          .notificationType = maybeType.value(),
          .notificationShown = maybeShown.value(),
          .notificationAction = maybeAction.value(),
          .prevNotificationAction = mozilla::Nothing(),
      };
      result = VersionedEnqueue(entry);
      if (result.isErr()) {
        // We already deleted the version 1 cache entry. No real reason to abort
        // now. May as well keep attempting to migrate.
        LOG_ERROR_MESSAGE(L"Warning: Version 1 cache entry %u dropped: %#X",
                          index, result.unwrapErr().AsHResult());
      }
    } else if (maybeType.isNothing() && maybeShown.isNothing() &&
               maybeAction.isNothing()) {
      // Looks like we've reached the end of the version 1 cache.
      break;
    } else {
      // This cache entry seems to be missing a key. Just drop it.
      LOG_ERROR_MESSAGE(
          L"Warning: Version 1 cache entry %u dropped due to missing keys",
          index);
      mozilla::Unused << DeleteVersion1CacheEntry(index);
    }
  }
  return mozilla::Ok();
}

std::wstring Cache::MakeEntryRegKeyName(uint32_t index) {
  std::wstring regName = mCacheRegKey;
  regName += L'\\';
  regName += std::to_wstring(index);
  return regName;
}

VoidResult Cache::WriteEntryKeys(uint32_t index, const VersionedEntry& entry) {
  std::wstring subKey = MakeEntryRegKeyName(index);

  VoidResult result =
      RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kEntryVersionKey,
                            entry.entryVersion, subKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Unable to write entry version to index %u: %#X", index,
                      result.inspectErr().AsHResult());
    return result;
  }

  result = RegistrySetValueString(
      IsPrefixed::Unprefixed, Cache::kNotificationTypeKey,
      entry.notificationType.c_str(), subKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Unable to write notification type to index %u: %#X",
                      index, result.inspectErr().AsHResult());
    return result;
  }

  result = RegistrySetValueString(
      IsPrefixed::Unprefixed, Cache::kNotificationShownKey,
      entry.notificationShown.c_str(), subKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Unable to write notification shown to index %u: %#X",
                      index, result.inspectErr().AsHResult());
    return result;
  }

  result = RegistrySetValueString(
      IsPrefixed::Unprefixed, Cache::kNotificationActionKey,
      entry.notificationAction.c_str(), subKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Unable to write notification type to index %u: %#X",
                      index, result.inspectErr().AsHResult());
    return result;
  }

  if (entry.prevNotificationAction.isSome()) {
    result = RegistrySetValueString(
        IsPrefixed::Unprefixed, Cache::kPrevNotificationActionKey,
        entry.prevNotificationAction.value().c_str(), subKey.c_str());
    if (result.isErr()) {
      LOG_ERROR_MESSAGE(
          L"Unable to write prev notification type to index %u: %#X", index,
          result.inspectErr().AsHResult());
      return result;
    }
  }

  return mozilla::Ok();
}

// Returns success on an attempt to delete a non-existent entry.
VoidResult Cache::DeleteEntry(uint32_t index) {
  std::wstring key = AGENT_REGKEY_NAME;
  key += L'\\';
  key += MakeEntryRegKeyName(index);
  // We could probably just delete they key here, rather than use this function,
  // which deletes keys recursively. But this mechanism allows future entry
  // versions to contain sub-keys without causing problems for older versions.
  LSTATUS ls = RegDeleteTreeW(HKEY_CURRENT_USER, key.c_str());
  if (ls != ERROR_SUCCESS && ls != ERROR_FILE_NOT_FOUND) {
    return mozilla::Err(mozilla::WindowsError::FromWin32Error(ls));
  }
  return mozilla::Ok();
}

VoidResult Cache::SetFront(uint32_t newFront) {
  VoidResult result =
      RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kFrontRegName,
                            newFront, mCacheRegKey.c_str());
  if (result.isOk()) {
    mFront = newFront;
  }
  return result;
}

VoidResult Cache::SetSize(uint32_t newSize) {
  VoidResult result =
      RegistrySetValueDword(IsPrefixed::Unprefixed, Cache::kSizeRegName,
                            newSize, mCacheRegKey.c_str());
  if (result.isOk()) {
    mSize = newSize;
  }
  return result;
}

// The entry passed to this function MUST already be valid. This function does
// not do any validation internally. We must not, for example, pass an entry
// to it with a version of 2 and a prevNotificationAction of mozilla::Nothing()
// because a version 2 entry requires that key.
VoidResult Cache::VersionedEnqueue(const VersionedEntry& entry) {
  VoidResult result = Init();
  if (result.isErr()) {
    return result;
  }

  if (mSize >= mCapacity) {
    LOG_ERROR_MESSAGE(L"Attempted to add an entry to the cache, but it's full");
    return mozilla::Err(mozilla::WindowsError::FromHResult(E_BOUNDS));
  }

  uint32_t index = (mFront + mSize) % mCapacity;

  // We really don't want to write to a location that has stale cache entry data
  // already lying around.
  result = DeleteEntry(index);
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Unable to remove stale entry: %#X",
                      result.inspectErr().AsHResult());
    return result;
  }

  result = WriteEntryKeys(index, entry);
  if (result.isErr()) {
    // We might have written a partial key. Attempt to clean up after ourself.
    mozilla::Unused << DeleteEntry(index);
    return result;
  }

  result = SetSize(mSize + 1);
  if (result.isErr()) {
    // If we failed to write the size, the new entry was not added successfully.
    // Attempt to clean up after ourself.
    mozilla::Unused << DeleteEntry(index);
    return result;
  }

  return mozilla::Ok();
}

VoidResult Cache::Enqueue(const Cache::Entry& entry) {
  Cache::VersionedEntry vEntry = Cache::VersionedEntry{
      .entryVersion = Cache::kEntryVersion,
      .notificationType = entry.notificationType,
      .notificationShown = entry.notificationShown,
      .notificationAction = entry.notificationAction,
      .prevNotificationAction = mozilla::Some(entry.prevNotificationAction),
  };
  return VersionedEnqueue(vEntry);
}

VoidResult Cache::DiscardFront() {
  if (mSize < 1) {
    LOG_ERROR_MESSAGE(L"Attempted to discard entry from an empty cache");
    return mozilla::Err(mozilla::WindowsError::FromHResult(E_BOUNDS));
  }
  // It's not a huge deal if we can't delete this. Moving mFront will result in
  // it being excluded from the cache anyways. We'll try to delete it again
  // anyways if we try to write to this index again.
  mozilla::Unused << DeleteEntry(mFront);

  VoidResult result = SetSize(mSize - 1);
  // We don't really need to bother moving mFront to the next index if the cache
  // is empty.
  if (result.isErr() || mSize == 0) {
    return result;
  }
  result = SetFront((mFront + 1) % mCapacity);
  if (result.isErr()) {
    // If we failed to set the front after we set the size, the cache is
    // in an inconsistent state.
    // But, even if the cache is inconsistent, we'll likely lose some data, but
    // we should eventually be able to recover. Any expected entries with no
    // data will be discarded and any unexpected entries with data will be
    // cleared out before we write data there.
    LOG_ERROR_MESSAGE(L"Cache inconsistent: Updated Size but not Front: %#X",
                      result.inspectErr().AsHResult());
  }
  return result;
}

/**
 * This function reads a DWORD cache key's value and returns it. If the expected
 * argument is true and the key is missing, this will delete the entire entry
 * and return mozilla::Nothing().
 */
MaybeDwordResult Cache::ReadEntryKeyDword(const std::wstring& regKey,
                                          const wchar_t* regName,
                                          bool expected) {
  MaybeDwordResult result =
      RegistryGetValueDword(IsPrefixed::Unprefixed, regName, regKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Failed to read \"%s\" from \"%s\": %#X", regName,
                      regKey.c_str(), result.inspectErr().AsHResult());
    return mozilla::Err(result.unwrapErr());
  }
  MaybeDword maybeValue = result.unwrap();
  if (expected && maybeValue.isNothing()) {
    LOG_ERROR_MESSAGE(L"Missing expected value \"%s\" from \"%s\"", regName,
                      regKey.c_str());
    VoidResult result = DiscardFront();
    if (result.isErr()) {
      return mozilla::Err(result.unwrapErr());
    }
  }
  return maybeValue;
}

/**
 * This function reads a string cache key's value and returns it. If the
 * expected argument is true and the key is missing, this will delete the entire
 * entry and return mozilla::Nothing().
 */
MaybeStringResult Cache::ReadEntryKeyString(const std::wstring& regKey,
                                            const wchar_t* regName,
                                            bool expected) {
  MaybeStringResult result =
      RegistryGetValueString(IsPrefixed::Unprefixed, regName, regKey.c_str());
  if (result.isErr()) {
    LOG_ERROR_MESSAGE(L"Failed to read \"%s\" from \"%s\": %#X", regName,
                      regKey.c_str(), result.inspectErr().AsHResult());
    return mozilla::Err(result.unwrapErr());
  }
  MaybeString maybeValue = result.unwrap();
  if (expected && maybeValue.isNothing()) {
    LOG_ERROR_MESSAGE(L"Missing expected value \"%s\" from \"%s\"", regName,
                      regKey.c_str());
    VoidResult result = DiscardFront();
    if (result.isErr()) {
      return mozilla::Err(result.unwrapErr());
    }
  }
  return maybeValue;
}

Cache::MaybeEntryResult Cache::Dequeue() {
  VoidResult result = Init();
  if (result.isErr()) {
    return mozilla::Err(result.unwrapErr());
  }

  std::wstring subKey = MakeEntryRegKeyName(mFront);

  // We are going to read within a loop so that if we find incomplete entries,
  // we can just discard them and try to read the next entry. We'll put a limit
  // on the maximum number of times this loop can possibly run so that if
  // something goes horribly wrong, we don't loop forever. If we exit this loop
  // without returning, it means that not only were we not able to read
  // anything, but something very unexpected happened.
  // We are going to potentially loop over this mCapacity + 1 times so that if
  // we end up discarding every item in the cache, we return mozilla::Nothing()
  // rather than an error.
  for (uint32_t i = 0; i <= mCapacity; ++i) {
    if (mSize == 0) {
      return MaybeEntry(mozilla::Nothing());
    }

    Cache::VersionedEntry entry;

    // CacheEntryVersion
    MaybeDwordResult dResult =
        ReadEntryKeyDword(subKey, Cache::kEntryVersionKey, true);
    if (dResult.isErr()) {
      return mozilla::Err(dResult.unwrapErr());
    }
    MaybeDword maybeDValue = dResult.unwrap();
    if (maybeDValue.isNothing()) {
      // Note that we only call continue in this function after DiscardFront()
      // has been called (either directly, or by one of the ReadEntryKey.*
      // functions). So the continue call results in attempting to read the
      // next entry in the cache.
      continue;
    }
    entry.entryVersion = maybeDValue.value();
    if (entry.entryVersion < 1) {
      LOG_ERROR_MESSAGE(L"Invalid entry version of %u in \"%s\"",
                        entry.entryVersion, subKey.c_str());
      VoidResult result = DiscardFront();
      if (result.isErr()) {
        return mozilla::Err(result.unwrapErr());
      }
      continue;
    }

    // NotificationType
    MaybeStringResult sResult =
        ReadEntryKeyString(subKey, Cache::kNotificationTypeKey, true);
    if (sResult.isErr()) {
      return mozilla::Err(sResult.unwrapErr());
    }
    MaybeString maybeSValue = sResult.unwrap();
    if (maybeSValue.isNothing()) {
      continue;
    }
    entry.notificationType = maybeSValue.value();

    // NotificationShown
    sResult = ReadEntryKeyString(subKey, Cache::kNotificationShownKey, true);
    if (sResult.isErr()) {
      return mozilla::Err(sResult.unwrapErr());
    }
    maybeSValue = sResult.unwrap();
    if (maybeSValue.isNothing()) {
      continue;
    }
    entry.notificationShown = maybeSValue.value();

    // NotificationAction
    sResult = ReadEntryKeyString(subKey, Cache::kNotificationActionKey, true);
    if (sResult.isErr()) {
      return mozilla::Err(sResult.unwrapErr());
    }
    maybeSValue = sResult.unwrap();
    if (maybeSValue.isNothing()) {
      continue;
    }
    entry.notificationAction = maybeSValue.value();

    // PrevNotificationAction
    bool expected =
        entry.entryVersion >= Cache::kInitialVersionPrevNotificationActionKey;
    sResult =
        ReadEntryKeyString(subKey, Cache::kPrevNotificationActionKey, expected);
    if (sResult.isErr()) {
      return mozilla::Err(sResult.unwrapErr());
    }
    maybeSValue = sResult.unwrap();
    if (expected && maybeSValue.isNothing()) {
      continue;
    }
    entry.prevNotificationAction = maybeSValue;

    // We successfully read the entry. Now we need to remove it from the cache.
    VoidResult result = DiscardFront();
    if (result.isErr()) {
      // If we aren't able to remove the entry from the cache, don't return it.
      // We don't want to return the same item over and over again if we get
      // into a bad state.
      return mozilla::Err(result.unwrapErr());
    }

    return mozilla::Some(entry);
  }

  LOG_ERROR_MESSAGE(L"Unexpected: This line shouldn't be reached");
  return mozilla::Err(mozilla::WindowsError::FromHResult(E_FAIL));
}