~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/content/base/src/nsContentPolicy.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 
2
// vim: ft=cpp tw=78 sw=4 et ts=8
 
3
/* ***** BEGIN LICENSE BLOCK *****
 
4
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
5
 *
 
6
 * The contents of this file are subject to the Mozilla Public License Version
 
7
 * 1.1 (the "License"); you may not use this file except in compliance with
 
8
 * the License. You may obtain a copy of the License at
 
9
 * http://www.mozilla.org/MPL/
 
10
 *
 
11
 * Software distributed under the License is distributed on an "AS IS" basis,
 
12
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
13
 * for the specific language governing rights and limitations under the
 
14
 * License.
 
15
 *
 
16
 * The Original Code is Mozilla code.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Zero-Knowledge Systems, Inc.
 
20
 * Portions created by the Initial Developer are Copyright (C) 2000
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 
27
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the MPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the MPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "prlog.h"
 
40
 
 
41
#include "nsISupports.h"
 
42
#include "nsXPCOM.h"
 
43
#include "nsISupportsPrimitives.h"
 
44
#include "nsXPIDLString.h"
 
45
#include "nsContentPolicyUtils.h"
 
46
#include "nsContentPolicy.h"
 
47
#include "nsICategoryManager.h"
 
48
#include "nsIURI.h"
 
49
#include "nsIDOMNode.h"
 
50
#include "nsIDOMWindow.h"
 
51
 
 
52
NS_IMPL_ISUPPORTS1(nsContentPolicy, nsIContentPolicy)
 
53
 
 
54
#ifdef PR_LOGGING
 
55
static PRLogModuleInfo* gConPolLog;
 
56
#endif
 
57
 
 
58
nsresult
 
59
NS_NewContentPolicy(nsIContentPolicy **aResult)
 
60
{
 
61
  *aResult = new nsContentPolicy;
 
62
  if (!*aResult)
 
63
      return NS_ERROR_OUT_OF_MEMORY;
 
64
  NS_ADDREF(*aResult);
 
65
  return NS_OK;
 
66
}
 
67
 
 
68
/*
 
69
 * This constructor does far too much.  I wish there was a way to get
 
70
 * an Init method called by the service manager after the factory
 
71
 * returned the new object, so that errors could be propagated back to
 
72
 * the caller correctly.
 
73
 */
 
74
nsContentPolicy::nsContentPolicy()
 
75
{
 
76
#ifdef PR_LOGGING
 
77
    if (! gConPolLog) {
 
78
        gConPolLog = PR_NewLogModule("nsContentPolicy");
 
79
    }
 
80
#endif
 
81
    nsresult rv;
 
82
    nsCOMPtr<nsICategoryManager> catman = 
 
83
             do_GetService(NS_CATEGORYMANAGER_CONTRACTID, &rv);
 
84
    if (NS_FAILED(rv))
 
85
        return; /* log an error? */
 
86
 
 
87
    /*
 
88
     * I'd like to use GetCategoryContents, so that I can size the array
 
89
     * correctly on the first go and avoid the enumerator overhead, but it's
 
90
     * not yet implemented (see nsCategoryManager.cpp).  No biggie, I guess.
 
91
     */
 
92
    nsCOMPtr<nsISimpleEnumerator> catEnum;
 
93
    rv = catman->EnumerateCategory(NS_CONTENTPOLICY_CATEGORY,
 
94
                                   getter_AddRefs(catEnum));
 
95
    if (NS_FAILED(rv))
 
96
        return; /* no category, no problem */
 
97
 
 
98
    PRBool hasMore;
 
99
    if (NS_FAILED(catEnum->HasMoreElements(&hasMore)) || !hasMore)
 
100
       return;
 
101
    
 
102
    /* 
 
103
     * Populate mPolicies with policy services named by contractids in the
 
104
     * "content-policy" category.
 
105
     */
 
106
    nsCOMPtr<nsISupports> item;
 
107
    while (NS_SUCCEEDED(catEnum->GetNext(getter_AddRefs(item)))) {
 
108
        nsCOMPtr<nsISupportsCString> string = do_QueryInterface(item, &rv);
 
109
        if (NS_FAILED(rv))
 
110
            continue;
 
111
 
 
112
        nsCAutoString contractid;
 
113
        if (NS_FAILED(string->GetData(contractid)))
 
114
            continue;
 
115
 
 
116
        PR_LOG(gConPolLog, PR_LOG_DEBUG,
 
117
                ("POLICY: loading %s\n", contractid.get()));
 
118
 
 
119
        /*
 
120
         * Create this policy service and add to mPolicies.
 
121
         *
 
122
         * Should we try to parse as a CID, in case the component prefers to be
 
123
         * registered that way?
 
124
         */
 
125
        nsCOMPtr<nsIContentPolicy> policy = do_GetService(contractid.get(),
 
126
                                                          &rv);
 
127
        if (NS_SUCCEEDED(rv) && policy) {
 
128
            mPolicies.AppendObject(policy);
 
129
        }
 
130
    }
 
131
        
 
132
}
 
