Page Speed Optimization Libraries  1.5.27.2
net/instaweb/rewriter/public/critical_css_filter.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 
00029 
00030 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_FILTER_H_
00031 #define NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_FILTER_H_
00032 
00033 #include <map>
00034 #include <vector>
00035 
00036 #include "net/instaweb/htmlparse/public/empty_html_filter.h"
00037 #include "net/instaweb/rewriter/public/css_tag_scanner.h"
00038 #include "net/instaweb/util/public/basictypes.h"
00039 #include "net/instaweb/util/public/string.h"
00040 
00041 namespace net_instaweb {
00042 
00043 class CriticalCssFinder;
00044 class CriticalCssResult;
00045 class CriticalCssResult_LinkRules;
00046 class HtmlCharactersNode;
00047 class HtmlElement;
00048 class RewriteDriver;
00049 
00050 class CriticalCssFilter : public EmptyHtmlFilter {
00051  public:
00052   explicit CriticalCssFilter(RewriteDriver* rewrite_driver,
00053                              CriticalCssFinder* finder);
00054   virtual ~CriticalCssFilter();
00055 
00056   static const char kAddStylesScript[];
00057   static const char kStatsScriptTemplate[];
00058   static const char kMoveAndApplyLinkScriptTemplate[];
00059   static const char kMoveAndApplyLinkTagTemplate[];
00060   static const char kNoscriptStylesId[];
00061   static const char kMoveScriptId[];
00062 
00064   virtual void DetermineEnabled();
00065   virtual void StartDocument();
00066   virtual void EndDocument();
00067   virtual void StartElement(HtmlElement* element);
00068   virtual void EndElement(HtmlElement* element);
00069   virtual void Characters(HtmlCharactersNode* characters);
00070 
00071   virtual const char* Name() const { return "CriticalCss"; }
00072 
00073  private:
00075   GoogleString DecodeUrl(const GoogleString& url);
00076 
00080   const CriticalCssResult_LinkRules* GetLinkRules(
00081       const GoogleString& decoded_url);
00082 
00085   void LogRewrite(int status);
00086 
00087   RewriteDriver* driver_;
00088   CssTagScanner css_tag_scanner_;
00089   CriticalCssFinder* finder_;
00090 
00091   CriticalCssResult* critical_css_result_;
00092   HtmlElement* noscript_element_;
00093 
00095   typedef std::map<GoogleString, int> UrlIndexes;
00096   UrlIndexes url_indexes_;
00097 
00098   bool has_critical_css_;
00099   bool is_move_link_script_added_;
00100 
00101   class CssElement;
00102   class CssStyleElement;
00103   typedef std::vector<CssElement*> CssElementVector;
00104   CssElementVector css_elements_;
00105   CssStyleElement* current_style_element_;
00106 
00111   int total_critical_size_;
00112   int total_original_size_;
00113   int repeated_style_blocks_size_;
00114   int num_repeated_style_blocks_;
00115   int num_links_;
00116   int num_replaced_links_;
00117 
00118   DISALLOW_COPY_AND_ASSIGN(CriticalCssFilter);
00119 };
00120 
00121 }  
00122 
00123 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_CSS_FILTER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines