Page Speed Optimization Libraries  1.4.26.1
net/instaweb/rewriter/public/resource_combiner.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 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 
00022 
00023 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_COMBINER_H_
00024 #define NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_COMBINER_H_
00025 
00026 #include "net/instaweb/rewriter/public/resource.h"
00027 #include "net/instaweb/rewriter/public/server_context.h"
00028 #include "net/instaweb/rewriter/public/url_partnership.h"
00029 #include "net/instaweb/util/public/basictypes.h"
00030 #include "net/instaweb/util/public/gtest_prod.h"
00031 #include "net/instaweb/util/public/string.h"
00032 #include "net/instaweb/util/public/string_util.h"
00033 
00034 namespace net_instaweb {
00035 
00036 struct ContentType;
00037 class MessageHandler;
00038 class OutputResource;
00039 class RewriteDriver;
00040 class RewriteFilter;
00041 class Writer;
00042 
00044 struct TimedBool {
00047   int64 expiration_ms;
00048   bool value;
00049 };
00050 
00057 class ResourceCombiner {
00058  public:
00067   static const int kUrlSlack = 100;
00068 
00070   ResourceCombiner(RewriteDriver* rewrite_driver,
00071                    const StringPiece& extension,
00072                    RewriteFilter* filter);
00073 
00074   virtual ~ResourceCombiner();
00075 
00079   void Reset();
00080 
00083   GoogleString UrlSafeId() const;
00084 
00086   int num_urls() const { return partnership_.num_urls(); }
00087 
00088   const ResourceVector& resources() const { return resources_; }
00089 
00091   GoogleString ResolvedBase() const { return partnership_.ResolvedBase(); }
00092 
00094   TimedBool AddResourceNoFetch(const ResourcePtr& resource,
00095                                MessageHandler* handler);
00096 
00097  protected:
00101   virtual void RemoveLastResource();
00102 
00107   OutputResourcePtr Combine(MessageHandler* handler);
00108 
00111   virtual bool WriteCombination(const ResourceVector& combine_resources,
00112                                 const OutputResourcePtr& combination,
00113                                 MessageHandler* handler);
00114 
00119   virtual bool WritePiece(int index, const Resource* input,
00120                           OutputResource* combination, Writer* writer,
00121                           MessageHandler* handler);
00122 
00125   virtual void Clear();
00126 
00127   ServerContext* const server_context_;
00128   RewriteDriver* const rewrite_driver_;
00129 
00130  private:
00131   friend class AggregateCombiner;
00132 
00134   virtual const ContentType* CombinationContentType() = 0;
00135 
00137   void UpdateResolvedBase();
00138 
00140   void ComputeLeafSize();
00141 
00144   void AccumulateLeafSize(const StringPiece& url);
00145 
00148   bool UrlTooBig();
00149 
00152   virtual void AccumulateCombinedSize(const ResourcePtr& resource) {}
00153 
00156   virtual bool ContentSizeTooBig() const { return false; }
00157 
00162   virtual bool ResourceCombinable(Resource* resource,
00163                                   MessageHandler* handler);
00164 
00165   UrlPartnership partnership_;
00166   ResourceVector resources_;
00167   StringVector multipart_encoder_urls_;
00168   int prev_num_components_;
00169   int accumulated_leaf_size_;
00170   GoogleString resolved_base_;
00171   const int url_overhead_;
00172   RewriteFilter* filter_;
00173 
00174   FRIEND_TEST(ResourceCombinerTest, TestRemove);
00175   FRIEND_TEST(ResourceCombinerTest, TestRemoveFrom3);
00176   FRIEND_TEST(ResourceCombinerTest, TestRebaseRemove);
00177   FRIEND_TEST(ResourceCombinerTest, TestRebaseRemoveAdd);
00178 
00179   DISALLOW_COPY_AND_ASSIGN(ResourceCombiner);
00180 };
00181 
00182 }  
00183 
00184 #endif  ///< NET_INSTAWEB_REWRITER_PUBLIC_RESOURCE_COMBINER_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines