~alinuxninja/nginx-edge/trunk

« back to all changes in this revision

Viewing changes to debian/modules/ngx_pagespeed/psol/include/net/instaweb/rewriter/public/association_transformer.h

  • Committer: Vivian
  • Date: 2015-12-04 18:20:11 UTC
  • Revision ID: git-v1:a36f2bc32e884f7473b3a47040e5411306144d7d
* Do not extract psol.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 
17
 
// Author: sligocki@google.com (Shawn Ligocki)
18
 
 
19
 
#ifndef NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
20
 
#define NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_
21
 
 
22
 
#include <map>
23
 
 
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"
31
 
 
32
 
namespace net_instaweb {
33
 
 
34
 
class GoogleUrl;
35
 
class HtmlElement;
36
 
class MessageHandler;
37
 
class RewriteOptions;
38
 
 
39
 
// Transformer that uses a std::map to specify which URLs to rewrite to
40
 
// which other URLs.
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 {
44
 
 public:
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.
50
 
  //
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();
60
 
 
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_; }
65
 
 
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);
71
 
 
72
 
 private:
73
 
  // Mapping of input URLs to output URLs.
74
 
  StringStringMap map_;
75
 
 
76
 
  // Base URL for CSS file, needed to absolutify URLs in Transform.
77
 
  const GoogleUrl* base_url_;
78
 
  const RewriteOptions* options_;
79
 
 
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_;
83
 
 
84
 
  MessageHandler* handler_;
85
 
 
86
 
  FRIEND_TEST(AssociationTransformerTest, TransformsCorrectly);
87
 
 
88
 
  DISALLOW_COPY_AND_ASSIGN(AssociationTransformer);
89
 
};
90
 
 
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 {
97
 
 public:
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_);
103
 
  }
104
 
  virtual ~AssociationSlot();
105
 
 
106
 
  virtual HtmlElement* element() const { return NULL; }
107
 
 
108
 
  // All Render() calls are from the same thread, so this doesn't need to be
109
 
  // thread-safe.
110
 
  virtual void Render() {
111
 
    // We should never try to render unauthorized resource URLs as is.
112
 
    if (!resource()->is_authorized_domain()) {
113
 
      return;
114
 
    }
115
 
    if (!disable_rendering()) {
116
 
      (*map_)[key_] = resource()->url();
117
 
    }
118
 
  }
119
 
 
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()) {
123
 
      return false;
124
 
    }
125
 
    url.CopyToString(&((*map_)[key_]));
126
 
    return true;
127
 
  }
128
 
 
129
 
  virtual GoogleString LocationString() {
130
 
    // TODO(sligocki): Improve quality of this diagnostic.
131
 
    // Also improve CssResourceSlot::LocationString() which is identical.
132
 
    return "Inside CSS";
133
 
  }
134
 
 
135
 
 private:
136
 
  StringStringMap* map_;
137
 
  GoogleString key_;
138
 
 
139
 
  DISALLOW_COPY_AND_ASSIGN(AssociationSlot);
140
 
};
141
 
 
142
 
}  // namespace net_instaweb
143
 
 
144
 
#endif  // NET_INSTAWEB_REWRITER_PUBLIC_ASSOCIATION_TRANSFORMER_H_