133
 
 
134
nsContentPolicy::~nsContentPolicy()
 
135
{
 
136
}
 
137
 
 
138
#ifdef DEBUG
 
139
#define WARN_IF_URI_UNINITIALIZED(uri,name)                         \
 
140
  PR_BEGIN_MACRO                                                    \
 
141
    if ((uri)) {                                                    \
 
142
        nsCAutoString spec;                                         \
 
143
        (uri)->GetAsciiSpec(spec);                                  \
 
144
        if (spec.IsEmpty()) {                                       \
 
145
            NS_WARNING(name " is uninitialized, fix caller");       \
 
146
        }                                                           \
 
147
    }                                                               \
 
148
  PR_END_MACRO
 
149
 
 
150
#else  // ! defined(DEBUG)
 
151
 
 
152
#define WARN_IF_URI_UNINITIALIZED(uri,name)
 
153
 
 
154
#endif // defined(DEBUG)
 
155
 
 
156
inline nsresult
 
157
nsContentPolicy::CheckPolicy(CPMethod          policyMethod,
 
158
                             PRUint32          contentType,
 
159
                             nsIURI           *contentLocation,
 
160
                             nsIURI           *requestingLocation,
 
161
                             nsISupports      *requestingContext,
 
162
                             const nsACString &mimeType,
 
163
                             nsISupports      *extra,
 
164
                             PRInt16           *decision)
 
165
{
 
166
    //sanity-check passed-through parameters
 
167
    NS_PRECONDITION(decision, "Null out pointer");
 
168
    WARN_IF_URI_UNINITIALIZED(contentLocation, "Request URI");
 
169
    WARN_IF_URI_UNINITIALIZED(requestingLocation, "Requesting URI");
 
170
 
 
171
#ifdef DEBUG
 
172
    {
 
173
        nsCOMPtr<nsIDOMNode> node(do_QueryInterface(requestingContext));
 
174
        nsCOMPtr<nsIDOMWindow> window(do_QueryInterface(requestingContext));
 
175
        NS_ASSERTION(!requestingContext || node || window,
 
176
                     "Context should be a DOM node or a DOM window!");
 
177
    }
 
178
#endif
 
179
 
 
180
    PRInt32 count = mPolicies.Count();
 
181
    nsresult rv = NS_OK;
 
182
 
 
183
    /* 
 
184
     * Enumerate mPolicies and ask each of them, taking the logical AND of
 
185
     * their permissions.
 
186
     */
 
187
    for (PRInt32 i = 0; i < count; i++) {
 
188
        nsIContentPolicy *policy = mPolicies[i];
 
189
        if (!policy) { //shouldn't happen
 
190
            NS_ERROR("Somehow a null policy got into the list");
 
191
            continue;
 
192
        }
 
193
 
 
194
        /* check the appropriate policy */
 
195
        rv = (policy->*policyMethod)(contentType, contentLocation,
 
196
                                     requestingLocation, requestingContext,
 
197
                                     mimeType, extra, decision);
 
198
 
 
199
        if (NS_SUCCEEDED(rv) && NS_CP_REJECTED(*decision)) {
 
200
            /* policy says no, no point continuing to check */
 
201
            return NS_OK;
 
202
        }
 
203
    }
 
204
 
 
205
    // everyone returned failure, or no policies: sanitize result
 
206
    *decision = nsIContentPolicy::ACCEPT;
 
207
    return NS_OK;
 
208
}
 
