void UpdateSubscriptionStatus(const AdblockPlus::SubscriptionPtr subscription)
{
    D(D_WARN, "UpdateSubscriptionStatus()");

    std::string downloadStatus = subscription->GetProperty("downloadStatus")->IsNull() ? "" : subscription->GetProperty("downloadStatus")->AsString();
    int64_t lastDownload = subscription->GetProperty("lastDownload")->AsInt();

    std::string status = "synchronize_never";
    int64_t time = 0;

    if (subscription->IsUpdating())
    {
        status = "synchronize_in_progress";
    }
    else if (!downloadStatus.empty() && downloadStatus != "synchronize_ok")
    {
        status = downloadStatus;
    }
    else if (lastDownload > 0)
    {
        time = lastDownload;
        status = "synchronize_last_at";
    }

    JNIEnv* jniEnv = NULL;
    int stat = globalJvm->GetEnv((void **)&jniEnv, JNI_VERSION_1_6);
    if (stat == JNI_EDETACHED)
    {
        if (globalJvm->AttachCurrentThread(&jniEnv, NULL) != 0)
            throw std::runtime_error("Failed to get JNI environment");
    }

    jstring jUrl = jniEnv->NewStringUTF(subscription->GetProperty("url")->AsString().c_str());
    jstring jStatus = jniEnv->NewStringUTF(status.c_str());
    jlong jTime = time * 1000;

    static jclass cls = jniEnv->GetObjectClass(jniObject);
    static jmethodID mid = jniEnv->GetMethodID(cls, "onFilterChanged", "(Ljava/lang/String;Ljava/lang/String;J)V");
    if (mid)
        jniEnv->CallVoidMethod(jniObject, mid, jUrl, jStatus, jTime);
    jniEnv->DeleteLocalRef(jUrl);
    jniEnv->DeleteLocalRef(jStatus);

    if (stat == JNI_EDETACHED)
        globalJvm->DetachCurrentThread();
}
Пример #2
0
TEST_F(FilterEngineTest, SubscriptionUpdates)
{
  AdblockPlus::SubscriptionPtr subscription = filterEngine->GetSubscription("foo");
  ASSERT_FALSE(subscription->IsUpdating());
  subscription->UpdateFilters();
}