Upstream version 9.37.195.0
[platform/framework/web/crosswalk.git] / src / content / renderer / media / android / media_info_loader.h
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.
4
5 #ifndef CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_
6 #define CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_
7
8 #include <string>
9
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 "content/renderer/media/active_loader.h"
15 #include "third_party/WebKit/public/platform/WebMediaPlayer.h"
16 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
17 #include "url/gurl.h"
18
19 namespace blink {
20 class WebFrame;
21 class WebURLLoader;
22 class WebURLRequest;
23 }
24
25 namespace content {
26
27 // This class provides additional information about a media URL. Currently it
28 // can be used to determine if a media URL has a single security origin and
29 // whether the URL passes a CORS access check.
30 class CONTENT_EXPORT MediaInfoLoader : private blink::WebURLLoaderClient {
31  public:
32   // Status codes for start operations on MediaInfoLoader.
33   enum Status {
34     // The operation failed, which may have been due to:
35     //   - Page navigation
36     //   - Server replied 4xx/5xx
37     //   - The response was invalid
38     //   - Connection was terminated
39     //
40     // At this point you should delete the loader.
41     kFailed,
42
43     // Everything went as planned.
44     kOk,
45   };
46
47   // Start loading information about the given media URL.
48   // |url| - URL for the media resource to be loaded.
49   // |cors_mode| - HTML media element's crossorigin attribute.
50   // |ready_cb| - Called when media info has finished or failed loading.
51   typedef base::Callback<void(Status)> ReadyCB;
52   MediaInfoLoader(
53       const GURL& url,
54       blink::WebMediaPlayer::CORSMode cors_mode,
55       const ReadyCB& ready_cb);
56   virtual ~MediaInfoLoader();
57
58   // Start loading media info.
59   void Start(blink::WebFrame* frame);
60
61   // Returns true if the media resource has a single origin, false otherwise.
62   // Only valid to call after the loader becomes ready.
63   bool HasSingleOrigin() const;
64
65   // Returns true if the media resource passed a CORS access control check.
66   // Only valid to call after the loader becomes ready.
67   bool DidPassCORSAccessCheck() const;
68
69   void set_single_origin(bool single_origin) {
70     single_origin_ = single_origin;
71   }
72
73  private:
74   friend class MediaInfoLoaderTest;
75
76   // blink::WebURLLoaderClient implementation.
77   virtual void willSendRequest(
78       blink::WebURLLoader* loader,
79       blink::WebURLRequest& newRequest,
80       const blink::WebURLResponse& redirectResponse);
81   virtual void didSendData(
82       blink::WebURLLoader* loader,
83       unsigned long long bytesSent,
84       unsigned long long totalBytesToBeSent);
85   virtual void didReceiveResponse(
86       blink::WebURLLoader* loader,
87       const blink::WebURLResponse& response);
88   virtual void didDownloadData(
89       blink::WebURLLoader* loader,
90       int data_length,
91       int encodedDataLength);
92   virtual void didReceiveData(
93       blink::WebURLLoader* loader,
94       const char* data,
95       int data_length,
96       int encoded_data_length);
97   virtual void didReceiveCachedMetadata(
98       blink::WebURLLoader* loader,
99       const char* data, int dataLength);
100   virtual void didFinishLoading(
101       blink::WebURLLoader* loader,
102       double finishTime,
103       int64_t total_encoded_data_length);
104   virtual void didFail(
105       blink::WebURLLoader* loader,
106       const blink::WebURLError&);
107
108   void DidBecomeReady(Status status);
109
110   // Injected WebURLLoader instance for testing purposes.
111   scoped_ptr<blink::WebURLLoader> test_loader_;
112
113   // Keeps track of an active WebURLLoader and associated state.
114   scoped_ptr<ActiveLoader> active_loader_;
115
116   bool loader_failed_;
117   GURL url_;
118   blink::WebMediaPlayer::CORSMode cors_mode_;
119   bool single_origin_;
120
121   ReadyCB ready_cb_;
122   base::TimeTicks start_time_;
123
124   DISALLOW_COPY_AND_ASSIGN(MediaInfoLoader);
125 };
126
127 }  // namespace content
128
129 #endif  // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_