Page Speed Optimization Libraries
1.2.24.1
|
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 00033 namespace net_instaweb { 00034 00035 class HtmlParse; 00036 class HtmlResourceSlot; 00037 class ResourceSlot; 00038 class RewriteContext; 00039 00040 typedef RefCountedPtr<ResourceSlot> ResourceSlotPtr; 00041 typedef RefCountedPtr<HtmlResourceSlot> HtmlResourceSlotPtr; 00042 typedef std::vector<ResourceSlotPtr> ResourceSlotVector; 00043 00050 class ResourceSlot : public RefCounted<ResourceSlot> { 00051 public: 00052 explicit ResourceSlot(const ResourcePtr& resource) 00053 : resource_(resource), 00054 disable_rendering_(false), 00055 should_delete_element_(false), 00056 disable_further_processing_(false), 00057 was_optimized_(false) { 00058 } 00059 00060 ResourcePtr resource() const { return resource_; } 00061 00075 void SetResource(const ResourcePtr& resource); 00076 00080 void set_disable_rendering(bool x) { disable_rendering_ = x; } 00081 bool disable_rendering() const { return disable_rendering_; } 00082 00089 void RequestDeleteElement() { 00090 should_delete_element_ = true; 00091 disable_further_processing_ = true; 00092 } 00093 bool should_delete_element() const { return should_delete_element_; } 00094 00101 bool was_optimized() const { return was_optimized_; } 00102 00104 void set_was_optimized(bool x) { was_optimized_ = x; } 00105 00113 void set_disable_further_processing(bool x) { 00114 disable_further_processing_ = x; 00115 } 00116 00117 bool disable_further_processing() const { 00118 return disable_further_processing_; 00119 } 00120 00126 virtual void Render() = 0; 00127 00131 virtual void Finished() {} 00132 00140 virtual void DirectSetUrl(const StringPiece& url); 00141 00144 virtual bool CanDirectSetUrl() { return false; } 00145 00148 RewriteContext* LastContext() const; 00149 00151 void AddContext(RewriteContext* context) { contexts_.push_back(context); } 00152 00155 void DetachContext(RewriteContext* context); 00156 00159 virtual GoogleString LocationString() = 0; 00160 00161 protected: 00162 virtual ~ResourceSlot(); 00163 REFCOUNT_FRIEND_DECLARATION(ResourceSlot); 00164 00165 private: 00166 ResourcePtr resource_; 00167 bool disable_rendering_; 00168 bool should_delete_element_; 00169 bool disable_further_processing_; 00170 bool was_optimized_; 00171 00174 VectorDeque<RewriteContext*> contexts_; 00175 00176 DISALLOW_COPY_AND_ASSIGN(ResourceSlot); 00177 }; 00178 00181 class FetchResourceSlot : public ResourceSlot { 00182 public: 00183 explicit FetchResourceSlot(const ResourcePtr& resource) 00184 : ResourceSlot(resource) { 00185 } 00186 00187 virtual void Render(); 00188 virtual GoogleString LocationString(); 00189 00190 protected: 00191 REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot); 00192 virtual ~FetchResourceSlot(); 00193 00194 private: 00195 DISALLOW_COPY_AND_ASSIGN(FetchResourceSlot); 00196 }; 00197 00198 class HtmlResourceSlot : public ResourceSlot { 00199 public: 00200 HtmlResourceSlot(const ResourcePtr& resource, 00201 HtmlElement* element, 00202 HtmlElement::Attribute* attribute, 00203 HtmlParse* html_parse) 00204 : ResourceSlot(resource), 00205 element_(element), 00206 attribute_(attribute), 00207 html_parse_(html_parse), 00208 begin_line_number_(element->begin_line_number()), 00209 end_line_number_(element->end_line_number()) { 00210 } 00211 00212 HtmlElement* element() { return element_; } 00213 HtmlElement::Attribute* attribute() { return attribute_; } 00214 00215 virtual void Render(); 00216 virtual GoogleString LocationString(); 00217 virtual void DirectSetUrl(const StringPiece& url); 00218 virtual bool CanDirectSetUrl() { return true; } 00219 00220 protected: 00221 REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot); 00222 virtual ~HtmlResourceSlot(); 00223 00224 private: 00225 HtmlElement* element_; 00226 HtmlElement::Attribute* attribute_; 00227 HtmlParse* html_parse_; 00228 00229 int begin_line_number_; 00230 int end_line_number_; 00231 00232 DISALLOW_COPY_AND_ASSIGN(HtmlResourceSlot); 00233 }; 00234 00235 class HtmlResourceSlotComparator { 00236 public: 00237 bool operator()(const HtmlResourceSlotPtr& p, 00238 const HtmlResourceSlotPtr& q) const; 00239 }; 00240 00241 typedef std::set<HtmlResourceSlotPtr, 00242 HtmlResourceSlotComparator> HtmlResourceSlotSet; 00243 00244 } 00245 00246 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_