summaryrefslogtreecommitdiffstats
path: root/extensions/spellcheck/hunspell/glue/mozHunspellFileMgrHost.h
blob: 8812416f71c53be0b262fdd134e13fb940f9366c (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 mozHunspellFileMgrHost_h
#define mozHunspellFileMgrHost_h

#include <map>
#include <memory>
#include <mutex>
#include <stdint.h>
#include <string>

#include "mozilla/Result.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/RWLock.h"
#include "nsIInputStream.h"
#include "nsReadLine.h"

namespace mozilla {

class mozHunspellFileMgrHost final {
 public:
  /**
   * aFilename must be a local file/jar URI for the file to load.
   *
   * aKey is the decription key for encrypted Hunzip files, and is
   * unsupported. The argument is there solely for compatibility.
   */
  explicit mozHunspellFileMgrHost(const char* aFilename,
                                  const char* aKey = nullptr);
  ~mozHunspellFileMgrHost() = default;

  bool GetLine(std::string& aResult);
  int GetLineNum() const { return mLineNum; }

 private:
  mozilla::Result<mozilla::Ok, nsresult> Open(const nsACString& aPath);

  mozilla::Result<mozilla::Ok, nsresult> ReadLine(nsACString& aLine);

  int mLineNum = 0;
  nsCOMPtr<nsIInputStream> mStream;
  nsLineBuffer<char> mLineBuffer;
};

class mozHunspellCallbacks {
 public:
  // APIs invoked by the sandboxed hunspell file manager
  static uint32_t CreateFilemgr(const char* aFilename, const char* aKey);
  static bool GetLine(uint32_t aFd, char** aLinePtr);
  static int GetLineNum(uint32_t aFd);
  static void DestructFilemgr(uint32_t aFd);

 private:
  /**
   * sFileMgrMap holds a map between unique uint32_t
   * integers and mozHunspellFileMgrHost instances
   */
  static std::map<uint32_t, std::unique_ptr<mozHunspellFileMgrHost>>
      sFileMgrMap;
  /**
   * Reader-writer lock for the sFileMgrMap
   */
  static mozilla::detail::StaticRWLock sFileMgrMapLock;
  /**
   * Tracks the next possibly unused id for sFileMgrMap
   */
  static uint32_t sCurrentFreshId;
  /**
   * Returns an unused id for sFileMgrMap
   */
  static uint32_t GetFreshId();
  /**
   * Returns the mozHunspellFileMgrHost for the given uint32_t id
   */
  static mozHunspellFileMgrHost& GetMozHunspellFileMgrHost(uint32_t aFd);
};
}  // namespace mozilla

#endif