Page Speed Optimization Libraries  1.9.32.1
 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 
25 #include "net/instaweb/htmlparse/public/html_element.h"
28 #include "net/instaweb/util/public/ref_counted_ptr.h"
31 #include "net/instaweb/util/public/vector_deque.h"
32 #include "pagespeed/kernel/base/ref_counted_ptr.h"
33 #include "pagespeed/kernel/http/google_url.h"
34 
35 namespace net_instaweb {
36 
37 class HtmlResourceSlot;
38 class ResourceSlot;
39 class RewriteContext;
40 class RewriteDriver;
41 class RewriteOptions;
42 
43 typedef RefCountedPtr<ResourceSlot> ResourceSlotPtr;
44 typedef RefCountedPtr<HtmlResourceSlot> HtmlResourceSlotPtr;
45 typedef std::vector<ResourceSlotPtr> ResourceSlotVector;
46 
53 class ResourceSlot : public RefCounted<ResourceSlot> {
54  public:
55  explicit ResourceSlot(const ResourcePtr& resource)
56  : resource_(resource),
57  disable_rendering_(false),
58  should_delete_element_(false),
59  disable_further_processing_(false),
60  was_optimized_(false) {
61  }
62 
63  ResourcePtr resource() const { return resource_; }
65  virtual HtmlElement* element() const = 0;
66 
80  void SetResource(const ResourcePtr& resource);
81 
85  void set_disable_rendering(bool x) { disable_rendering_ = x; }
86  bool disable_rendering() const { return disable_rendering_; }
87 
95  should_delete_element_ = true;
96  disable_further_processing_ = true;
97  }
98  bool should_delete_element() const { return should_delete_element_; }
99 
106  bool was_optimized() const { return was_optimized_; }
107 
109  void set_was_optimized(bool x) { was_optimized_ = x; }
110 
119  disable_further_processing_ = x;
120  }
121 
122  bool disable_further_processing() const {
123  return disable_further_processing_;
124  }
125 
131  virtual void Render() = 0;
132 
136  virtual void Finished() {}
137 
148  virtual bool DirectSetUrl(const StringPiece& url);
149 
152  virtual bool CanDirectSetUrl() { return false; }
153 
156  RewriteContext* LastContext() const;
157 
159  void AddContext(RewriteContext* context) { contexts_.push_back(context); }
160 
163  void DetachContext(RewriteContext* context);
164 
167  virtual GoogleString LocationString() = 0;
168 
172  static GoogleString RelativizeOrPassthrough(const RewriteOptions* options,
173  StringPiece url,
174  UrlRelativity url_relativity,
175  const GoogleUrl& base_url);
176 
177  protected:
178  virtual ~ResourceSlot();
179  REFCOUNT_FRIEND_DECLARATION(ResourceSlot);
180 
181  private:
182  ResourcePtr resource_;
183  bool disable_rendering_;
184  bool should_delete_element_;
185  bool disable_further_processing_;
186  bool was_optimized_;
187 
190  VectorDeque<RewriteContext*> contexts_;
191 
192  DISALLOW_COPY_AND_ASSIGN(ResourceSlot);
193 };
194 
198  public:
199  explicit FetchResourceSlot(const ResourcePtr& resource)
200  : ResourceSlot(resource) {
201  }
202  virtual HtmlElement* element() const { return NULL; }
203  virtual void Render();
204  virtual GoogleString LocationString();
205 
206  protected:
207  REFCOUNT_FRIEND_DECLARATION(FetchResourceSlot);
208  virtual ~FetchResourceSlot();
209 
210  private:
211  DISALLOW_COPY_AND_ASSIGN(FetchResourceSlot);
212 };
213 
215  public:
216  HtmlResourceSlot(const ResourcePtr& resource,
217  HtmlElement* element,
218  HtmlElement::Attribute* attribute,
219  RewriteDriver* driver);
220 
221  virtual HtmlElement* element() const { return element_; }
222  HtmlElement::Attribute* attribute() const { return attribute_; }
223 
224  virtual void Render();
225  virtual GoogleString LocationString();
226  virtual bool DirectSetUrl(const StringPiece& url);
227  virtual bool CanDirectSetUrl() { return true; }
228 
231  UrlRelativity url_relativity() const { return url_relativity_; }
232 
233  protected:
234  REFCOUNT_FRIEND_DECLARATION(HtmlResourceSlot);
235  virtual ~HtmlResourceSlot();
236 
237  private:
238  HtmlElement* element_;
239  HtmlElement::Attribute* attribute_;
240  RewriteDriver* driver_;
241  UrlRelativity url_relativity_;
242 
243  int begin_line_number_;
244  int end_line_number_;
245 
246  DISALLOW_COPY_AND_ASSIGN(HtmlResourceSlot);
247 };
248 
250  public:
251  bool operator()(const HtmlResourceSlotPtr& p,
252  const HtmlResourceSlotPtr& q) const;
253 };
254 
255 typedef std::set<HtmlResourceSlotPtr,
256  HtmlResourceSlotComparator> HtmlResourceSlotSet;
257 
258 }
259 
260 #endif
virtual void Finished()
Definition: resource_slot.h:136
void set_disable_rendering(bool x)
Definition: resource_slot.h:85
bool was_optimized() const
Definition: resource_slot.h:106
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:53
virtual bool DirectSetUrl(const StringPiece &url)
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:152
Definition: resource_slot.h:214
virtual GoogleString LocationString()
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:221
void RequestDeleteElement()
Definition: resource_slot.h:94
virtual HtmlElement * element() const
Return HTML element associated with slot, or NULL if none (CSS, IPRO)
Definition: resource_slot.h:202
void set_disable_further_processing(bool x)
Definition: resource_slot.h:118
UrlRelativity url_relativity() const
Definition: resource_slot.h:231
Definition: resource_slot.h:197
Definition: rewrite_driver.h:98
RewriteContext * LastContext() const
void AddContext(RewriteContext *context)
Adds a new context to this slot.
Definition: resource_slot.h:159
virtual bool DirectSetUrl(const StringPiece &url)
Definition: rewrite_context.h:144
virtual bool CanDirectSetUrl()
Definition: resource_slot.h:227
Definition: resource_slot.h:249
virtual GoogleString LocationString()=0
Definition: rewrite_options.h:81
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:109