File: BackgroundTasks.h

package info (click to toggle)
thunderbird 1%3A91.13.0-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,953,400 kB
  • sloc: cpp: 6,084,049; javascript: 4,790,441; ansic: 3,341,496; python: 862,958; asm: 366,542; xml: 204,277; java: 152,477; sh: 111,436; makefile: 21,388; perl: 15,312; yacc: 4,583; objc: 3,026; lex: 1,720; exp: 762; pascal: 635; awk: 564; sql: 453; php: 436; lisp: 432; ruby: 99; sed: 69; csh: 45
file content (150 lines) | stat: -rw-r--r-- 4,155 bytes parent folder | download | duplicates (4)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#ifndef mozilla_BackgroundTasks_h
#define mozilla_BackgroundTasks_h

#include "nsCOMPtr.h"
#include "nsIBackgroundTasks.h"
#include "nsIBackgroundTasksManager.h"
#include "nsICommandLine.h"
#include "nsIFile.h"
#include "nsISupports.h"
#include "nsImportModule.h"
#include "nsString.h"
#include "nsXULAppAPI.h"

#include "mozilla/LateWriteChecks.h"
#include "mozilla/Maybe.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Unused.h"

#include "prenv.h"

namespace mozilla {

class BackgroundTasks final : public nsIBackgroundTasks {
 public:
  NS_DECL_ISUPPORTS
  NS_DECL_NSIBACKGROUNDTASKS

 public:
  explicit BackgroundTasks(Maybe<nsCString> aBackgroundTask)
      : mBackgroundTask(aBackgroundTask) {}

  static void Init(Maybe<nsCString> aBackgroundTask) {
    MOZ_RELEASE_ASSERT(XRE_IsParentProcess());

    MOZ_RELEASE_ASSERT(!sSingleton,
                       "BackgroundTasks singleton already initialized");
    // The singleton will be cleaned up by `Shutdown()`.
    sSingleton = new BackgroundTasks(aBackgroundTask);
  }

  static void Shutdown() {
    MOZ_RELEASE_ASSERT(XRE_IsParentProcess());

    if (!sSingleton) {
      return;
    }

    if (sSingleton->mProfD) {
      AutoSuspendLateWriteChecks suspend;

      mozilla::Unused << sSingleton->mProfD->Remove(/* aRecursive */ true);
    }

    sSingleton = nullptr;
  }

  /**
   * Return a raw pointer to the singleton instance.  Use this accessor in C++
   * code that just wants to call a method on the instance, but does not need to
   * hold a reference.
   */
  static BackgroundTasks* GetSingleton() {
    if (!sSingleton) {
      // xpcshell doesn't set up background tasks: default to no background
      // task.
      Init(Nothing());
    }

    MOZ_RELEASE_ASSERT(
        sSingleton, "BackgroundTasks singleton should have been initialized");

    return sSingleton.get();
  }

  /**
   * Return an addRef'd pointer to the singleton instance. This is used by the
   * XPCOM constructor that exists to support usage from JS.
   */
  static already_AddRefed<BackgroundTasks> GetSingletonAddRefed() {
    return RefPtr<BackgroundTasks>(GetSingleton()).forget();
  }

  static const Maybe<nsCString> GetBackgroundTasks() {
    if (!XRE_IsParentProcess()) {
      return Nothing();
    }

    return GetSingleton()->mBackgroundTask;
  }

  static bool IsBackgroundTaskMode() {
    if (!XRE_IsParentProcess()) {
      return false;
    }

    return GetBackgroundTasks().isSome();
  }

  static nsresult CreateTemporaryProfileDirectory(const nsCString& aInstallHash,
                                                  nsIFile** aFile) {
    if (!XRE_IsParentProcess()) {
      return NS_ERROR_NOT_AVAILABLE;
    }

    return GetSingleton()->CreateTemporaryProfileDirectoryImpl(aInstallHash,
                                                               aFile);
  }

  static bool IsUsingTemporaryProfile() {
    return sSingleton && sSingleton->mProfD;
  }

  static nsresult RunBackgroundTask(nsICommandLine* aCmdLine) {
    Maybe<nsCString> task = GetBackgroundTasks();
    if (task.isNothing()) {
      return NS_ERROR_NOT_AVAILABLE;
    }

    nsCOMPtr<nsIBackgroundTasksManager> manager =
        do_ImportModule("resource://gre/modules/BackgroundTasksManager.jsm",
                        "BackgroundTasksManager");

    NS_ENSURE_TRUE(manager, NS_ERROR_FAILURE);

    NS_ConvertASCIItoUTF16 name(task.ref().get());
    Unused << manager->RunBackgroundTaskNamed(name, aCmdLine);

    return NS_OK;
  }

 protected:
  static StaticRefPtr<BackgroundTasks> sSingleton;

  Maybe<nsCString> mBackgroundTask;
  nsCOMPtr<nsIFile> mProfD;

  nsresult CreateTemporaryProfileDirectoryImpl(const nsCString& aInstallHash,
                                               nsIFile** aFile);

  virtual ~BackgroundTasks() = default;
};

}  // namespace mozilla

#endif  // mozilla_BackgroundTasks_h