Page Speed Optimization Libraries  1.7.30.1
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 
00143   virtual void DirectSetUrl(const StringPiece& url);
00144 
00147   virtual bool CanDirectSetUrl() { return false; }
00148 
00151   RewriteContext* LastContext() const;
00152 
00154   void AddContext(RewriteContext* context) { contexts_.push_back(context); }
00155 
00158   void DetachContext(RewriteContext* context);
00159 
00162   virtual GoogleString LocationString() = 0;
00163 
00167   static GoogleString RelativizeOrPassthrough(const RewriteOptions* options,
00168                                               StringPiece url,
00169                                               UrlRelativity url_relativity,
00170                                               const GoogleUrl& base_url);
00171 
00172  protected:
00173   virtual ~ResourceSlot();
00174   REFCOUNT_FRIEND_DECLARATION(ResourceSlot);
00175 
00176  private:
00177   ResourcePtr resource_;
00178   bool disable_rendering_;
00179   bool should_delete_element_;
00180   bool disable_further_processing_;
00181   bool was_optimized_;
00182 
00185   VectorDeque<RewriteContext*> contexts_;
00186 
00187   DISALLOW_COPY_AND_ASSIGN(ResourceSlot);
00188 };
00189 
00192 class FetchResourceSlot : public ResourceSlot {
00193  public:
00194   explicit FetchResourceSlot(const ResourcePtr& resource)
00195       : ResourceSlot(resource) {
00196   }
00197 
00198   virtual void Render();
00199   virtual GoogleString LocationString();
00200 
00201  protected:
00202   REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot);
00203   virtual ~FetchResourceSlot();
00204 
00205  private:
00206   DISALLOW_COPY_AND_ASSIGN(FetchResourceSlot);
00207 };
00208 
00209 class HtmlResourceSlot : public ResourceSlot {
00210  public:
00211   HtmlResourceSlot(const ResourcePtr& resource,
00212                    HtmlElement* element,
00213                    HtmlElement::Attribute* attribute,
00214                    RewriteDriver* driver);
00215 
00216   HtmlElement* element() { return element_; }
00217   HtmlElement::Attribute* attribute() { return attribute_; }
00218 
00219   virtual void Render();
00220   virtual GoogleString LocationString();
00221   virtual void DirectSetUrl(const StringPiece& url);
00222   virtual bool CanDirectSetUrl() { return true; }
00223 
00226   UrlRelativity url_relativity() const { return url_relativity_; }
00227 
00228  protected:
00229   REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot);
00230   virtual ~HtmlResourceSlot();
00231 
00232  private:
00233   HtmlElement* element_;
00234   HtmlElement::Attribute* attribute_;
00235   RewriteDriver* driver_;
00236   UrlRelativity url_relativity_;
00237 
00238   int begin_line_number_;
00239   int end_line_number_;
00240 
00241   DISALLOW_COPY_AND_ASSIGN(HtmlResourceSlot);
00242 };
00243 
00244 class HtmlResourceSlotComparator {
00245  public:
00246   bool operator()(const HtmlResourceSlotPtr& p,
00247                   const HtmlResourceSlotPtr& q) const;
00248 };
00249 
00250 typedef std::set<HtmlResourceSlotPtr,
00251                  HtmlResourceSlotComparator> HtmlResourceSlotSet;
00252 
00253 }  
00254 
00255 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines