Page Speed Optimization Libraries
1.7.30.4
|
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 00024 00025 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_SELECTOR_FILTER_H_ 00026 #define NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_SELECTOR_FILTER_H_ 00027 00028 #include <vector> 00029 00030 #include "net/instaweb/http/public/semantic_type.h" 00031 #include "net/instaweb/rewriter/public/css_summarizer_base.h" 00032 #include "net/instaweb/rewriter/public/rewrite_driver.h" 00033 #include "net/instaweb/rewriter/public/rewrite_options.h" 00034 #include "net/instaweb/util/public/basictypes.h" 00035 #include "net/instaweb/util/public/string.h" 00036 #include "net/instaweb/util/public/string_util.h" 00037 00038 namespace Css { 00039 00040 class Stylesheet; 00041 00042 } 00043 00044 namespace net_instaweb { 00045 00046 class HtmlCharactersNode; 00047 class HtmlElement; 00048 00049 class CriticalSelectorFilter : public CssSummarizerBase { 00050 public: 00051 static const char kAddStylesFunction[]; 00052 static const char kAddStylesInvocation[]; 00053 static const char kApplyFlushEarlyCss[]; 00054 static const char kInvokeFlushEarlyCssTemplate[]; 00055 static const char kMoveScriptId[]; 00056 static const char kNoscriptStylesClass[]; 00057 00058 explicit CriticalSelectorFilter(RewriteDriver* rewrite_driver); 00059 virtual ~CriticalSelectorFilter(); 00060 00061 virtual const char* Name() const { return "CriticalSelectorFilter"; } 00062 virtual const char* id() const { return "cl"; } 00063 00069 virtual RewriteDriver::InlineAuthorizationPolicy AllowUnauthorizedDomain() 00070 const { 00071 return driver_->options()->HasInlineUnauthorizedResourceType( 00072 semantic_type::kStylesheet) ? 00073 RewriteDriver::kInlineUnauthorizedResources : 00074 RewriteDriver::kInlineOnlyAuthorizedResources; 00075 } 00076 00078 virtual bool IntendedForInlining() const { return true; } 00079 00080 protected: 00086 virtual void Summarize(Css::Stylesheet* stylesheet, 00087 GoogleString* out) const; 00088 virtual void RenderSummary(int pos, 00089 HtmlElement* element, 00090 HtmlCharactersNode* char_node, 00091 bool* is_element_deleted); 00092 virtual void WillNotRenderSummary(int pos, 00093 HtmlElement* element, 00094 HtmlCharactersNode* char_node, 00095 bool* is_element_deleted); 00096 00099 virtual GoogleString CacheKeySuffix() const; 00100 00102 virtual void StartDocumentImpl(); 00103 virtual void EndDocument(); 00104 virtual void RenderDone(); 00105 00107 virtual void DetermineEnabled(); 00108 00109 private: 00110 class CssElement; 00111 class CssStyleElement; 00112 typedef std::vector<CssElement*> CssElementVector; 00113 00114 void RememberFullCss(int pos, 00115 HtmlElement* element, 00116 HtmlCharactersNode* char_node); 00117 00118 bool IsCssFlushedEarly(const GoogleString& url) const; 00119 void ApplyCssFlushedEarly(HtmlElement* element, 00120 const GoogleString& style_id, 00121 const char* media); 00122 00126 StringSet critical_selectors_; 00127 00129 GoogleString cache_key_suffix_; 00130 00134 CssElementVector css_elements_; 00135 00137 bool saw_end_document_; 00138 00140 bool any_rendered_; 00141 00143 bool is_flush_script_added_; 00144 00145 DISALLOW_COPY_AND_ASSIGN(CriticalSelectorFilter); 00146 }; 00147 00148 } 00149 00150 #endif ///< NET_INSTAWEB_REWRITER_PUBLIC_CRITICAL_SELECTOR_FILTER_H_