Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 #include <cstddef>
25 
26 #include "net/instaweb/rewriter/static_asset_config.pb.h"
32 #include "pagespeed/kernel/base/thread_annotations.h"
35 
36 namespace net_instaweb {
37 
38 class Hasher;
39 class MessageHandler;
40 class RewriteOptions;
41 
49  public:
50  static const char kGStaticBase[];
51  static const char kDefaultLibraryUrlPrefix[];
52 
53  enum ConfigurationMode {
54  kInitialConfiguration,
55  kUpdateConfiguration
56  };
57 
59  StaticAssetManager(const GoogleString& static_asset_base,
60  ThreadSystem* threads,
61  Hasher* hasher,
62  MessageHandler* message_handler);
63 
65 
67  bool IsValidIndex(size_t i) const {
68  lock_->Lock();
69  bool valid = (i <= assets_.size()) && (assets_[i]->file_name != nullptr);
70  lock_->Unlock();
71  return valid;
72  }
73 
76  const GoogleString& GetAssetUrl(StaticAssetEnum::StaticAsset module,
77  const RewriteOptions* options) const;
78 
80  const char* GetAsset(StaticAssetEnum::StaticAsset module,
81  const RewriteOptions* options) const;
82 
88  bool GetAsset(StringPiece file_name, StringPiece* content,
89  ContentType* content_type, StringPiece* cache_header) const;
90 
94  void SetGStaticHashForTest(StaticAssetEnum::StaticAsset module,
95  const GoogleString& hash);
96 
101  void ServeAssetsFromGStatic(StringPiece gstatic_base) {
102  ScopedMutex write_lock(lock_.get());
103  serve_assets_from_gstatic_ = true;
104  gstatic_base.CopyToString(&gstatic_base_);
105  }
106 
107  void DoNotServeAssetsFromGStatic() {
108  ScopedMutex write_lock(lock_.get());
109  serve_assets_from_gstatic_ = false;
110  gstatic_base_.clear();
111  }
112 
123  void ApplyGStaticConfiguration(const StaticAssetConfig& config,
124  ConfigurationMode mode);
125 
131 
133  void set_library_url_prefix(const StringPiece& url_prefix) {
134  ScopedMutex write_lock(lock_.get());
135  url_prefix.CopyToString(&library_url_prefix_);
136  InitializeAssetUrls();
137  }
138 
139  void set_static_asset_base(const StringPiece& x) {
140  ScopedMutex write_lock(lock_.get());
141  x.CopyToString(&static_asset_base_);
142  InitializeAssetUrls();
143  }
144 
145  private:
148  struct Asset {
149  const char* file_name;
150  GoogleString js_optimized;
151  GoogleString js_debug;
152  GoogleString js_opt_hash;
153  GoogleString js_debug_hash;
154  GoogleString opt_url;
155  GoogleString debug_url;
156  GoogleString release_label;
157  ContentType content_type;
158  };
159 
160  typedef std::map<GoogleString, StaticAssetEnum::StaticAsset>
161  FileNameToModuleMap;
162 
163  void InitializeAssetStrings();
164  void InitializeAssetUrls() EXCLUSIVE_LOCKS_REQUIRED(lock_);
165 
166 
170  void ApplyGStaticConfigurationImpl(const StaticAssetConfig& config,
171  ConfigurationMode mode)
172  EXCLUSIVE_LOCKS_REQUIRED(lock_);
173 
174  GoogleString static_asset_base_;
176  Hasher* hasher_;
177  MessageHandler* message_handler_;
178 
179  scoped_ptr<ThreadSystem::RWLock> lock_;
180  std::vector<Asset*> assets_ GUARDED_BY(lock_);
181  FileNameToModuleMap file_name_to_module_map_ GUARDED_BY(lock_);
182 
183  bool serve_assets_from_gstatic_ GUARDED_BY(lock_);
184  GoogleString gstatic_base_ GUARDED_BY(lock_);
185  scoped_ptr<StaticAssetConfig> initial_gstatic_config_ GUARDED_BY(lock_);
186  GoogleString library_url_prefix_ GUARDED_BY(lock_);
187  GoogleString cache_header_with_long_ttl_ GUARDED_BY(lock_);
188  GoogleString cache_header_with_private_ttl_ GUARDED_BY(lock_);
189 
190 
191 };
192 
193 }
194 
195 #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:48
bool IsValidIndex(size_t i) const
Determines whether the specified index is a valid asset enum.
Definition: static_asset_manager.h:67
void set_library_url_prefix(const StringPiece &url_prefix)
Set the prefix for the URLs of assets.
Definition: static_asset_manager.h:133
void ServeAssetsFromGStatic(StringPiece gstatic_base)
Definition: static_asset_manager.h:101
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
Helper class for lexically scoped mutexing.
Definition: abstract_mutex.h:46
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
Definition: content_type.h:31
Definition: thread_system.h:40
Definition: message_handler.h:39
void ApplyGStaticConfiguration(const StaticAssetConfig &config, ConfigurationMode mode)
Definition: rewrite_options.h:84
Definition: hasher.h:30