2
* Copyright 2012 Google Inc.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
// Author: sligocki@google.com (Shawn Ligocki)
19
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
20
#define NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
24
#include "net/instaweb/rewriter/public/css_tag_scanner.h"
25
#include "net/instaweb/rewriter/public/resource.h"
26
#include "net/instaweb/rewriter/public/resource_slot.h"
27
#include "net/instaweb/util/public/basictypes.h"
28
#include "net/instaweb/util/public/gtest_prod.h"
29
#include "net/instaweb/util/public/string.h"
30
#include "net/instaweb/util/public/string_util.h"
32
namespace net_instaweb {
39
// Transformer that uses a std::map to specify which URLs to rewrite to
41
// Used by CssFilter to rewrite subresources in CSS even when it cannot
42
// be parsed, by using AssociationSlots to update the map before transforming.
43
class AssociationTransformer : public CssTagScanner::Transformer {
45
// base_url is the URL all CSS url()s should be absolutified against,
46
// this is generally the URL for the CSS file or HTML file for inline CSS.
47
// backup_transformer is another transformer to be applied if no
48
// association has been set in AssociationTransformer's map_. It may be
49
// set to NULL if no backup is needed.
51
// base_url, options, backup_transformer and handler must live longer than
52
// AssociationTransformer.
53
AssociationTransformer(const GoogleUrl* base_url,
54
const RewriteOptions* options,
55
CssTagScanner::Transformer* backup_transformer,
56
MessageHandler* handler)
57
: base_url_(base_url), options_(options),
58
backup_transformer_(backup_transformer), handler_(handler) {}
59
virtual ~AssociationTransformer();
61
// Map is exposed so that you can set associations.
62
// Each key -> value specifies that every instance of the absolute URL
63
// key should be transformed to the absolute URL value.
64
StringStringMap* map() { return &map_; }
66
// To do the actual transformation. Call CssTagScanner::TransformUrls()
67
// with this AssociationTransformer which will call Transform() on all URLs.
68
// Transform will lookup all (absolutified) URLs in map_ and rewrite them
69
// if present (otherwise it will pass them to the backup_transformer_).
70
virtual TransformStatus Transform(GoogleString* str);
73
// Mapping of input URLs to output URLs.
76
// Base URL for CSS file, needed to absolutify URLs in Transform.
77
const GoogleUrl* base_url_;
78
const RewriteOptions* options_;
80
// Transformer to be applied to URLs we don't rewrite. For example, we might
81
// want to make sure we absolutify all URLs, even if we don't rewrite them.
82
CssTagScanner::Transformer* backup_transformer_;
84
MessageHandler* handler_;
86
FRIEND_TEST(AssociationTransformerTest, TransformsCorrectly);
88
DISALLOW_COPY_AND_ASSIGN(AssociationTransformer);
91
// Extremely simple slot which just sets an association in a std::map when
92
// it is Render()ed. It associates the key (input URL) with this slot's
93
// resource URL (the output URL).
94
// Can be used to set AssociationTransformer::map() so that
95
// AssocitationTransformer::Transform() will rewrite the rendered URLs.
96
class AssociationSlot : public ResourceSlot {
98
// Note: map must outlive AssociationSlot.
99
AssociationSlot(ResourcePtr resource,
100
StringStringMap* map, const StringPiece& key)
101
: ResourceSlot(resource), map_(map) {
102
key.CopyToString(&key_);
104
virtual ~AssociationSlot();
106
virtual HtmlElement* element() const { return NULL; }
108
// All Render() calls are from the same thread, so this doesn't need to be
110
virtual void Render() {
111
// We should never try to render unauthorized resource URLs as is.
112
if (!resource()->is_authorized_domain()) {
115
if (!disable_rendering()) {
116
(*map_)[key_] = resource()->url();
120
virtual bool DirectSetUrl(const StringPiece& url) {
121
// We should never try to render unauthorized resource URLs as is.
122
if (!resource()->is_authorized_domain()) {
125
url.CopyToString(&((*map_)[key_]));
129
virtual GoogleString LocationString() {
130
// TODO(sligocki): Improve quality of this diagnostic.
131
// Also improve CssResourceSlot::LocationString() which is identical.
136
StringStringMap* map_;
139
DISALLOW_COPY_AND_ASSIGN(AssociationSlot);
142
} // namespace net_instaweb
144
#endif // NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_