Page Speed Optimization Libraries  1.5.27.2
net/instaweb/rewriter/public/css_summarizer_base.h
Go to the documentation of this file.
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 HtmlNode;
00043 class RewriteContext;
00044 class RewriteDriver;
00045 
00050 class CssSummarizerBase : public RewriteFilter {
00051  public:
00052   explicit CssSummarizerBase(RewriteDriver* driver);
00053   virtual ~CssSummarizerBase();
00054 
00055  protected:
00056   enum SummaryState {
00058     kSummaryOk,
00059 
00061     kSummaryStillPending,
00062 
00064     kSummaryCssParseError,
00065 
00068     kSummaryResourceCreationFailed,
00069 
00071     kSummaryInputUnavailable,
00072 
00074     kSummarySlotRemoved,
00075   };
00076 
00077   struct SummaryInfo {
00078     SummaryInfo()
00079         : state(kSummaryStillPending),
00080           is_external(false),
00081           is_inside_noscript(false) {}
00082 
00085     GoogleString data;
00086 
00088     SummaryState state;
00089 
00092     GoogleString location;
00093 
00095     GoogleString base;
00096 
00098     GoogleString media_from_html;
00099 
00101     GoogleString rel;
00102 
00104     bool is_external;
00105 
00107     bool is_inside_noscript;
00108   };
00109 
00112   virtual GoogleString CacheKeySuffix() const;
00113 
00118   virtual bool MustSummarize(HtmlElement* element) const {
00119     return true;
00120   }
00121 
00129   virtual void Summarize(Css::Stylesheet* stylesheet,
00130                          GoogleString* out) const = 0;
00131 
00147   virtual void RenderSummary(int pos,
00148                              HtmlElement* element,
00149                              HtmlCharactersNode* char_node);
00150 
00158   virtual void WillNotRenderSummary(int pos,
00159                                     HtmlElement* element,
00160                                     HtmlCharactersNode* char_node);
00161 
00176   virtual void SummariesDone();
00177 
00184   void InjectSummaryData(HtmlNode* data);
00185 
00190   int NumStyles() const { return static_cast<int>(summaries_.size()); }
00191 
00197   const SummaryInfo& GetSummaryForStyle(int pos) const {
00198     return summaries_.at(pos);
00199   }
00200 
00203   virtual void StartDocumentImpl();
00204   virtual void EndDocument();
00205   virtual void StartElementImpl(HtmlElement* element);
00206   virtual void Characters(HtmlCharactersNode* characters);
00207   virtual void EndElementImpl(HtmlElement* element);
00208   virtual void RenderDone();
00209 
00210   virtual RewriteContext* MakeRewriteContext();
00211 
00212  private:
00213   class Context;
00214 
00216   void Clear();
00217 
00220   void ReportSummariesDone();
00221 
00224   void StartInlineRewrite(HtmlElement* style, HtmlCharactersNode* text);
00225 
00228   void StartExternalRewrite(HtmlElement* link, HtmlElement::Attribute* src,
00229                             StringPiece rel);
00230 
00236   Context* CreateContextAndSummaryInfo(const HtmlElement* element,
00237                                        bool external,
00238                                        const ResourceSlotPtr& slot,
00239                                        const GoogleString& location,
00240                                        StringPiece base_for_resources,
00241                                        StringPiece rel);
00242 
00243   ResourceSlot* MakeSlotForInlineCss(const StringPiece& content);
00244 
00246   std::vector<SummaryInfo> summaries_;
00247 
00248   scoped_ptr<AbstractMutex> progress_lock_;
00249   int outstanding_rewrites_; 
00250   bool saw_end_of_document_; 
00251 
00252 
00253 
00254   std::vector<int> canceled_summaries_; 
00255 
00256   HtmlElement* style_element_; 
00257   HtmlElement* injection_point_; 
00258 
00259   DISALLOW_COPY_AND_ASSIGN(CssSummarizerBase);
00260 };
00261 
00262 }  
00263 
00264 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_SUMMARIZER_BASE_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines