"Fossies" - the Fresh Open Source Software Archive

Member "incubator-pagespeed-mod-1.14.36.1/pagespeed/opt/ads/ads_util.cc" (28 Feb 2020, 3474 Bytes) of package /linux/www/apache_httpd_modules/incubator-pagespeed-mod-1.14.36.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ads_util.cc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.13.35.2_vs_1.14.36.1.

    1 /*
    2  * Licensed to the Apache Software Foundation (ASF) under one
    3  * or more contributor license agreements.  See the NOTICE file
    4  * distributed with this work for additional information
    5  * regarding copyright ownership.  The ASF licenses this file
    6  * to you under the Apache License, Version 2.0 (the
    7  * "License"); you may not use this file except in compliance
    8  * with the License.  You may obtain a copy of the License at
    9  * 
   10  *   http://www.apache.org/licenses/LICENSE-2.0
   11  * 
   12  * Unless required by applicable law or agreed to in writing,
   13  * software distributed under the License is distributed on an
   14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15  * KIND, either express or implied.  See the License for the
   16  * specific language governing permissions and limitations
   17  * under the License.
   18  */
   19 
   20 #include "pagespeed/opt/ads/ads_util.h"
   21 
   22 #include <cstddef>
   23 
   24 #include "pagespeed/opt/ads/ads_attribute.h"
   25 #include "pagespeed/kernel/base/string_util.h"
   26 #include "pagespeed/kernel/html/html_element.h"
   27 #include "pagespeed/kernel/html/html_name.h"
   28 
   29 // Separator used in publisher code.
   30 namespace { const char kAdsPublisherCodeSeparator[] = "-"; }
   31 
   32 namespace net_instaweb {
   33 
   34 namespace ads_util {
   35 
   36 const char kAdsByGoogleJavascriptSrc[] =
   37     "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
   38 const char kAdsByGoogleApiCallJavascript[] =
   39     "(adsbygoogle = window.adsbygoogle || []).push({})";
   40 const char kAdsbyGoogleClass[] = "adsbygoogle";
   41 
   42 const char kShowAdsApiCallJSSrc[] =
   43     "//pagead2.googlesyndication.com/pagead/show_ads.js";
   44 
   45 // TODO(chenyu): add unittests for methods defined in this file.
   46 StringPiece GetPublisherIdWithoutProductPrefix(StringPiece publisher_code) {
   47   // TODO(chenyu): Modify this search to support Direct publishers
   48   // ("partner-aol-in" is a valid Direct AFS property code).
   49   stringpiece_ssize_type last_separator_pos = publisher_code.find_last_of(
   50       kAdsPublisherCodeSeparator);
   51   if (last_separator_pos != StringPiece::npos) {
   52     publisher_code.remove_prefix(last_separator_pos + 1);
   53   }
   54   return publisher_code;
   55 }
   56 
   57 bool IsValidAdsByGoogle(
   58     const HtmlElement& element, StringPiece publisher_id) {
   59   // An adsbygoogle element must be an <Ins> element.
   60   if (element.keyword() != HtmlName::kIns) {
   61     return false;
   62   }
   63 
   64   // An adsbygoogle element must have class name 'kAdsbyGoogleClass'.
   65   if (StringPiece(element.AttributeValue(HtmlName::kClass)) !=
   66       kAdsbyGoogleClass) {
   67     return false;
   68   }
   69 
   70   // An adsbygoogle element for publisher with 'publisher_id' must have
   71   // 'kDataAdClient' attribute containing 'publisher_id'.
   72   const HtmlElement::Attribute* ad_client_attribute =
   73       element.FindAttribute(ads_attribute::kDataAdClient);
   74   if (ad_client_attribute == NULL ||
   75       FindIgnoreCase(
   76           ad_client_attribute->DecodedValueOrNull(),
   77           publisher_id) == StringPiece::npos) {
   78     return false;
   79   }
   80 
   81   // An adsbygoogle element must have 'kDataAdSlot' attribute.
   82   const HtmlElement::Attribute* ad_slot_attribute =
   83       element.FindAttribute(ads_attribute::kDataAdSlot);
   84   if (ad_slot_attribute == NULL ||
   85       ad_slot_attribute->DecodedValueOrNull() == NULL) {
   86     return false;
   87   }
   88 
   89   return true;
   90 }
   91 
   92 bool IsShowAdsApiCallJsSrc(StringPiece src) {
   93   return src.find(kShowAdsApiCallJSSrc) != StringPiece::npos;
   94 }
   95 
   96 bool IsAdsByGoogleJsSrc(StringPiece src) {
   97   return src.find(kAdsByGoogleJavascriptSrc) != StringPiece::npos;
   98 }
   99 
  100 }  // namespace ads_util
  101 }  // namespace net_instaweb