Esempio n. 1
0
TEST_F(FilterEngineTest, FilterProperties)
{
  AdblockPlus::FilterPtr filter = filterEngine->GetFilter("foo");

  ASSERT_TRUE(filter->GetProperty("stringFoo")->IsUndefined());
  ASSERT_TRUE(filter->GetProperty("intFoo")->IsUndefined());
  ASSERT_TRUE(filter->GetProperty("boolFoo")->IsUndefined());

  filter->SetProperty("stringFoo", "y");
  filter->SetProperty("intFoo", 24);
  filter->SetProperty("boolFoo", true);
  ASSERT_EQ("y", filter->GetProperty("stringFoo")->AsString());
  ASSERT_EQ(24, filter->GetProperty("intFoo")->AsInt());
  ASSERT_TRUE(filter->GetProperty("boolFoo")->AsBool());
}
AdblockPlus::FilterPtr FilterEngine::Matches(const std::string& url,
    ContentType contentType,
    const std::vector<std::string>& documentUrls) const
{
  if (documentUrls.empty())
    return CheckFilterMatch(url, contentType, "");

  std::string lastDocumentUrl = documentUrls.front();
  for (std::vector<std::string>::const_iterator it = documentUrls.begin();
       it != documentUrls.end(); it++) {
    const std::string documentUrl = *it;
    AdblockPlus::FilterPtr match = CheckFilterMatch(documentUrl,
                                                    CONTENT_TYPE_DOCUMENT,
                                                    lastDocumentUrl);
    if (match && match->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION)
      return match;
    lastDocumentUrl = documentUrl;
  }

  return CheckFilterMatch(url, contentType, lastDocumentUrl);
}
Esempio n. 3
0
TEST_F(FilterEngineTest, AddRemoveFilters)
{
  ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
  AdblockPlus::FilterPtr filter = filterEngine->GetFilter("foo");
  ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
  ASSERT_FALSE(filter->IsListed());
  filter->AddToList();
  ASSERT_EQ(1u, filterEngine->GetListedFilters().size());
  ASSERT_EQ(*filter, *filterEngine->GetListedFilters()[0]);
  ASSERT_TRUE(filter->IsListed());
  filter->AddToList();
  ASSERT_EQ(1u, filterEngine->GetListedFilters().size());
  ASSERT_EQ(*filter, *filterEngine->GetListedFilters()[0]);
  ASSERT_TRUE(filter->IsListed());
  filter->RemoveFromList();
  ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
  ASSERT_FALSE(filter->IsListed());
  filter->RemoveFromList();
  ASSERT_EQ(0u, filterEngine->GetListedFilters().size());
  ASSERT_FALSE(filter->IsListed());
}
JNIEXPORT jboolean JNICALL Java_org_adblockplus_android_ABPEngine_matches(
    JNIEnv *pEnv, jobject, jstring url, jstring contentType, jobjectArray documentUrls)
{
    try
    {
        const std::string surl = GetString(pEnv, url);
        const std::string stype = GetString(pEnv, contentType);
        const int documentUrlsLength = pEnv->GetArrayLength(documentUrls);
        std::vector<std::string> sdocumentUrls;
        for(int i = 0; i < documentUrlsLength; i++)
        {
            jstring documentUrl = static_cast<jstring>(pEnv->GetObjectArrayElement(documentUrls, i));
            sdocumentUrls.push_back(GetString(pEnv, documentUrl));
        }

        AdblockPlus::FilterPtr filter = filterEngine->Matches(surl, stype, sdocumentUrls);

        if (! filter)
            return JNI_FALSE;

        // hack: if there is no referrer, block only if filter is domain-specific
        // (to re-enable in-app ads blocking, proposed on 12.11.2012 Monday meeting)
        // (documentUrls contains the referrers on Android)
        if (!sdocumentUrls.size() &&
                (filter->GetProperty("text")->AsString()).find("||") != std::string::npos)
            return JNI_FALSE;

        return filter->GetType() == AdblockPlus::Filter::TYPE_EXCEPTION ? JNI_FALSE : JNI_TRUE;
    }
    catch (const std::exception& e)
    {
        ThrowJavaException(pEnv, e);
    }
    catch (...)
    {
        ThrowJavaException(pEnv);
    }
    return JNI_FALSE;
}
static jobject JNICALL JniMatches(JNIEnv* env, jclass clazz, jlong ptr, jstring jUrl,
    jobjectArray jContentTypes, jstring jDocumentUrl)
{
  AdblockPlus::FilterEngine& engine = GetFilterEngineRef(ptr);

  std::string url = JniJavaToStdString(env, jUrl);
  AdblockPlus::FilterEngine::ContentTypeMask contentTypeMask = 0;

  int contentTypesSize = env->GetArrayLength(jContentTypes);
  for (int i = 0; i < contentTypesSize; i++)
  {
    contentTypeMask |= ConvertContentType(env, env->GetObjectArrayElement(jContentTypes, i));
  }
  std::string documentUrl = JniJavaToStdString(env, jDocumentUrl);

  try
  {
    AdblockPlus::FilterPtr filterPtr = engine.Matches(url, contentTypeMask, documentUrl);

    return filterPtr.get() ? NewJniFilter(env, std::move(*filterPtr)) : 0;
  }
  CATCH_THROW_AND_RETURN(env, 0)
}