Page Speed Optimization Libraries
1.6.29.3
|
00001 /* 00002 * Copyright 2013 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_CSS_SUMMARIZER_BASE_H_ 00020 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_ 00021 00022 #include <vector> 00023 00024 #include "net/instaweb/htmlparse/public/html_element.h" 00025 #include "net/instaweb/rewriter/public/resource_slot.h" 00026 #include "net/instaweb/rewriter/public/rewrite_filter.h" 00027 #include "net/instaweb/util/public/basictypes.h" 00028 #include "net/instaweb/util/public/scoped_ptr.h" 00029 #include "net/instaweb/util/public/string.h" 00030 #include "net/instaweb/util/public/string_util.h" 00031 00032 namespace Css { 00033 00034 class Stylesheet; 00035 00036 } 00037 00038 namespace net_instaweb { 00039 00040 class AbstractMutex; 00041 class HtmlCharactersNode; 00042 class RewriteContext; 00043 class RewriteDriver; 00044 00049 class CssSummarizerBase : public RewriteFilter { 00050 public: 00051 explicit CssSummarizerBase(RewriteDriver* driver); 00052 virtual ~CssSummarizerBase(); 00053 00054 protected: 00055 enum SummaryState { 00057 kSummaryOk, 00058 00060 kSummaryStillPending, 00061 00063 kSummaryCssParseError, 00064 00067 kSummaryResourceCreationFailed, 00068 00070 kSummaryInputUnavailable, 00071 00073 kSummarySlotRemoved, 00074 }; 00075 00076 struct SummaryInfo { 00077 SummaryInfo() 00078 : state(kSummaryStillPending), 00079 is_external(false), 00080 is_inside_noscript(false) {} 00081 00084 GoogleString data; 00085 00087 SummaryState state; 00088 00091 GoogleString location; 00092 00094 GoogleString base; 00095 00097 GoogleString media_from_html; 00098 00100 GoogleString rel; 00101 00103 bool is_external; 00104 00106 bool is_inside_noscript; 00107 }; 00108 00111 virtual GoogleString CacheKeySuffix() const; 00112 00117 virtual bool MustSummarize(HtmlElement* element) const { 00118 return true; 00119 } 00120 00128 virtual void Summarize(Css::Stylesheet* stylesheet, 00129 GoogleString* out) const = 0; 00130 00146 virtual void RenderSummary(int pos, 00147 HtmlElement* element, 00148 HtmlCharactersNode* char_node); 00149 00157 virtual void WillNotRenderSummary(int pos, 00158 HtmlElement* element, 00159 HtmlCharactersNode* char_node); 00160 00175 virtual void SummariesDone(); 00176 00181 int NumStyles() const { return static_cast<int>(summaries_.size()); } 00182 00188 const SummaryInfo& GetSummaryForStyle(int pos) const { 00189 return summaries_.at(pos); 00190 } 00191 00194 virtual void StartDocumentImpl(); 00195 virtual void EndDocument(); 00196 virtual void StartElementImpl(HtmlElement* element); 00197 virtual void Characters(HtmlCharactersNode* characters); 00198 virtual void EndElementImpl(HtmlElement* element); 00199 virtual void RenderDone(); 00200 00201 virtual RewriteContext* MakeRewriteContext(); 00202 00203 private: 00204 class Context; 00205 00207 void Clear(); 00208 00211 void ReportSummariesDone(); 00212 00215 void StartInlineRewrite(HtmlElement* style, HtmlCharactersNode* text); 00216 00219 void StartExternalRewrite(HtmlElement* link, HtmlElement::Attribute* src, 00220 StringPiece rel); 00221 00227 Context* CreateContextAndSummaryInfo(const HtmlElement* element, 00228 bool external, 00229 const ResourceSlotPtr& slot, 00230 const GoogleString& location, 00231 StringPiece base_for_resources, 00232 StringPiece rel); 00233 00234 ResourceSlot* MakeSlotForInlineCss(const StringPiece& content); 00235 00237 std::vector<SummaryInfo> summaries_; 00238 00239 scoped_ptr<AbstractMutex> progress_lock_; 00240 int outstanding_rewrites_; 00241 bool saw_end_of_document_; 00242 00243 00244 00245 std::vector<int> canceled_summaries_; 00246 00247 HtmlElement* style_element_; 00248 00249 DISALLOW_COPY_AND_ASSIGN(CssSummarizerBase); 00250 }; 00251 00252 } 00253 00254 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_