1 // Copyright 2013 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.
5 #ifndef CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_
6 #define CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/time/time.h"
13 #include "content/common/content_export.h"
14 #include "media/blink/active_loader.h"
15 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
16 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
17 #include "third_party/WebKit/public/web/WebDocument.h"
28 // This class provides additional information about a media URL. Currently it
29 // can be used to determine if a media URL has a single security origin and
30 // whether the URL passes a CORS access check.
31 class CONTENT_EXPORT MediaInfoLoader
: private blink::WebURLLoaderClient
{
33 // Status codes for start operations on MediaInfoLoader.
35 // The operation failed, which may have been due to:
37 // - Server replied 4xx/5xx
38 // - The response was invalid
39 // - Connection was terminated
41 // At this point you should delete the loader.
44 // Everything went as planned.
48 // Callback when MediaInfoLoader finishes loading the url. Args: whether URL
49 // is successfully loaded, the final URL destination following all the
50 // redirect, the first party URL for the final destination, and whether
51 // credentials needs to be sent to the final destination.
52 typedef base::Callback
<void(Status
, const GURL
&, const GURL
&, bool)> ReadyCB
;
54 // Start loading information about the given media URL.
55 // |url| - URL for the media resource to be loaded.
56 // |cors_mode| - HTML media element's crossorigin attribute.
57 // |ready_cb| - Called when media info has finished or failed loading.
60 blink::WebMediaPlayer::CORSMode cors_mode
,
61 const ReadyCB
& ready_cb
);
62 virtual ~MediaInfoLoader();
64 // Start loading media info.
65 void Start(blink::WebFrame
* frame
);
67 // Returns true if the media resource has a single origin, false otherwise.
68 // Only valid to call after the loader becomes ready.
69 bool HasSingleOrigin() const;
71 // Returns true if the media resource passed a CORS access control check.
72 // Only valid to call after the loader becomes ready.
73 bool DidPassCORSAccessCheck() const;
76 friend class MediaInfoLoaderTest
;
78 // blink::WebURLLoaderClient implementation.
79 virtual void willSendRequest(
80 blink::WebURLLoader
* loader
,
81 blink::WebURLRequest
& newRequest
,
82 const blink::WebURLResponse
& redirectResponse
);
83 virtual void didSendData(
84 blink::WebURLLoader
* loader
,
85 unsigned long long bytesSent
,
86 unsigned long long totalBytesToBeSent
);
87 virtual void didReceiveResponse(
88 blink::WebURLLoader
* loader
,
89 const blink::WebURLResponse
& response
);
90 virtual void didDownloadData(
91 blink::WebURLLoader
* loader
,
93 int encodedDataLength
);
94 virtual void didReceiveData(
95 blink::WebURLLoader
* loader
,
98 int encoded_data_length
);
99 virtual void didReceiveCachedMetadata(
100 blink::WebURLLoader
* loader
,
101 const char* data
, int dataLength
);
102 virtual void didFinishLoading(
103 blink::WebURLLoader
* loader
,
105 int64_t total_encoded_data_length
);
106 virtual void didFail(
107 blink::WebURLLoader
* loader
,
108 const blink::WebURLError
&);
110 void DidBecomeReady(Status status
);
112 // Injected WebURLLoader instance for testing purposes.
113 scoped_ptr
<blink::WebURLLoader
> test_loader_
;
115 // Keeps track of an active WebURLLoader and associated state.
116 scoped_ptr
<media::ActiveLoader
> active_loader_
;
120 GURL first_party_url_
;
121 bool allow_stored_credentials_
;
122 blink::WebMediaPlayer::CORSMode cors_mode_
;
126 base::TimeTicks start_time_
;
128 DISALLOW_COPY_AND_ASSIGN(MediaInfoLoader
);
131 } // namespace content
133 #endif // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_