Page Speed Optimization Libraries  1.8.31.2
net/instaweb/rewriter/public/css_hierarchy.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2011 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_HIERARCHY_H_
00020 #define NET_INSTAWEB_REWRITER_PUBLIC_CSS_HIERARCHY_H_
00021 
00022 #include <vector>
00023 
00024 #include "net/instaweb/rewriter/public/resource.h"
00025 #include "net/instaweb/util/public/basictypes.h"
00026 #include "net/instaweb/util/public/google_url.h"
00027 #include "net/instaweb/util/public/scoped_ptr.h"
00028 #include "net/instaweb/util/public/string.h"
00029 #include "net/instaweb/util/public/string_util.h"
00030 #include "pagespeed/kernel/http/data_url.h"
00031 
00032 namespace Css {
00033 class Stylesheet;
00034 }  
00035 
00036 namespace net_instaweb {
00037 
00038 class CssFilter;
00039 class MessageHandler;
00040 
00064 class CssHierarchy {
00065  public:
00066   static const char kFailureReasonPrefix[];
00067 
00070   explicit CssHierarchy(CssFilter* filter);
00071   ~CssHierarchy();
00072 
00076   void InitializeRoot(const GoogleUrl& css_base_url,
00077                       const GoogleUrl& css_trim_url,
00078                       const StringPiece input_contents,
00079                       bool has_unparseables,
00080                       int64 flattened_result_limit,
00081                       Css::Stylesheet* stylesheet,
00082                       MessageHandler* message_handler);
00083 
00085   bool NeedsRewriting() const {
00086     return flattening_succeeded_ && !url_.empty();
00087   }
00088 
00089   const StringPiece url() const { return url_; }
00090   const StringPiece url_for_humans() const {
00091     return (url_.empty() ? "inline"
00092             : IsDataUrl(url_) ? "data URL"
00093             : url_);
00094   }
00095 
00096   const GoogleUrl& css_base_url() const { return css_base_url_; }
00097   const GoogleUrl& css_trim_url() const { return css_trim_url_; }
00098 
00102   const GoogleUrl& css_resolution_base() {
00103     return (input_contents_resolved_ ? css_trim_url_ : css_base_url_);
00104   }
00105 
00106   const Css::Stylesheet* stylesheet() const { return stylesheet_.get(); }
00107   Css::Stylesheet* mutable_stylesheet() { return stylesheet_.get(); }
00108   void set_stylesheet(Css::Stylesheet* stylesheet);
00109 
00110   const StringPiece input_contents() const { return input_contents_; }
00113   void set_input_contents(const StringPiece input_contents) {
00114     input_contents_ = input_contents;
00115   }
00116 
00117   GoogleString* input_contents_backing_store() {
00118     return &input_contents_backing_store_;
00119   }
00120   void set_input_contents_to_backing_store() {
00121     input_contents_ = input_contents_backing_store_;
00122   }
00123 
00124   const GoogleString& minified_contents() const { return minified_contents_; }
00125   void set_minified_contents(const StringPiece minified_contents);
00126 
00127   const GoogleString& charset() const { return charset_; }
00128   GoogleString* mutable_charset() { return &charset_; }
00129 
00130   const GoogleString& charset_source() const { return charset_source_; }
00131 
00132   const StringVector& media() const { return media_; }
00133   StringVector* mutable_media() { return &media_; }
00134 
00136   const std::vector<CssHierarchy*>& children() const { return children_; }
00137   std::vector<CssHierarchy*>& children() { return children_; }
00138 
00139   bool input_contents_resolved() const { return input_contents_resolved_; }
00140   void set_input_contents_resolved(bool x) { input_contents_resolved_ = x; }
00141 
00142   bool flattening_succeeded() const { return flattening_succeeded_; }
00143   void set_flattening_succeeded(bool ok) { flattening_succeeded_ = ok; }
00144 
00145   const GoogleString& flattening_failure_reason() const {
00146     return flattening_failure_reason_;
00147   }
00153   void AddFlatteningFailureReason(const GoogleString& reason);
00154 
00155   bool unparseable_detected() const { return unparseable_detected_; }
00156   void set_unparseable_detected(bool ok) { unparseable_detected_ = ok; }
00157 
00158   int64 flattened_result_limit() const { return flattened_result_limit_; }
00159   void set_flattened_result_limit(int64 x) { flattened_result_limit_ = x; }
00160 
00173   bool CheckCharsetOk(const ResourcePtr& resource,
00174                       GoogleString* failure_reason);
00175 
00181   bool Parse();
00182 
00195   bool ExpandChildren();
00196 
00210   void RollUpContents();
00211 
00226   bool RollUpStylesheets();
00227 
00228  private:
00229   friend class CssHierarchyTest;
00230 
00234   void InitializeNested(const CssHierarchy& parent,
00235                         const GoogleUrl& import_url);
00236 
00238   void ResizeChildren(int n);
00239 
00243   bool IsRecursive() const;
00244 
00250   bool DetermineImportMedia(const StringVector& containing_media,
00251                             const StringVector& import_media);
00252 
00259   bool DetermineRulesetMedia(StringVector* ruleset_media);
00260 
00262   CssFilter* filter_;
00263 
00266   StringPiece url_;
00267 
00269   GoogleUrl css_base_url_;
00270 
00273   GoogleUrl css_trim_url_;
00274 
00277   const CssHierarchy* parent_;
00278 
00282   std::vector<CssHierarchy*> children_;
00283 
00285   StringPiece input_contents_;
00286 
00292   GoogleString input_contents_backing_store_;
00293 
00295   GoogleString minified_contents_;
00296 
00300   scoped_ptr<Css::Stylesheet> stylesheet_;
00301 
00304   GoogleString charset_;
00305 
00307   GoogleString charset_source_;
00308 
00314   StringVector media_;
00315 
00320   bool input_contents_resolved_;
00321 
00325   bool flattening_succeeded_;
00326 
00329   GoogleString flattening_failure_reason_;
00330 
00332   bool unparseable_detected_;
00333 
00342   int64 flattened_result_limit_;
00343 
00345   MessageHandler* message_handler_;
00346 
00347   DISALLOW_COPY_AND_ASSIGN(CssHierarchy);
00348 };
00349 
00350 }  
00351 
00352 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_CSS_HIERARCHY_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines