Page Speed Optimization Libraries  1.7.30.2
net/instaweb/rewriter/public/association_transformer.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2012 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
00021 
00022 #include <map>
00023 
00024 #include "net/instaweb/rewriter/public/css_tag_scanner.h"
00025 #include "net/instaweb/rewriter/public/resource.h"
00026 #include "net/instaweb/rewriter/public/resource_slot.h"
00027 #include "net/instaweb/util/public/basictypes.h"
00028 #include "net/instaweb/util/public/gtest_prod.h"
00029 #include "net/instaweb/util/public/string.h"
00030 #include "net/instaweb/util/public/string_util.h"
00031 
00032 namespace net_instaweb {
00033 
00034 class GoogleUrl;
00035 class MessageHandler;
00036 class RewriteOptions;
00037 
00042 class AssociationTransformer : public CssTagScanner::Transformer {
00043  public:
00052   AssociationTransformer(const GoogleUrl* base_url,
00053                          const RewriteOptions* options,
00054                          CssTagScanner::Transformer* backup_transformer,
00055                          MessageHandler* handler)
00056       : base_url_(base_url), options_(options),
00057         backup_transformer_(backup_transformer), handler_(handler) {}
00058   virtual ~AssociationTransformer();
00059 
00063   StringStringMap* map() { return &map_; }
00064 
00069   virtual TransformStatus Transform(GoogleString* str);
00070 
00071  private:
00073   StringStringMap map_;
00074 
00076   const GoogleUrl* base_url_;
00077   const RewriteOptions* options_;
00078 
00081   CssTagScanner::Transformer* backup_transformer_;
00082 
00083   MessageHandler* handler_;
00084 
00085   FRIEND_TEST(AssociationTransformerTest, TransformsCorrectly);
00086 
00087   DISALLOW_COPY_AND_ASSIGN(AssociationTransformer);
00088 };
00089 
00095 class AssociationSlot : public ResourceSlot {
00096  public:
00098   AssociationSlot(ResourcePtr resource,
00099                   StringStringMap* map, const StringPiece& key)
00100       : ResourceSlot(resource), map_(map) {
00101     key.CopyToString(&key_);
00102   }
00103   virtual ~AssociationSlot();
00104 
00107   virtual void Render() {
00109     if (!resource()->is_authorized_domain()) {
00110       return;
00111     }
00112     if (!disable_rendering()) {
00113       (*map_)[key_] = resource()->url();
00114     }
00115   }
00116 
00117   virtual bool DirectSetUrl(const StringPiece& url) {
00119     if (!resource()->is_authorized_domain()) {
00120       return false;
00121     }
00122     url.CopyToString(&((*map_)[key_]));
00123     return true;
00124   }
00125 
00126   virtual GoogleString LocationString() {
00129     return "Inside CSS";
00130   }
00131 
00132  private:
00133   StringStringMap* map_;
00134   GoogleString key_;
00135 
00136   DISALLOW_COPY_AND_ASSIGN(AssociationSlot);
00137 };
00138 
00139 }  
00140 
00141 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines