Page Speed Optimization Libraries  1.8.31.3
net/instaweb/rewriter/public/resource_slot.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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_RESOURCE_SLOT_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
00021 
00022 #include <set>
00023 #include <vector>
00024 
00025 #include "net/instaweb/htmlparse/public/html_element.h"
00026 #include "net/instaweb/rewriter/public/resource.h"
00027 #include "net/instaweb/util/public/basictypes.h"
00028 #include "net/instaweb/util/public/ref_counted_ptr.h"
00029 #include "net/instaweb/util/public/string.h"
00030 #include "net/instaweb/util/public/string_util.h"
00031 #include "net/instaweb/util/public/vector_deque.h"
00032 #include "pagespeed/kernel/base/ref_counted_ptr.h"
00033 #include "pagespeed/kernel/http/google_url.h"
00034 
00035 namespace net_instaweb {
00036 
00037 class HtmlResourceSlot;
00038 class ResourceSlot;
00039 class RewriteContext;
00040 class RewriteDriver;
00041 class RewriteOptions;
00042 
00043 typedef RefCountedPtr<ResourceSlot> ResourceSlotPtr;
00044 typedef RefCountedPtr<HtmlResourceSlot> HtmlResourceSlotPtr;
00045 typedef std::vector<ResourceSlotPtr> ResourceSlotVector;
00046 
00053 class ResourceSlot : public RefCounted<ResourceSlot> {
00054  public:
00055   explicit ResourceSlot(const ResourcePtr& resource)
00056       : resource_(resource),
00057         disable_rendering_(false),
00058         should_delete_element_(false),
00059         disable_further_processing_(false),
00060         was_optimized_(false) {
00061   }
00062 
00063   ResourcePtr resource() const { return resource_; }
00064 
00078   void SetResource(const ResourcePtr& resource);
00079 
00083   void set_disable_rendering(bool x) { disable_rendering_ = x; }
00084   bool disable_rendering() const { return disable_rendering_; }
00085 
00092   void RequestDeleteElement() {
00093     should_delete_element_ = true;
00094     disable_further_processing_ = true;
00095   }
00096   bool should_delete_element() const { return should_delete_element_; }
00097 
00104   bool was_optimized() const { return was_optimized_; }
00105 
00107   void set_was_optimized(bool x) { was_optimized_ = x; }
00108 
00116   void set_disable_further_processing(bool x) {
00117     disable_further_processing_ = x;
00118   }
00119 
00120   bool disable_further_processing() const {
00121     return disable_further_processing_;
00122   }
00123 
00129   virtual void Render() = 0;
00130 
00134   virtual void Finished() {}
00135 
00146   virtual bool DirectSetUrl(const StringPiece& url);
00147 
00150   virtual bool CanDirectSetUrl() { return false; }
00151 
00154   RewriteContext* LastContext() const;
00155 
00157   void AddContext(RewriteContext* context) { contexts_.push_back(context); }
00158 
00161   void DetachContext(RewriteContext* context);
00162 
00165   virtual GoogleString LocationString() = 0;
00166 
00170   static GoogleString RelativizeOrPassthrough(const RewriteOptions* options,
00171                                               StringPiece url,
00172                                               UrlRelativity url_relativity,
00173                                               const GoogleUrl& base_url);
00174 
00175  protected:
00176   virtual ~ResourceSlot();
00177   REFCOUNT_FRIEND_DECLARATION(ResourceSlot);
00178 
00179  private:
00180   ResourcePtr resource_;
00181   bool disable_rendering_;
00182   bool should_delete_element_;
00183   bool disable_further_processing_;
00184   bool was_optimized_;
00185 
00188   VectorDeque<RewriteContext*> contexts_;
00189 
00190   DISALLOW_COPY_AND_ASSIGN(ResourceSlot);
00191 };
00192 
00195 class FetchResourceSlot : public ResourceSlot {
00196  public:
00197   explicit FetchResourceSlot(const ResourcePtr& resource)
00198       : ResourceSlot(resource) {
00199   }
00200 
00201   virtual void Render();
00202   virtual GoogleString LocationString();
00203 
00204  protected:
00205   REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot);
00206   virtual ~FetchResourceSlot();
00207 
00208  private:
00209   DISALLOW_COPY_AND_ASSIGN(FetchResourceSlot);
00210 };
00211 
00212 class HtmlResourceSlot : public ResourceSlot {
00213  public:
00214   HtmlResourceSlot(const ResourcePtr& resource,
00215                    HtmlElement* element,
00216                    HtmlElement::Attribute* attribute,
00217                    RewriteDriver* driver);
00218 
00219   HtmlElement* element() { return element_; }
00220   HtmlElement::Attribute* attribute() { return attribute_; }
00221 
00222   virtual void Render();
00223   virtual GoogleString LocationString();
00224   virtual bool DirectSetUrl(const StringPiece& url);
00225   virtual bool CanDirectSetUrl() { return true; }
00226 
00229   UrlRelativity url_relativity() const { return url_relativity_; }
00230 
00231  protected:
00232   REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot);
00233   virtual ~HtmlResourceSlot();
00234 
00235  private:
00236   HtmlElement* element_;
00237   HtmlElement::Attribute* attribute_;
00238   RewriteDriver* driver_;
00239   UrlRelativity url_relativity_;
00240 
00241   int begin_line_number_;
00242   int end_line_number_;
00243 
00244   DISALLOW_COPY_AND_ASSIGN(HtmlResourceSlot);
00245 };
00246 
00247 class HtmlResourceSlotComparator {
00248  public:
00249   bool operator()(const HtmlResourceSlotPtr& p,
00250                   const HtmlResourceSlotPtr& q) const;
00251 };
00252 
00253 typedef std::set<HtmlResourceSlotPtr,
00254                  HtmlResourceSlotComparator> HtmlResourceSlotSet;
00255 
00256 }  
00257 
00258 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines