2016-05-04 20:00:03 +02:00
|
|
|
// Copyright (c) 2016 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/main_message_loop_external_pump.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
#import <AppKit/AppKit.h>
|
2017-05-17 11:29:28 +02:00
|
|
|
#import <Foundation/Foundation.h>
|
2016-05-04 20:00:03 +02:00
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
#include "include/cef_app.h"
|
|
|
|
|
|
|
|
@class EventHandler;
|
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
class MainMessageLoopExternalPumpMac : public MainMessageLoopExternalPump {
|
|
|
|
public:
|
|
|
|
MainMessageLoopExternalPumpMac();
|
|
|
|
~MainMessageLoopExternalPumpMac();
|
|
|
|
|
|
|
|
// MainMessageLoopStd methods:
|
2021-06-17 21:43:06 +02:00
|
|
|
void Quit() override;
|
|
|
|
int Run() override;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// MainMessageLoopExternalPump methods:
|
2023-06-01 16:06:15 +02:00
|
|
|
void OnScheduleMessagePumpWork(int64_t delay_ms) override;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// Internal methods used for processing the event callbacks. They are public
|
|
|
|
// for simplicity but should not be used directly.
|
2023-06-01 16:06:15 +02:00
|
|
|
void HandleScheduleWork(int64_t delay_ms);
|
2016-05-04 20:00:03 +02:00
|
|
|
void HandleTimerTimeout();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
// MainMessageLoopExternalPump methods:
|
2023-06-01 16:06:15 +02:00
|
|
|
void SetTimer(int64_t delay_ms) override;
|
2021-06-17 21:43:06 +02:00
|
|
|
void KillTimer() override;
|
|
|
|
bool IsTimerPending() override { return timer_ != nil; }
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Owner thread that will run events.
|
|
|
|
NSThread* owner_thread_;
|
|
|
|
|
|
|
|
// Pending work timer.
|
|
|
|
NSTimer* timer_;
|
|
|
|
|
|
|
|
// Used to handle event callbacks on the owner thread.
|
|
|
|
EventHandler* event_handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace client
|
|
|
|
|
|
|
|
// Object that handles event callbacks on the owner thread.
|
|
|
|
@interface EventHandler : NSObject {
|
|
|
|
@private
|
|
|
|
client::MainMessageLoopExternalPumpMac* pump_;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (id)initWithPump:(client::MainMessageLoopExternalPumpMac*)pump;
|
|
|
|
- (void)scheduleWork:(NSNumber*)delay_ms;
|
|
|
|
- (void)timerTimeout:(id)obj;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation EventHandler
|
|
|
|
|
|
|
|
- (id)initWithPump:(client::MainMessageLoopExternalPumpMac*)pump {
|
|
|
|
if (self = [super init]) {
|
|
|
|
pump_ = pump;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)scheduleWork:(NSNumber*)delay_ms {
|
|
|
|
pump_->HandleScheduleWork([delay_ms integerValue]);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)timerTimeout:(id)obj {
|
|
|
|
pump_->HandleTimerTimeout();
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
MainMessageLoopExternalPumpMac::MainMessageLoopExternalPumpMac()
|
2019-04-23 19:17:56 +02:00
|
|
|
: owner_thread_([NSThread currentThread]), timer_(nil) {
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
[owner_thread_ retain];
|
|
|
|
#endif // !__has_feature(objc_arc)
|
|
|
|
event_handler_ = [[EventHandler alloc] initWithPump:this];
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
MainMessageLoopExternalPumpMac::~MainMessageLoopExternalPumpMac() {
|
|
|
|
KillTimer();
|
2019-04-23 19:17:56 +02:00
|
|
|
#if !__has_feature(objc_arc)
|
2016-05-04 20:00:03 +02:00
|
|
|
[owner_thread_ release];
|
|
|
|
[event_handler_ release];
|
2019-04-23 19:17:56 +02:00
|
|
|
#endif // !__has_feature(objc_arc)
|
|
|
|
owner_thread_ = nil;
|
|
|
|
event_handler_ = nil;
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopExternalPumpMac::Quit() {
|
|
|
|
[NSApp stop:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainMessageLoopExternalPumpMac::Run() {
|
|
|
|
// Run the message loop.
|
|
|
|
[NSApp run];
|
|
|
|
|
|
|
|
KillTimer();
|
|
|
|
|
|
|
|
// We need to run the message pump until it is idle. However we don't have
|
|
|
|
// that information here so we run the message loop "for a while".
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
// Let default runloop observers run.
|
|
|
|
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, 1);
|
|
|
|
|
|
|
|
// Do some work.
|
|
|
|
CefDoMessageLoopWork();
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
// Sleep to allow the CEF proc to do work.
|
2017-05-17 11:29:28 +02:00
|
|
|
[NSThread sleepForTimeInterval:0.05];
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void MainMessageLoopExternalPumpMac::OnScheduleMessagePumpWork(
|
|
|
|
int64_t delay_ms) {
|
2016-05-04 20:00:03 +02:00
|
|
|
// This method may be called on any thread.
|
|
|
|
NSNumber* number = [NSNumber numberWithInt:static_cast<int>(delay_ms)];
|
|
|
|
[event_handler_ performSelector:@selector(scheduleWork:)
|
|
|
|
onThread:owner_thread_
|
|
|
|
withObject:number
|
|
|
|
waitUntilDone:NO];
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void MainMessageLoopExternalPumpMac::HandleScheduleWork(int64_t delay_ms) {
|
2016-05-04 20:00:03 +02:00
|
|
|
OnScheduleWork(delay_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopExternalPumpMac::HandleTimerTimeout() {
|
|
|
|
OnTimerTimeout();
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void MainMessageLoopExternalPumpMac::SetTimer(int64_t delay_ms) {
|
2016-05-04 20:00:03 +02:00
|
|
|
DCHECK_GT(delay_ms, 0);
|
|
|
|
DCHECK(!timer_);
|
|
|
|
|
|
|
|
const double delay_s = static_cast<double>(delay_ms) / 1000.0;
|
2019-04-23 19:17:56 +02:00
|
|
|
timer_ = [NSTimer timerWithTimeInterval:delay_s
|
|
|
|
target:event_handler_
|
|
|
|
selector:@selector(timerTimeout:)
|
|
|
|
userInfo:nil
|
|
|
|
repeats:NO];
|
|
|
|
#if !__has_feature(objc_arc)
|
|
|
|
[timer_ retain];
|
|
|
|
#endif // !__has_feature(objc_arc)
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// Add the timer to default and tracking runloop modes.
|
|
|
|
NSRunLoop* owner_runloop = [NSRunLoop currentRunLoop];
|
2017-05-17 11:29:28 +02:00
|
|
|
[owner_runloop addTimer:timer_ forMode:NSRunLoopCommonModes];
|
|
|
|
[owner_runloop addTimer:timer_ forMode:NSEventTrackingRunLoopMode];
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopExternalPumpMac::KillTimer() {
|
|
|
|
if (timer_ != nil) {
|
|
|
|
[timer_ invalidate];
|
2019-04-23 19:17:56 +02:00
|
|
|
#if !__has_feature(objc_arc)
|
2016-05-04 20:00:03 +02:00
|
|
|
[timer_ release];
|
2019-04-23 19:17:56 +02:00
|
|
|
#endif // !__has_feature(objc_arc)
|
2016-05-04 20:00:03 +02:00
|
|
|
timer_ = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<MainMessageLoopExternalPump>
|
|
|
|
MainMessageLoopExternalPump::Create() {
|
|
|
|
return std::make_unique<MainMessageLoopExternalPumpMac>();
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace client
|