mozHunspellRLBoxHost.h (3890B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozHunspellRLBoxHost_h 8 #define mozHunspellRLBoxHost_h 9 10 #include <map> 11 #include <memory> 12 #include <mutex> 13 #include <set> 14 #include <stdint.h> 15 16 #include "RLBoxHunspell.h" 17 #include "mozilla/Result.h" 18 #include "mozilla/ResultExtensions.h" 19 #include "mozilla/RWLock.h" 20 #include "nsIChannel.h" 21 #include "nsIInputStream.h" 22 #include "nsReadLine.h" 23 24 namespace mozilla { 25 26 class mozHunspellFileMgrHost final { 27 public: 28 /** 29 * aFilename must be a local file/jar URI for the file to load. 30 */ 31 explicit mozHunspellFileMgrHost(const nsCString& aFilename); 32 ~mozHunspellFileMgrHost() = default; 33 34 bool GetLine(nsACString& aResult); 35 int GetLineNum() const { return mLineNum; } 36 37 static Result<int64_t, nsresult> GetSize(const nsCString& aFilename); 38 39 private: 40 static mozilla::Result<mozilla::Ok, nsresult> Open( 41 const nsCString& aPath, nsCOMPtr<nsIChannel>& aChannel, 42 nsCOMPtr<nsIInputStream>& aStream); 43 44 mozilla::Result<mozilla::Ok, nsresult> ReadLine(nsACString& aLine); 45 46 int mLineNum = 0; 47 nsCOMPtr<nsIInputStream> mStream; 48 nsLineBuffer<char> mLineBuffer; 49 }; 50 51 class mozHunspellCallbacks { 52 public: 53 // APIs invoked by the sandboxed hunspell file manager 54 static tainted_hunspell<uint32_t> CreateFilemgr( 55 rlbox_sandbox_hunspell& aSandbox, 56 tainted_hunspell<const char*> t_aFilename); 57 static tainted_hunspell<bool> GetLine(rlbox_sandbox_hunspell& aSandbox, 58 tainted_hunspell<uint32_t> t_aFd, 59 tainted_hunspell<char**> t_aLinePtr); 60 static tainted_hunspell<int> GetLineNum(rlbox_sandbox_hunspell& aSandbox, 61 tainted_hunspell<uint32_t> t_aFd); 62 static void DestructFilemgr(rlbox_sandbox_hunspell& aSandbox, 63 tainted_hunspell<uint32_t> t_aFd); 64 65 // APIs necessary for hunspell UTF encoding 66 static tainted_hunspell<uint32_t> ToUpperCase( 67 rlbox_sandbox_hunspell& aSandbox, tainted_hunspell<uint32_t> t_aChar); 68 static tainted_hunspell<uint32_t> ToLowerCase( 69 rlbox_sandbox_hunspell& aSandbox, tainted_hunspell<uint32_t> t_aChar); 70 static tainted_hunspell<struct cs_info*> GetCurrentCS( 71 rlbox_sandbox_hunspell& aSandbox, tainted_hunspell<const char*> t_es); 72 73 protected: 74 // API called by RLBox 75 76 /** 77 * Add filename to allow list. 78 */ 79 static void AllowFile(const nsCString& aFilename); 80 friend RLBoxHunspell* RLBoxHunspell::Create(const nsCString& affpath, 81 const nsCString& dpath); 82 /** 83 * Clear allow list and map of hunspell file managers. 84 */ 85 static void Clear(); 86 friend RLBoxHunspell::~RLBoxHunspell(); 87 88 private: 89 /** 90 * sFileMgrMap holds a map between unique uint32_t 91 * integers and mozHunspellFileMgrHost instances 92 */ 93 static std::map<uint32_t, std::unique_ptr<mozHunspellFileMgrHost>> 94 sFileMgrMap; 95 96 /** 97 * sFileMgrAllowList contains the filenames of the dictionary files hunspell 98 * is allowed to open 99 */ 100 static std::set<nsCString> sFileMgrAllowList; 101 /** 102 * Reader-writer lock for the sFileMgrMap 103 */ 104 static mozilla::StaticRWLock sFileMgrMapLock; 105 /** 106 * Tracks the next possibly unused id for sFileMgrMap 107 */ 108 static uint32_t sCurrentFreshId; 109 /** 110 * Returns an unused id for sFileMgrMap 111 */ 112 static uint32_t GetFreshId(); 113 /** 114 * Returns the mozHunspellFileMgrHost for the given uint32_t id 115 */ 116 static mozHunspellFileMgrHost& GetMozHunspellFileMgrHost( 117 tainted_hunspell<uint32_t> t_aFd); 118 }; 119 } // namespace mozilla 120 121 #endif