1// Copyright (c) 2011 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 "webkit/glue/media/web_data_source_factory.h"
6
7#include "base/logging.h"
8
9namespace webkit_glue {
10
11class WebDataSourceFactory::BuildRequest
12    : public media::AsyncDataSourceFactoryBase::BuildRequest {
13 public:
14  BuildRequest(const std::string& url, BuildCallback* callback,
15               WebDataSource* data_source,
16               WebDataSourceBuildObserverHack* build_observer);
17  virtual ~BuildRequest();
18
19 protected:
20  // AsyncDataSourceFactoryBase::BuildRequest method.
21  virtual void DoStart();
22
23 private:
24  void InitDone(media::PipelineStatus status);
25
26  scoped_refptr<WebDataSource> data_source_;
27  WebDataSourceBuildObserverHack* build_observer_;
28
29  DISALLOW_COPY_AND_ASSIGN(BuildRequest);
30};
31
32WebDataSourceFactory::WebDataSourceFactory(
33    MessageLoop* render_loop,
34    WebKit::WebFrame* frame,
35    FactoryFunction factory_function,
36    WebDataSourceBuildObserverHack* build_observer)
37    : render_loop_(render_loop),
38      frame_(frame),
39      factory_function_(factory_function),
40      build_observer_(build_observer) {
41  DCHECK(render_loop_);
42  DCHECK(frame_);
43  DCHECK(factory_function_);
44}
45
46WebDataSourceFactory::~WebDataSourceFactory() {}
47
48media::DataSourceFactory* WebDataSourceFactory::Clone() const {
49  return new WebDataSourceFactory(render_loop_, frame_, factory_function_,
50                                  build_observer_);
51}
52
53bool WebDataSourceFactory::AllowRequests() const {
54  return true;
55}
56
57media::AsyncDataSourceFactoryBase::BuildRequest*
58WebDataSourceFactory::CreateRequest(const std::string& url,
59                                    BuildCallback* callback) {
60  WebDataSource* data_source = factory_function_(render_loop_, frame_);
61
62  return new WebDataSourceFactory::BuildRequest(url, callback, data_source,
63                                                build_observer_);
64}
65
66WebDataSourceFactory::BuildRequest::BuildRequest(
67    const std::string& url,
68    BuildCallback* callback,
69    WebDataSource* data_source,
70    WebDataSourceBuildObserverHack* build_observer)
71    : AsyncDataSourceFactoryBase::BuildRequest(url, callback),
72      data_source_(data_source),
73      build_observer_(build_observer) {
74}
75
76WebDataSourceFactory::BuildRequest::~BuildRequest() {
77  if (data_source_.get()) {
78    data_source_->CancelInitialize();
79    data_source_ = NULL;
80  }
81}
82
83void WebDataSourceFactory::BuildRequest::DoStart() {
84  data_source_->Initialize(url(), NewCallback(this, &BuildRequest::InitDone));
85}
86
87void WebDataSourceFactory::BuildRequest::InitDone(
88    media::PipelineStatus status) {
89  scoped_refptr<WebDataSource> data_source;
90
91  data_source = (status == media::PIPELINE_OK) ? data_source_ : NULL;
92  data_source_ = NULL;
93
94  if (build_observer_ && data_source.get()) {
95    build_observer_->Run(data_source.get());
96  }
97
98  RequestComplete(status, data_source);
99  // Don't do anything after this line. This object is deleted by
100  // RequestComplete().
101}
102
103}  // namespace webkit_glue
104