Home | History | Annotate | Download | only in android
      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 "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"
     18 #include "url/gurl.h"
     19 
     20 namespace blink {
     21 class WebFrame;
     22 class WebURLLoader;
     23 class WebURLRequest;
     24 }
     25 
     26 namespace content {
     27 
     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 {
     32  public:
     33   // Status codes for start operations on MediaInfoLoader.
     34   enum Status {
     35     // The operation failed, which may have been due to:
     36     //   - Page navigation
     37     //   - Server replied 4xx/5xx
     38     //   - The response was invalid
     39     //   - Connection was terminated
     40     //
     41     // At this point you should delete the loader.
     42     kFailed,
     43 
     44     // Everything went as planned.
     45     kOk,
     46   };
     47 
     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;
     53 
     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.
     58   MediaInfoLoader(
     59       const GURL& url,
     60       blink::WebMediaPlayer::CORSMode cors_mode,
     61       const ReadyCB& ready_cb);
     62   virtual ~MediaInfoLoader();
     63 
     64   // Start loading media info.
     65   void Start(blink::WebFrame* frame);
     66 
     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;
     70 
     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;
     74 
     75  private:
     76   friend class MediaInfoLoaderTest;
     77 
     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,
     92       int data_length,
     93       int encodedDataLength);
     94   virtual void didReceiveData(
     95       blink::WebURLLoader* loader,
     96       const char* data,
     97       int data_length,
     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,
    104       double finishTime,
    105       int64_t total_encoded_data_length);
    106   virtual void didFail(
    107       blink::WebURLLoader* loader,
    108       const blink::WebURLError&);
    109 
    110   void DidBecomeReady(Status status);
    111 
    112   // Injected WebURLLoader instance for testing purposes.
    113   scoped_ptr<blink::WebURLLoader> test_loader_;
    114 
    115   // Keeps track of an active WebURLLoader and associated state.
    116   scoped_ptr<media::ActiveLoader> active_loader_;
    117 
    118   bool loader_failed_;
    119   GURL url_;
    120   GURL first_party_url_;
    121   bool allow_stored_credentials_;
    122   blink::WebMediaPlayer::CORSMode cors_mode_;
    123   bool single_origin_;
    124 
    125   ReadyCB ready_cb_;
    126   base::TimeTicks start_time_;
    127 
    128   DISALLOW_COPY_AND_ASSIGN(MediaInfoLoader);
    129 };
    130 
    131 }  // namespace content
    132 
    133 #endif  // CONTENT_RENDERER_MEDIA_ANDROID_MEDIA_INFO_LOADER_H_
    134