2012-10-03 00:04:58 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
// Original code by: ekr@rtfm.com
|
|
|
|
|
|
|
|
// Implementation of the NR timer interface
|
|
|
|
|
2013-01-06 02:58:59 +04:00
|
|
|
// Some code here copied from nrappkit. The license was.
|
|
|
|
|
|
|
|
/**
|
|
|
|
Copyright (C) 2004, Network Resonance, Inc.
|
|
|
|
Copyright (C) 2006, Network Resonance, Inc.
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions
|
|
|
|
are met:
|
|
|
|
|
|
|
|
1. Redistributions of source code must retain the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
notice, this list of conditions and the following disclaimer in the
|
|
|
|
documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of Network Resonance, Inc. nor the name of any
|
|
|
|
contributors to this software may be used to endorse or promote
|
|
|
|
products derived from this software without specific prior written
|
|
|
|
permission.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
|
|
|
|
ekr@rtfm.com Sun Feb 22 19:35:24 2004
|
|
|
|
*/
|
|
|
|
|
2013-02-11 11:23:02 +04:00
|
|
|
#include <string>
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2013-02-19 21:01:12 +04:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
#include "nsIEventTarget.h"
|
|
|
|
#include "nsITimer.h"
|
|
|
|
#include "nsNetCID.h"
|
2013-02-19 21:01:12 +04:00
|
|
|
#include "runnable_utils.h"
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "nr_api.h"
|
|
|
|
#include "async_timer.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
class nrappkitTimerCallback : public nsITimerCallback
|
|
|
|
{
|
|
|
|
public:
|
2013-07-19 06:23:32 +04:00
|
|
|
// We're going to release ourself in the callback, so we need to be threadsafe
|
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2012-10-03 00:04:58 +04:00
|
|
|
NS_DECL_NSITIMERCALLBACK
|
|
|
|
|
2013-02-11 11:23:02 +04:00
|
|
|
nrappkitTimerCallback(NR_async_cb cb, void *cb_arg,
|
|
|
|
const char *function, int line)
|
|
|
|
: cb_(cb), cb_arg_(cb_arg), function_(function), line_(line) {
|
|
|
|
}
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~nrappkitTimerCallback() {}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
/* additional members */
|
|
|
|
NR_async_cb cb_;
|
|
|
|
void *cb_arg_;
|
2013-02-11 11:23:02 +04:00
|
|
|
std::string function_;
|
|
|
|
int line_;
|
2012-10-03 00:04:58 +04:00
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nrappkitTimerCallback, nsITimerCallback)
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nrappkitTimerCallback::Notify(nsITimer *timer) {
|
2013-02-11 11:23:02 +04:00
|
|
|
r_log(LOG_GENERIC, LOG_DEBUG, "Timer callback fired (set in %s:%d)",
|
|
|
|
function_.c_str(), line_);
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
cb_(0, 0, cb_arg_);
|
|
|
|
|
|
|
|
// Allow the timer to go away.
|
|
|
|
timer->Release();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} // close namespace
|
|
|
|
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-02-19 21:01:12 +04:00
|
|
|
// These timers must only be used from the STS thread.
|
|
|
|
// This function is a helper that enforces that.
|
|
|
|
static void CheckSTSThread() {
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIEventTarget> sts_thread;
|
|
|
|
|
|
|
|
sts_thread = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv);
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
ASSERT_ON_THREAD(sts_thread);
|
|
|
|
}
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
int NR_async_timer_set(int timeout, NR_async_cb cb, void *arg, char *func,
|
|
|
|
int l, void **handle) {
|
|
|
|
nsresult rv;
|
2013-02-19 21:01:12 +04:00
|
|
|
CheckSTSThread();
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsITimer> timer = do_CreateInstance(NS_TIMER_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return(R_FAILED);
|
|
|
|
}
|
|
|
|
|
2013-02-11 11:23:02 +04:00
|
|
|
rv = timer->InitWithCallback(new nrappkitTimerCallback(cb, arg, func, l),
|
|
|
|
timeout, nsITimer::TYPE_ONE_SHOT);
|
2012-10-03 00:04:58 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return R_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need an AddRef here to keep the timer alive, per the spec.
|
|
|
|
timer->AddRef();
|
|
|
|
|
|
|
|
if (handle)
|
|
|
|
*handle = timer.get();
|
2012-12-15 03:16:29 +04:00
|
|
|
// Bug 818806: if we have no handle to the timer, we have no way to avoid
|
|
|
|
// it leaking (though not the callback object) if it never fires (or if
|
|
|
|
// we exit before it fires).
|
2012-10-03 00:04:58 +04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int NR_async_schedule(NR_async_cb cb, void *arg, char *func, int l) {
|
2013-02-19 21:01:12 +04:00
|
|
|
// No need to check the thread because we check it next in the
|
|
|
|
// timer set.
|
2012-10-03 00:04:58 +04:00
|
|
|
return NR_async_timer_set(0, cb, arg, func, l, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
int NR_async_timer_cancel(void *handle) {
|
2013-04-17 06:29:23 +04:00
|
|
|
// Check for the handle being nonzero because sometimes we get
|
|
|
|
// no-op cancels that aren't on the STS thread. This can be
|
|
|
|
// non-racy as long as the upper-level code is careful.
|
2013-01-02 03:14:28 +04:00
|
|
|
if (!handle)
|
|
|
|
return 0;
|
|
|
|
|
2013-04-17 06:29:23 +04:00
|
|
|
CheckSTSThread();
|
|
|
|
|
2012-10-03 00:04:58 +04:00
|
|
|
nsITimer *timer = static_cast<nsITimer *>(handle);
|
|
|
|
|
|
|
|
timer->Cancel();
|
|
|
|
// Allow the timer to go away.
|
|
|
|
timer->Release();
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|