1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "content/child/web_url_request_util.h"
6
7#include "base/logging.h"
8#include "third_party/WebKit/public/platform/WebURLRequest.h"
9
10using blink::WebURLRequest;
11
12namespace content {
13
14ResourceType WebURLRequestToResourceType(const WebURLRequest& request) {
15  WebURLRequest::RequestContext requestContext = request.requestContext();
16  if (request.frameType() != WebURLRequest::FrameTypeNone) {
17    DCHECK(requestContext == WebURLRequest::RequestContextForm ||
18           requestContext == WebURLRequest::RequestContextFrame ||
19           requestContext == WebURLRequest::RequestContextHyperlink ||
20           requestContext == WebURLRequest::RequestContextIframe ||
21           requestContext == WebURLRequest::RequestContextInternal ||
22           requestContext == WebURLRequest::RequestContextLocation);
23    if (request.frameType() == WebURLRequest::FrameTypeTopLevel ||
24        request.frameType() == WebURLRequest::FrameTypeAuxiliary) {
25      return RESOURCE_TYPE_MAIN_FRAME;
26    }
27    if (request.frameType() == WebURLRequest::FrameTypeNested)
28      return RESOURCE_TYPE_SUB_FRAME;
29    NOTREACHED();
30    return RESOURCE_TYPE_SUB_RESOURCE;
31  }
32
33  switch (requestContext) {
34    // Favicon
35    case WebURLRequest::RequestContextFavicon:
36      return RESOURCE_TYPE_FAVICON;
37
38    // Font
39    case WebURLRequest::RequestContextFont:
40      return RESOURCE_TYPE_FONT_RESOURCE;
41
42    // Image
43    case WebURLRequest::RequestContextImage:
44    case WebURLRequest::RequestContextImageSet:
45      return RESOURCE_TYPE_IMAGE;
46
47    // Media
48    case WebURLRequest::RequestContextAudio:
49    case WebURLRequest::RequestContextVideo:
50      return RESOURCE_TYPE_MEDIA;
51
52    // Object
53    case WebURLRequest::RequestContextEmbed:
54    case WebURLRequest::RequestContextObject:
55      return RESOURCE_TYPE_OBJECT;
56
57    // Ping
58    case WebURLRequest::RequestContextBeacon:
59    case WebURLRequest::RequestContextCSPReport:
60    case WebURLRequest::RequestContextPing:
61      return RESOURCE_TYPE_PING;
62
63    // Prefetch
64    case WebURLRequest::RequestContextPrefetch:
65      return RESOURCE_TYPE_PREFETCH;
66
67    // Script
68    case WebURLRequest::RequestContextImport:
69    case WebURLRequest::RequestContextScript:
70      return RESOURCE_TYPE_SCRIPT;
71
72    // Style
73    case WebURLRequest::RequestContextXSLT:
74    case WebURLRequest::RequestContextStyle:
75      return RESOURCE_TYPE_STYLESHEET;
76
77    // Subresource
78    case WebURLRequest::RequestContextDownload:
79    case WebURLRequest::RequestContextManifest:
80    case WebURLRequest::RequestContextSubresource:
81    case WebURLRequest::RequestContextPlugin:
82      return RESOURCE_TYPE_SUB_RESOURCE;
83
84    // TextTrack
85    case WebURLRequest::RequestContextTrack:
86      return RESOURCE_TYPE_MEDIA;
87
88    // Workers
89    case WebURLRequest::RequestContextServiceWorker:
90      return RESOURCE_TYPE_SERVICE_WORKER;
91    case WebURLRequest::RequestContextSharedWorker:
92      return RESOURCE_TYPE_SHARED_WORKER;
93    case WebURLRequest::RequestContextWorker:
94      return RESOURCE_TYPE_WORKER;
95
96    // Unspecified
97    case WebURLRequest::RequestContextInternal:
98    case WebURLRequest::RequestContextUnspecified:
99      return RESOURCE_TYPE_SUB_RESOURCE;
100
101    // XHR
102    case WebURLRequest::RequestContextEventSource:
103    case WebURLRequest::RequestContextFetch:
104    case WebURLRequest::RequestContextXMLHttpRequest:
105      return RESOURCE_TYPE_XHR;
106
107    // These should be handled by the FrameType checks at the top of the
108    // function.
109    case WebURLRequest::RequestContextForm:
110    case WebURLRequest::RequestContextHyperlink:
111    case WebURLRequest::RequestContextLocation:
112    case WebURLRequest::RequestContextFrame:
113    case WebURLRequest::RequestContextIframe:
114      NOTREACHED();
115      return RESOURCE_TYPE_SUB_RESOURCE;
116
117    default:
118      NOTREACHED();
119      return RESOURCE_TYPE_SUB_RESOURCE;
120  }
121}
122
123}  // namespace content
124