-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv.WebService.PluginController.pas
203 lines (174 loc) · 6.14 KB
/
mv.WebService.PluginController.pas
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
unit mv.WebService.PluginController;
interface
type
TPluginController = class
private
public
function PluginResource(const HttpMethod, Path, Query: string): string;
function AssetResource(const HttpMethod, Path, Query: string): string;
function ReadLayoutValue(const HttpMethod, Path, Query: string): string;
function WriteLayoutValue(const HttpMethod, Path, Query, PostBody: string): string;
function DialogAlert(const HttpMethod, Path, Query, PostBody: string): string;
end;
implementation
uses
System.SysUtils, System.IOUtils, System.Classes, System.NetEncoding,
FMX.Dialogs,
mv.MainForm, mv.PluginManager, mv.StringUtils, mv.PluginService,
mv.PluginStorageService;
{ THomeController }
function TPluginController.PluginResource(const HttpMethod, Path, Query: string): string;
var
PluginManager: TPluginManager;
PluginName: string;
PluginResourceName: string;
Plugin: TPlugin;
I: integer;
Filename: string;
begin
Result := '';
PluginManager := frmStoneNotes.PluginManager;
PluginName := ExtractField(Path, '/', 3);
PluginResourceName := ExtractField(Path, '/', 4);
for I := 0 to PluginManager.PluginCount-1 do
begin
Plugin := PluginManager.Plugins[I];
if not SameText(Plugin.DirName, PluginName) then
Continue;
Filename := PluginManager.PluginsDirectory + '/' + Plugin.DirName + '/' + PluginResourceName;
if TFile.Exists(Filename) then
begin
// This key will be calculated on the client-side and passed in for any
// service requests triggered by this plugin instance
if '' = Plugin.ServiceContext then // Only set for the initializing HTML file
Plugin.ServiceContext := Path + Query; //TNetEncoding.Base64.Encode(Path + Query).Replace('=', '*').Replace('/', '-').Replace('+', '.');
Result := TFile.ReadAllText(Filename);
Exit;
end;
end;
end;
function TPluginController.AssetResource(const HttpMethod, Path, Query: string): string;
var
PluginManager: TPluginManager;
ResourceName: string;
Filename: string;
begin
Result := '';
PluginManager := frmStoneNotes.PluginManager;
ResourceName := ExtractField(Path, '/', 3);
Filename := PluginManager.PluginsDirectory.Replace('Plugins','Assets') + '/' + ResourceName;
if TFile.Exists(Filename) then
begin
Result := TFile.ReadAllText(Filename);
end;
end;
function TPluginController.ReadLayoutValue(const HttpMethod, Path, Query: string): string;
var
PluginManager: TPluginManager;
ResourceName: string;
Params: TStringList;
AContext: string;
AKey: string;
ADefault: string;
Plugin: TPlugin;
I: integer;
StorageService: TPluginStorageService;
begin
Result := '';
PluginManager := frmStoneNotes.PluginManager;
Params := TStringList.Create('"', '&');
try
Params.DelimitedText := Query;
AContext := Params.Values['AContext'].Replace('%2F', '/').Replace('%3D', '=');
AKey := Params.Values['AKey'];
ADefault := Params.Values['ADefault'];
if '' = ADefault then
ADefault := '{}';
// Look for a plugin with that ServiceContext
for I := 0 to PluginManager.PluginCount-1 do
begin
Plugin := PluginManager.Plugins[I];
if not SameText(Plugin.ServiceContext, AContext) then
Continue;
// Use the service instance on this plugin instance to perform the action
StorageService := Plugin.GetService('TPluginStorageService') as TPluginStorageService;
if Assigned(StorageService) then
Result := StorageService.ReadLayoutValue(AKey, ADefault);
end;
finally
Params.Free;
end;
end;
function TPluginController.WriteLayoutValue(const HttpMethod, Path, Query, PostBody: string): string;
var
PluginManager: TPluginManager;
ResourceName: string;
Params: TStringList;
AContext: string;
AKey: string;
AValue: string;
Plugin: TPlugin;
I: integer;
StorageService: TPluginStorageService;
ValLen: integer;
begin
Result := '';
PluginManager := frmStoneNotes.PluginManager;
Params := TStringList.Create(#0, '&');
Params.StrictDelimiter := true;
try
Params.DelimitedText := PostBody.Replace(' ', ' ') + '&' + Query.Replace(' ', ' ');
AContext := Params.Values['AContext'].Replace('%2F', '/').Replace('%3D', '=');
AKey := Params.Values['AKey'];
AValue := Params.Values['AValue'];
ValLen := Length(AValue);
// Look for a plugin with that ServiceContext
for I := 0 to PluginManager.PluginCount-1 do
begin
Plugin := PluginManager.Plugins[I];
if not SameText(Plugin.ServiceContext, AContext) then
Continue;
// Use the service instance on this plugin instance to perform the action
StorageService := Plugin.GetService('TPluginStorageService') as TPluginStorageService;
if Assigned(StorageService) then
StorageService.WriteLayoutValue(AKey, AValue);
Result := StorageService.ReadLayoutValue(AKey, '');
end;
finally
Params.Free;
end;
end;
function TPluginController.DialogAlert(const HttpMethod, Path, Query, PostBody: string): string;
var
PluginManager: TPluginManager;
ResourceName: string;
Params: TStringList;
AContext, AMessage: string;
Plugin: TPlugin;
I: integer;
begin
Result := '';
PluginManager := frmStoneNotes.PluginManager;
Params := TStringList.Create(#0, '&');
Params.StrictDelimiter := true;
try
Params.DelimitedText := PostBody.Replace(' ', ' ') + '&' + Query.Replace(' ', ' ');
AContext := Params.Values['AContext'].Replace('%2F', '/').Replace('%3D', '=');
AMessage := Params.Values['AMessage'];
// Look for a plugin with that ServiceContext, we need to do this even in
// Alert to try to make sure that only loaded plugins can trigger a msgbox
for I := 0 to PluginManager.PluginCount-1 do
begin
Plugin := PluginManager.Plugins[I];
if not SameText(Plugin.ServiceContext, AContext) then
Continue;
TThread.Synchronize(Nil, procedure
begin
ShowMessage(AMessage);
end);
end;
finally
Params.Free;
end;
end;
end.