forked from rdkcentral/rdkservices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPerformanceMetrics.h
693 lines (570 loc) · 23.3 KB
/
PerformanceMetrics.h
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2020 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "Module.h"
#include <interfaces/IMemory.h>
#include <interfaces/IBrowser.h>
#include <memory>
namespace WPEFramework {
namespace Plugin {
class PerformanceMetrics : public PluginHost::IPlugin {
private:
class Config : public Core::JSON::Container {
public:
Config(const Config&);
Config& operator=(const Config&);
public:
Config()
: Core::JSON::Container()
, ObservableCallsign()
, ObservableClassname()
{
Add(_T("callsign"), &ObservableCallsign);
Add(_T("classname"), &ObservableClassname);
}
public:
Core::JSON::String ObservableCallsign;
Core::JSON::String ObservableClassname;
};
class Notification : public PluginHost::IPlugin::INotification {
public:
Notification(const Notification&) = delete;
Notification& operator=(const Notification&) = delete;
explicit Notification(PerformanceMetrics& parent)
: PluginHost::IPlugin::INotification()
, _parent(parent)
{
}
~Notification() override = default;
void Activated (const string&, PluginHost::IShell* service) override
{
ASSERT(service != nullptr);
_parent.PluginActivated(*service);
}
void Deactivated (const string&, PluginHost::IShell* service) override
{
ASSERT(service != nullptr);
_parent.PluginDeactivated(*service);
}
void Unavailable(const string&, PluginHost::IShell*) override
{
}
BEGIN_INTERFACE_MAP(Notification)
INTERFACE_ENTRY(PluginHost::IPlugin::INotification)
END_INTERFACE_MAP
private:
PerformanceMetrics& _parent;
};
private:
template<class LOGGERINTERFACE>
static std::unique_ptr<LOGGERINTERFACE> LoggerFactory();
public:
class IBasicMetricsLogger {
public:
virtual ~IBasicMetricsLogger() = default;
virtual void Enable(PluginHost::IShell& service, const string& callsign) = 0;
virtual void Disable() = 0;
virtual void Activated() = 0;
virtual void Deactivated(const uint32_t uptime_s) = 0;
};
struct IStateMetricsLogger : public IBasicMetricsLogger {
public:
virtual void Resumed() = 0;
virtual void Suspended() = 0;
};
struct IBrowserMetricsLogger : public IStateMetricsLogger {
public:
static constexpr char startURL[] = _T("about:blank");
virtual void LoadFinished(const string& URL, const int32_t httpstatus, const bool success, const uint32_t totalsuccess, const uint32_t totalfailed) = 0;
virtual void URLChange(const string& URL, const bool loaded) = 0;
virtual void VisibilityChange(const bool hidden) = 0;
virtual void PageClosure() = 0;
};
private:
struct IObservable {
virtual ~IObservable() = default;
// do not call this Initialize and Deinitialize :)
virtual void Enable() = 0;
virtual void Disable() = 0;
virtual void Activated(PluginHost::IShell&) = 0;
virtual void Deactivated(PluginHost::IShell&) = 0;
};
private:
struct IPerfMetricsHandler {
virtual ~IPerfMetricsHandler() = default;
virtual void Initialize() = 0;
virtual void Deinitialize() = 0;
virtual void Activated(PluginHost::IShell&) = 0;
virtual void Deactivated(PluginHost::IShell&) = 0;
};
class CallsignPerfMetricsHandler : public IPerfMetricsHandler
{
public:
CallsignPerfMetricsHandler(const string& callsign)
: IPerfMetricsHandler()
, _callsign(callsign)
, _observable()
{
}
~CallsignPerfMetricsHandler() override
{
ASSERT(_observable.IsValid() == false);
}
CallsignPerfMetricsHandler(const CallsignPerfMetricsHandler&) = delete;
CallsignPerfMetricsHandler& operator=(const CallsignPerfMetricsHandler&) = delete;
const string& Callsign() const
{
return _callsign;
}
void Initialize() override
{
ASSERT(_observable.IsValid() == false);
}
void Deinitialize() override
{
if( _observable.IsValid() == true ) {
_observable->Disable();
VARIABLE_IS_NOT_USED uint32_t result =_observable.Release();
ASSERT(result == Core::ERROR_DESTRUCTION_SUCCEEDED);
ASSERT(_observable.IsValid() == false);
}
}
void Activated(PluginHost::IShell& service) override
{
if( service.Callsign() == _callsign ) {
ASSERT(_observable.IsValid() == false);
CreateObservable(service);
_observable->Activated(service);
}
}
void Deactivated(PluginHost::IShell& service) override
{
if( service.Callsign() == _callsign ) {
ASSERT(_observable.IsValid() == true);
_observable->Deactivated(service);
_observable->Disable();
VARIABLE_IS_NOT_USED uint32_t result =_observable.Release();
ASSERT(result == Core::ERROR_DESTRUCTION_SUCCEEDED);
ASSERT(_observable.IsValid() == false);
}
}
private:
void CreateObservable(PluginHost::IShell& service);
private:
string _callsign;
Core::ProxyType<IObservable> _observable;
};
class ClassnamePerfMetricsHandler : public IPerfMetricsHandler
{
public:
ClassnamePerfMetricsHandler(const string& classname)
: IPerfMetricsHandler()
, _classname(classname)
, _observers()
, _adminLock()
{
}
~ClassnamePerfMetricsHandler() override
{
ASSERT(_observers.empty() == true);
}
ClassnamePerfMetricsHandler(const ClassnamePerfMetricsHandler&) = delete;
ClassnamePerfMetricsHandler& operator=(const ClassnamePerfMetricsHandler&) = delete;
const string& Classname() const
{
return _classname;
}
void Initialize() override
{
ASSERT(_observers.empty() == true);
}
void Deinitialize() override
{
// no lock needed, no notification are possible here.
for( auto& observer : _observers ) {
observer.second.Deinitialize();
}
_observers.clear();
}
void Activated(PluginHost::IShell& service) override
{
if( service.ClassName() == Classname() ) {
_adminLock.Lock();
auto result =_observers.emplace(std::piecewise_construct,
std::forward_as_tuple(service.Callsign()),
std::forward_as_tuple(service.Callsign()));
ASSERT( ( result.second == true ) && ( result.first != _observers.end() ) );
result.first->second.Initialize();
result.first->second.Activated(service);
_adminLock.Unlock();
}
}
void Deactivated(PluginHost::IShell& service) override
{
if( service.ClassName() == Classname() ) {
_adminLock.Lock();
auto it =_observers.find(service.Callsign());
if( it != _observers.end() ) {
it->second.Deactivated(service);
it->second.Deinitialize();
_observers.erase(it);
}
_adminLock.Unlock();
}
}
private:
using OberserverMap = std::unordered_map<string, CallsignPerfMetricsHandler>;
string _classname;
OberserverMap _observers;
mutable Core::CriticalSection _adminLock;
};
private:
// we cannot make the Logger a static and get it via Instance or something similar as the Plugin (and therefore the library)
// might be used multipe times fot different callsigns and the they would share the same Logger instance (and Logger state)
template<class LOGGERINTERFACE>
class LoggerProxy {
public:
LoggerProxy() : _logger(LoggerFactory<LOGGERINTERFACE>()) {}
~LoggerProxy() = default;
LOGGERINTERFACE& Logger()
{
ASSERT(_logger.get() != nullptr);
return *_logger;
}
const LOGGERINTERFACE& Logger() const
{
ASSERT(_logger.get() != nullptr);
return *_logger;
}
private:
std::unique_ptr<LOGGERINTERFACE> _logger;
};
template<class LOGGERINTERFACE = IBasicMetricsLogger>
class BasicObservable : public IObservable, protected LoggerProxy<LOGGERINTERFACE> {
protected:
// make Logger accessable here, so we don't have to put this-> in front of it everywhere
using LoggerProxy<LOGGERINTERFACE>::Logger;
public:
BasicObservable(const BasicObservable&) = delete;
BasicObservable& operator=(const BasicObservable&) = delete;
BasicObservable(CallsignPerfMetricsHandler& parent, PluginHost::IShell& service)
: IObservable()
, LoggerProxy<LOGGERINTERFACE>()
, _parent(parent)
, _activatetime(0)
, _service(&service)
{
_service->AddRef();
}
~BasicObservable() override
{
ASSERT(_service == nullptr);
}
// do not call this Initialize and Deinitialize :)
void Enable() override
{
ASSERT(_service != nullptr);
ASSERT(_service->Callsign() == Parent().Callsign());
Logger().Enable(*_service, Parent().Callsign());
}
void Disable() override
{
ASSERT(_service != nullptr);
Logger().Disable();
_service->Release();
_service = nullptr;
}
void Activated(PluginHost::IShell&) override
{
_activatetime = Core::Time::Now().Ticks();
Logger().Activated();
}
void Deactivated(PluginHost::IShell&) override
{
Logger().Deactivated( Uptime() );
}
uint64_t ActivateTime() const
{
return _activatetime;
}
uint32_t Uptime() const
{
return ( Core::Time::Now().Ticks() - ActivateTime() ) / Core::Time::MicroSecondsPerSecond;
}
CallsignPerfMetricsHandler& Parent() const
{
return _parent;
}
PluginHost::IShell* Service () const {
return _service;
}
private:
CallsignPerfMetricsHandler& _parent;
uint64_t _activatetime;
PluginHost::IShell* _service;
};
template<class LOGGERINTERFACE = IStateMetricsLogger>
class StateObservable : public BasicObservable<LOGGERINTERFACE>, public PluginHost::IStateControl::INotification {
private:
using Base = BasicObservable<LOGGERINTERFACE>;
protected:
// make Logger accessable here, so we don't have to put this-> in front of it everywhere
using LoggerProxy<LOGGERINTERFACE>::Logger;
public:
StateObservable(const StateObservable&) = delete;
StateObservable& operator=(const StateObservable&) = delete;
StateObservable(CallsignPerfMetricsHandler& parent, PluginHost::IShell& service, PluginHost::IStateControl* statecontrol)
: Base(parent, service)
, PluginHost::IStateControl::INotification()
, _statecontrol(statecontrol)
{
// not very likely but it could happen that we have a Browser without StatControl
if(_statecontrol != nullptr) {
TRACE(Trace::Information, (_T("Observable supports Statecontrol")) );
_statecontrol->AddRef();
}
}
~StateObservable() override
{
ASSERT(_statecontrol == nullptr);
}
void Enable() override
{
Base::Enable();
if(_statecontrol != nullptr) {
_statecontrol->Register(this);
}
}
void Disable() override
{
Cleanup();
Base::Disable();
}
void StateChange(const PluginHost::IStateControl::state state) override
{
if( state == PluginHost::IStateControl::state::RESUMED ) {
Logger().Resumed();
} else if( state == PluginHost::IStateControl::state::SUSPENDED ) {
Logger().Suspended();
}
}
BEGIN_INTERFACE_MAP(StateObservable)
INTERFACE_ENTRY(PluginHost::IStateControl::INotification)
END_INTERFACE_MAP
private:
void Cleanup()
{
if(_statecontrol != nullptr) {
_statecontrol->Unregister(this);
_statecontrol->Release();
_statecontrol = nullptr;
}
}
private:
PluginHost::IStateControl* _statecontrol;
};
template<class LOGGERINTERFACE = IBrowserMetricsLogger>
class BrowserObservable : public StateObservable<LOGGERINTERFACE>, public Exchange::IBrowser::INotification {
private:
using Base = StateObservable<LOGGERINTERFACE>;
protected:
// make Logger accessable here, so we don't have to put this-> in front of it everywhere
using LoggerProxy<LOGGERINTERFACE>::Logger;
public:
BrowserObservable(const BrowserObservable&) = delete;
BrowserObservable& operator=(const BrowserObservable&) = delete;
BrowserObservable(CallsignPerfMetricsHandler& parent,
PluginHost::IShell& service,
Exchange::IBrowser& browser,
PluginHost::IStateControl* statecontrol)
: Base(parent, service, statecontrol)
, Exchange::IBrowser::INotification()
, _browser(&browser)
, _nbrloaded(0)
{
_browser->AddRef();
}
~BrowserObservable() override
{
ASSERT(_browser = nullptr);
}
void Enable() override
{
ASSERT(_browser != nullptr);
Base::Enable();
_browser->Register(this);
_nbrloaded = 0;
}
void Disable() override
{
ASSERT(_browser != nullptr);
_browser->Unregister(this);
_browser->Release();
_browser = nullptr;
Base::Disable();
}
BEGIN_INTERFACE_MAP(BrowserObservable)
INTERFACE_ENTRY(PluginHost::IStateControl::INotification)
INTERFACE_ENTRY(Exchange::IBrowser::INotification)
END_INTERFACE_MAP
void LoadFinished(const string& URL) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
++_nbrloaded;
}
Logger().LoadFinished(URL, 0, true, _nbrloaded, 0);
}
void URLChanged(const string& URL) override
{
Logger().URLChange(URL, false);
}
void Hidden(const bool hidden) override
{
Logger().VisibilityChange(hidden);
}
void Closure() override
{
Logger().PageClosure();
}
private:
Exchange::IBrowser* _browser;
uint32_t _nbrloaded;
};
template<class LOGGERINTERFACE = IBrowserMetricsLogger>
class WebBrowserObservable : public StateObservable<LOGGERINTERFACE>, public Exchange::IWebBrowser::INotification {
private:
using Base = StateObservable<LOGGERINTERFACE>;
protected:
// make Logger accessable here, so we don't have to out this-> in front of it everywhere
using LoggerProxy<LOGGERINTERFACE>::Logger;
public:
WebBrowserObservable(const WebBrowserObservable&) = delete;
WebBrowserObservable& operator=(const WebBrowserObservable&) = delete;
WebBrowserObservable(CallsignPerfMetricsHandler& parent,
PluginHost::IShell& service,
Exchange::IWebBrowser& browser,
PluginHost::IStateControl* statecontrol)
: Base(parent, service, statecontrol)
, Exchange::IWebBrowser::INotification()
, _browser(&browser)
, _nbrloadedsuccess(0)
, _nbrloadedfailed(0)
{
_browser->AddRef();
}
~WebBrowserObservable() override
{
ASSERT(_browser == nullptr);
}
void Enable() override
{
ASSERT(_browser != nullptr);
Base::Enable();
_browser->Register(this);
_nbrloadedsuccess = 0;
_nbrloadedfailed = 0;
}
void Disable() override
{
ASSERT(_browser != nullptr);
_browser->Unregister(this);
_browser->Release();
_browser = nullptr;
Base::Disable();
}
BEGIN_INTERFACE_MAP(BrowserObservable)
INTERFACE_ENTRY(PluginHost::IStateControl::INotification)
INTERFACE_ENTRY(Exchange::IWebBrowser::INotification)
END_INTERFACE_MAP
void LoadFinished(const string& URL, const int32_t httpstatus) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
++_nbrloadedsuccess;
}
Logger().LoadFinished(URL, httpstatus, true, _nbrloadedsuccess, _nbrloadedfailed);
}
void LoadFailed(const string& URL) override
{
if( URL != IBrowserMetricsLogger::startURL ) {
++_nbrloadedfailed;
}
Logger().LoadFinished(URL, 0, false, _nbrloadedsuccess, _nbrloadedfailed);
}
void URLChange(const string& URL, const bool loaded) override
{
Logger().URLChange(URL, loaded);
}
void VisibilityChange(const bool hidden) override
{
Logger().VisibilityChange(hidden);
}
void PageClosure() override
{
Logger().PageClosure();
}
void BridgeQuery(const string&) override
{
}
private:
Exchange::IWebBrowser* _browser;
uint32_t _nbrloadedsuccess;
uint32_t _nbrloadedfailed;
};
public:
PUSH_WARNING(DISABLE_WARNING_THIS_IN_MEMBER_INITIALIZER_LIST)
PerformanceMetrics()
: PluginHost::IPlugin()
, _notification(*this)
, _handler()
{
}
POP_WARNING()
~PerformanceMetrics() override = default;
PerformanceMetrics(const PerformanceMetrics&) = delete;
PerformanceMetrics& operator=(const PerformanceMetrics&) = delete;
BEGIN_INTERFACE_MAP(PerformanceMetrics)
INTERFACE_ENTRY(PluginHost::IPlugin)
END_INTERFACE_MAP
public:
// IPlugin methods
// -------------------------------------------------------------------------------------------------------
// First time initialization. Whenever a plugin is loaded, it is offered a Service object with relevant
// information and services for this particular plugin. The Service object contains configuration information that
// can be used to initialize the plugin correctly. If Initialization succeeds, return nothing (empty string)
// If there is an error, return a string describing the issue why the initialisation failed.
// The Service object is *NOT* reference counted, lifetime ends if the plugin is deactivated.
// The lifetime of the Service object is guaranteed till the deinitialize method is called.
virtual const string Initialize(PluginHost::IShell* service);
// The plugin is unloaded from WPEFramework. This is call allows the module to notify clients
// or to persist information if needed. After this call the plugin will unlink from the service path
// and be deactivated. The Service object is the same as passed in during the Initialize.
// After theis call, the lifetime of the Service object ends.
virtual void Deinitialize(PluginHost::IShell* service);
// Returns an interface to a JSON struct that can be used to return specific metadata information with respect
// to this plugin. This Metadata can be used by the MetData plugin to publish this information to the ouside world.
virtual string Information() const;
private:
void PluginActivated(PluginHost::IShell& service);
void PluginDeactivated(PluginHost::IShell& service);
private:
Core::Sink<Notification> _notification;
std::unique_ptr<IPerfMetricsHandler> _handler;
};
}
}