Page Speed Optimization Libraries  1.9.32.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
resource_slot.h
Go to the documentation of this file.
1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_SLOT_H_
21 
22 #include <set>
23 #include <vector>
24 
26 #include "pagespeed/kernel/base/basictypes.h"
27 #include "pagespeed/kernel/base/ref_counted_ptr.h"
28 #include "pagespeed/kernel/base/string.h"
29 #include "pagespeed/kernel/base/string_util.h"
30 #include "pagespeed/kernel/base/vector_deque.h"
31 #include "pagespeed/kernel/html/html_element.h"
32 #include "pagespeed/kernel/http/google_url.h"
33 
34 namespace net_instaweb {
35 
36 class HtmlResourceSlot;
37 class ResourceSlot;
38 class RewriteContext;
39 class RewriteDriver;
40 class RewriteOptions;
41 
42 typedef RefCountedPtr<ResourceSlot> ResourceSlotPtr;
43 typedef RefCountedPtr<HtmlResourceSlot> HtmlResourceSlotPtr;
44 typedef std::vector<ResourceSlotPtr> ResourceSlotVector;
45 
52 class ResourceSlot : public RefCounted<ResourceSlot> {
53  public:
54  explicit ResourceSlot(const ResourcePtr& resource)
55  : resource_(resource),
56  disable_rendering_(false),
57  should_delete_element_(false),
58  disable_further_processing_(false),
59  was_optimized_(false) {
60  }
61 
62  ResourcePtr resource() const { return resource_; }
64  virtual HtmlElement* element() const = 0;
65 
79  void SetResource(const ResourcePtr& resource);
80 
84  void set_disable_rendering(bool x) { disable_rendering_ = x; }
85  bool disable_rendering() const { return disable_rendering_; }
86 
94  should_delete_element_ = true;
95  disable_further_processing_ = true;
96  }
97  bool should_delete_element() const { return should_delete_element_; }
98 
105  bool was_optimized() const { return was_optimized_; }
106 
108  void set_was_optimized(bool x) { was_optimized_ = x; }
109 
118  disable_further_processing_ = x;
119  }
120 
121  bool disable_further_processing() const {
122  return disable_further_processing_;
123  }
124 
130  virtual void Render() = 0;
131 
135  virtual void Finished() {}
136 
147  virtual bool DirectSetUrl(const StringPiece& url);
148 
151  virtual bool CanDirectSetUrl() { return false; }
152 
155  RewriteContext* LastContext() const;
156 
158  void AddContext(RewriteContext* context) { contexts_.push_back(context); }
159 
162  void DetachContext(RewriteContext* context);
163 
166  virtual GoogleString LocationString() = 0;
167 
171  static GoogleString RelativizeOrPassthrough(const RewriteOptions* options,
172  StringPiece url,
173  UrlRelativity url_relativity,
174  const GoogleUrl& base_url);
175 
176  protected:
177  virtual ~ResourceSlot();
178  REFCOUNT_FRIEND_DECLARATION(ResourceSlot);
179 
180  private:
181  ResourcePtr resource_;
182  bool disable_rendering_;
183  bool should_delete_element_;
184  bool disable_further_processing_;
185  bool was_optimized_;
186 
189  VectorDeque<RewriteContext*> contexts_;
190 
191  DISALLOW_COPY_AND_ASSIGN(ResourceSlot);
192 };
193 
197  public:
198  explicit FetchResourceSlot(const ResourcePtr& resource)
199  : ResourceSlot(resource) {
200  }
201  virtual HtmlElement* element() const { return NULL; }
202  virtual void Render();
203  virtual GoogleString LocationString();
204 
205  protected:
206  REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot);
207  virtual ~FetchResourceSlot();
208 
209  private:
210  DISALLOW_COPY_AND_ASSIGN(FetchResourceSlot);
211 };
212 
214  public:
215  HtmlResourceSlot(const ResourcePtr& resource,
216  HtmlElement* element,
217  HtmlElement::Attribute* attribute,
218  RewriteDriver* driver);
219 
220  virtual HtmlElement* element() const { return element_; }
221  HtmlElement::Attribute* attribute() const { return attribute_; }
222 
223  virtual void Render();
224  virtual GoogleString LocationString();
225  virtual bool DirectSetUrl(const StringPiece& url);
226  virtual bool CanDirectSetUrl() { return true; }
227 
230  UrlRelativity url_relativity() const { return url_relativity_; }
231 
232  protected:
233  REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot);
234  virtual ~HtmlResourceSlot();
235 
236  private:
237  HtmlElement* element_;
238  HtmlElement::Attribute* attribute_;
239  RewriteDriver* driver_;
240  UrlRelativity url_relativity_;
241 
242  int begin_line_number_;
243  int end_line_number_;
244 
245  DISALLOW_COPY_AND_ASSIGN(HtmlResourceSlot);
246 };
247 
249  public:
250  bool operator()(const HtmlResourceSlotPtr& p,
251  const HtmlResourceSlotPtr& q) const;
252 };
253 
254 typedef std::set<HtmlResourceSlotPtr,
255  HtmlResourceSlotComparator> HtmlResourceSlotSet;
256 
257 }
258 
259 #endif
virtual void Finished()
Definition: resource_slot.h:135
void set_disable_rendering(bool x)
Definition: resource_slot.h:84
bool was_optimized() const
Definition: resource_slot.h:105
virtual GoogleString LocationString()
void SetResource(const ResourcePtr &resource)
virtual void Render()=0
static GoogleString RelativizeOrPassthrough(const RewriteOptions *options, StringPiece url, UrlRelativity url_relativity, const GoogleUrl &base_url)
Definition: resource_slot.h:52
virtual bool DirectSetUrl(const StringPiece &url)
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:151
Definition: resource_slot.h:213
virtual GoogleString LocationString()
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:220
void RequestDeleteElement()
Definition: resource_slot.h:93
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:201
void set_disable_further_processing(bool x)
Definition: resource_slot.h:117
UrlRelativity url_relativity() const
Definition: resource_slot.h:230
Definition: resource_slot.h:196
Definition: rewrite_driver.h:98
RewriteContext * LastContext() const
void AddContext(RewriteContext *context)
Adds a new context to this slot.
Definition: resource_slot.h:158
virtual bool DirectSetUrl(const StringPiece &url)
Definition: rewrite_context.h:144
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:226
Definition: resource_slot.h:248
virtual GoogleString LocationString()=0
Definition: rewrite_options.h:83
void DetachContext(RewriteContext *context)
virtual HtmlElement * element() const =0
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
void set_was_optimized(bool x)
Marks the slot as having been optimized.
Definition: resource_slot.h:108