209
 
 
210
#ifdef PR_LOGGING
 
211
 
 
212
//uses the parameters from ShouldXYZ to produce and log a message
 
213
//logType must be a literal string constant
 
214
#define LOG_CHECK(logType)                                                    \
 
215
  PR_BEGIN_MACRO                                                              \
 
216
    /* skip all this nonsense if the call failed */                           \
 
217
    if (NS_SUCCEEDED(rv)) {                                                   \
 
218
      const char *resultName;                                                 \
 
219
      if (decision) {                                                         \
 
220
        resultName = NS_CP_ResponseName(*decision);                           \
 
221
      } else {                                                                \
 
222
        resultName = "(null ptr)";                                            \
 
223
      }                                                                       \
 
224
      nsCAutoString spec("None");                                             \
 
225
      if (contentLocation) {                                                  \
 
226
          contentLocation->GetSpec(spec);                                     \
 
227
      }                                                                       \
 
228
      nsCAutoString refSpec("None");                                          \
 
229
      if (requestingLocation) {                                               \
 
230
          requestingLocation->GetSpec(refSpec);                               \
 
231
      }                                                                       \
 
232
      PR_LOG(gConPolLog, PR_LOG_DEBUG,                                        \
 
233
             ("Content Policy: " logType ": <%s> <Ref:%s> result=%s",         \
 
234
              spec.get(), refSpec.get(), resultName)                          \
 
235
             );                                                               \
 
236
    }                                                                         \
 
237
  PR_END_MACRO
 
238
 
 
239
#else //!defined(PR_LOGGING)
 
240
 
 
241
#define LOG_CHECK(logType)
 
242
 
 
243
#endif //!defined(PR_LOGGING)
 
244
 
 
245
NS_IMETHODIMP
 
246
nsContentPolicy::ShouldLoad(PRUint32          contentType,
 
247
                            nsIURI           *contentLocation,
 
248
                            nsIURI           *requestingLocation,
 
249
                            nsISupports      *requestingContext,
 
250
                            const nsACString &mimeType,
 
251
                            nsISupports      *extra,
 
252
                            PRInt16          *decision)
 
253
{
 
254
    // ShouldProcess does not need a content location, but we do
 
255
    NS_PRECONDITION(contentLocation, "Must provide request location");
 
256
    nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldLoad, contentType,
 
257
                              contentLocation, requestingLocation,
 
258
                              requestingContext, mimeType, extra, decision);
 
259
    LOG_CHECK("ShouldLoad");
 
260
 
 
261
    return rv;
 
262
}
 
263
 
 
264
NS_IMETHODIMP
 
265
nsContentPolicy::ShouldProcess(PRUint32          contentType,
 
266
                               nsIURI           *contentLocation,
 
267
                               nsIURI           *requestingLocation,
 
268
                               nsISupports      *requestingContext,
 
269
                               const nsACString &mimeType,
 
270
                               nsISupports      *extra,
 
271
                               PRInt16          *decision)
 
272
{
 
273
    nsresult rv = CheckPolicy(&nsIContentPolicy::ShouldProcess, contentType,
 
274
                              contentLocation, requestingLocation,
 
275
                              requestingContext, mimeType, extra, decision);
 
276
    LOG_CHECK("ShouldProcess");
 
277
 
 
278
    return rv;
 
279
}