1 | #pragma once |
---|
2 | #include <Carbon/Carbon.h> |
---|
3 | #include <list> |
---|
4 | |
---|
5 | using namespace std; |
---|
6 | |
---|
7 | #include "SCPatchPrivate.h" |
---|
8 | #include "SCPatchContext.h" |
---|
9 | #include "SCPatchRecord.h" |
---|
10 | #include "SCPatchMessenger.h" |
---|
11 | |
---|
12 | // Mac to Mach error morphing. |
---|
13 | #define err_mac err_system(0xf) /* Mac (Carbon) errors */ |
---|
14 | #define mac_err(CODE) SCmac_err_FromOSErr((CODE)) |
---|
15 | #define oserr(CODE) SCOSErrFrom_mac_err((CODE)) |
---|
16 | mach_error_t SCmac_err_FromOSErr(OSErr err); |
---|
17 | OSErr SCOSErrFrom_mac_err(mach_error_t error); |
---|
18 | |
---|
19 | typedef OSErr (*SCPatchIterationProc)(ProcessSerialNumber *inPSN, OSType inCreator, UInt32 inFlags, void *data); |
---|
20 | |
---|
21 | class SCPatchController : public SCPatchMessenger |
---|
22 | { |
---|
23 | public: |
---|
24 | |
---|
25 | SCPatchController(void); |
---|
26 | SCPatchController(CFStringRef bundleIdentifier); |
---|
27 | ~SCPatchController(void); |
---|
28 | |
---|
29 | // Use these to tell the controller where to find the patches |
---|
30 | void AddPatch(CFStringRef patchBundleIdentifier, |
---|
31 | CFStringRef subPath, |
---|
32 | CFStringRef name); |
---|
33 | |
---|
34 | // Inject the patches into running applications and start watching app launches |
---|
35 | OSErr InstallPatches(void); |
---|
36 | |
---|
37 | // Patch a single process (ignoring return value of ShouldPatchProcess) |
---|
38 | mach_error_t PatchProcess(ProcessSerialNumber *psn); |
---|
39 | |
---|
40 | // Overload these to customize patch loading and notification (note that |
---|
41 | // UnpatchNotification is only called when the process dies, since the patches |
---|
42 | // are not removable). |
---|
43 | virtual Boolean ShouldPatchProcess(ProcessSerialNumber * /* inPSN */, |
---|
44 | OSType /* inCreator */, OSType /* type */, |
---|
45 | CFStringRef /* name */, UInt32 /* flags */) { return false; } |
---|
46 | virtual void PatchNotification(ProcessSerialNumber * /* inPSN */, |
---|
47 | OSType /* inCreator */, OSType /* type */, |
---|
48 | CFStringRef /* name */, UInt32 /* flags */) {} |
---|
49 | virtual void UnpatchNotification(ProcessSerialNumber * /* inPSN */, |
---|
50 | OSType /* inCreator */, OSType /* type */, |
---|
51 | CFStringRef /* name */, UInt32 /* flags */) {} |
---|
52 | |
---|
53 | // You must also overload this method in SCPatchMessenger to receive messages. |
---|
54 | // virtual void ReceiveMessage(const AppleEvent *theAE) = 0; |
---|
55 | |
---|
56 | // Get info and keep tabs on patched processes |
---|
57 | Boolean IsProcessPatched(ProcessSerialNumber *inPSN); |
---|
58 | UInt32 GetPatchFlags(ProcessSerialNumber *inPSN); |
---|
59 | void SetPatchFlags(ProcessSerialNumber *inPSN, UInt32 flags, UInt32 whichFlags); |
---|
60 | |
---|
61 | // Loop through each patched process and execute a function. Note that this will |
---|
62 | // not recognize patched processes until after you've called IsProcessPatched() on |
---|
63 | // them or called InstallPatches() (which calls IsProcessPatched on every process). |
---|
64 | OSErr ForEachPatchedProcess(SCPatchIterationProc proc, void *data); |
---|
65 | |
---|
66 | private: |
---|
67 | CFBundleRef mBundle; |
---|
68 | CFStringRef mApplicationBundleIdentifier; |
---|
69 | |
---|
70 | private: |
---|
71 | list<SCPatchRecord> mPatchList; |
---|
72 | SCPatchContextList mPatchContextList; |
---|
73 | |
---|
74 | |
---|
75 | OSErr HandleMessage(const AppleEvent *theAE); |
---|
76 | static pascal OSStatus ApplicationEventHandler(EventHandlerCallRef handlerRef, |
---|
77 | EventRef event, void *userData); |
---|
78 | |
---|
79 | mach_error_t InstallPatchesInProcess(ProcessSerialNumber *psn, bool onDemand=false); |
---|
80 | OSErr AddPatchToParams(CFStringRef bundleID, CFURLRef url, SCPatchLoaderParams **params); |
---|
81 | mach_error_t InjectPatches(ProcessSerialNumber *psn, SCPatchLoaderParams *params); |
---|
82 | OSErr RecordPatchAndNotify(ProcessSerialNumber *psn, ProcessInfoRec *info); |
---|
83 | |
---|
84 | }; |
---|