Page Speed Optimization Libraries  1.11.33.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
static_asset_manager.h
Go to the documentation of this file.
1 /*
2  * Copyright 2012 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef NET_INSTAWEB_REWRITER_PUBLIC_STATIC_ASSET_MANAGER_H_
20 #define NET_INSTAWEB_REWRITER_PUBLIC_STATIC_ASSET_MANAGER_H_
21 
22 #include <map>
23 #include <vector>
24 
25 #include "net/instaweb/rewriter/static_asset_config.pb.h"
26 #include "pagespeed/kernel/base/abstract_mutex.h"
27 #include "pagespeed/kernel/base/basictypes.h"
28 #include "pagespeed/kernel/base/scoped_ptr.h"
29 #include "pagespeed/kernel/base/string.h"
30 #include "pagespeed/kernel/base/string_util.h"
31 #include "pagespeed/kernel/base/thread_annotations.h"
32 #include "pagespeed/kernel/base/thread_system.h"
33 
34 namespace net_instaweb {
35 
36 class Hasher;
37 class MessageHandler;
38 class RewriteOptions;
39 struct ContentType;
40 
48  public:
49  static const char kGStaticBase[];
50  static const char kDefaultLibraryUrlPrefix[];
51 
52  enum ConfigurationMode {
53  kInitialConfiguration,
54  kUpdateConfiguration
55  };
56 
58  StaticAssetManager(const GoogleString& static_asset_base,
59  ThreadSystem* threads,
60  Hasher* hasher,
61  MessageHandler* message_handler);
62 
64 
67  const GoogleString& GetAssetUrl(StaticAssetEnum::StaticAsset module,
68  const RewriteOptions* options) const;
69 
71  const char* GetAsset(StaticAssetEnum::StaticAsset module,
72  const RewriteOptions* options) const;
73 
79  bool GetAsset(StringPiece file_name, StringPiece* content,
80  ContentType* content_type, StringPiece* cache_header) const;
81 
85  void SetGStaticHashForTest(StaticAssetEnum::StaticAsset module,
86  const GoogleString& hash);
87 
92  void ServeAssetsFromGStatic(StringPiece gstatic_base) {
93  ScopedMutex write_lock(lock_.get());
94  serve_assets_from_gstatic_ = true;
95  gstatic_base.CopyToString(&gstatic_base_);
96  }
97 
98  void DoNotServeAssetsFromGStatic() {
99  ScopedMutex write_lock(lock_.get());
100  serve_assets_from_gstatic_ = false;
101  gstatic_base_.clear();
102  }
103 
114  void ApplyGStaticConfiguration(const StaticAssetConfig& config,
115  ConfigurationMode mode);
116 
122 
124  void set_library_url_prefix(const StringPiece& url_prefix) {
125  ScopedMutex write_lock(lock_.get());
126  url_prefix.CopyToString(&library_url_prefix_);
127  InitializeAssetUrls();
128  }
129 
130  void set_static_asset_base(const StringPiece& x) {
131  ScopedMutex write_lock(lock_.get());
132  x.CopyToString(&static_asset_base_);
133  InitializeAssetUrls();
134  }
135 
136  private:
137  class Asset;
138 
139  typedef std::map<GoogleString, StaticAssetEnum::StaticAsset>
140  FileNameToModuleMap;
141 
142  void InitializeAssetStrings();
143  void InitializeAssetUrls() EXCLUSIVE_LOCKS_REQUIRED(lock_);
144 
148  void ApplyGStaticConfigurationImpl(const StaticAssetConfig& config,
149  ConfigurationMode mode)
150  EXCLUSIVE_LOCKS_REQUIRED(lock_);
151 
152  GoogleString static_asset_base_;
154  Hasher* hasher_;
155  MessageHandler* message_handler_;
156 
157  scoped_ptr<ThreadSystem::RWLock> lock_;
158  std::vector<Asset*> assets_ GUARDED_BY(lock_);
159  FileNameToModuleMap file_name_to_module_map_ GUARDED_BY(lock_);
160 
161  bool serve_assets_from_gstatic_ GUARDED_BY(lock_);
162  GoogleString gstatic_base_ GUARDED_BY(lock_);
163  scoped_ptr<StaticAssetConfig> initial_gstatic_config_ GUARDED_BY(lock_);
164  GoogleString library_url_prefix_ GUARDED_BY(lock_);
165  GoogleString cache_header_with_long_ttl_ GUARDED_BY(lock_);
166  GoogleString cache_header_with_private_ttl_ GUARDED_BY(lock_);
167 
168 
169 };
170 
171 }
172 
173 #endif
void SetGStaticHashForTest(StaticAssetEnum::StaticAsset module, const GoogleString &hash)
StaticAssetManager(const GoogleString &static_asset_base, ThreadSystem *threads, Hasher *hasher, MessageHandler *message_handler)
static_asset_base is path on this host we serve resources from.
Definition: static_asset_manager.h:47
void set_library_url_prefix(const StringPiece &url_prefix)
Set the prefix for the URLs of assets.
Definition: static_asset_manager.h:124
void ServeAssetsFromGStatic(StringPiece gstatic_base)
Definition: static_asset_manager.h:92
const char * GetAsset(StaticAssetEnum::StaticAsset module, const RewriteOptions *options) const
Returns the contents of the asset.
const GoogleString & GetAssetUrl(StaticAssetEnum::StaticAsset module, const RewriteOptions *options) const
void ApplyGStaticConfiguration(const StaticAssetConfig &config, ConfigurationMode mode)
Definition: rewrite_options.h:84