summaryrefslogtreecommitdiff
path: root/chromium/components/dom_distiller/core/distiller.h
blob: adbd41b8cbb294a99f14f15740e6b40c839d767e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_
#define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_

#include <map>
#include <string>

#include "base/callback.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/values.h"
#include "components/dom_distiller/core/distiller_page.h"
#include "components/dom_distiller/core/distiller_url_fetcher.h"
#include "components/dom_distiller/core/proto/distilled_page.pb.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"

namespace dom_distiller {

class DistillerImpl;

class Distiller {
 public:
  typedef base::Callback<void(
      scoped_ptr<DistilledPageProto>)> DistillerCallback;
  virtual ~Distiller() {}

  // Distills a page, and asynchrounously returns the article HTML to the
  // supplied callback.
  virtual void DistillPage(const GURL& url,
                           const DistillerCallback& callback) = 0;
};

class DistillerFactory {
 public:
  virtual scoped_ptr<Distiller> CreateDistiller() = 0;
  virtual ~DistillerFactory() {}
};

// Factory for creating a Distiller.
class DistillerFactoryImpl : public DistillerFactory {
 public:
  DistillerFactoryImpl(
      scoped_ptr<DistillerPageFactory> distiller_page_factory,
      scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory);
  virtual ~DistillerFactoryImpl();
  virtual scoped_ptr<Distiller> CreateDistiller() OVERRIDE;

 private:
  scoped_ptr<DistillerPageFactory> distiller_page_factory_;
  scoped_ptr<DistillerURLFetcherFactory> distiller_url_fetcher_factory_;
};

// Distills a article from a page and associated pages.
class DistillerImpl : public Distiller,
                      public DistillerPage::Delegate {
 public:
  DistillerImpl(
      const DistillerPageFactory& distiller_page_factory,
      const DistillerURLFetcherFactory& distiller_url_fetcher_factory);
  virtual ~DistillerImpl();

  // Creates an execution context. This must be called once before any calls are
  // made to distill the page.
  virtual void Init();

  virtual void DistillPage(const GURL& url,
                           const DistillerCallback& callback) OVERRIDE;

  // PageDistillerContext::Delegate
  virtual void OnLoadURLDone() OVERRIDE;
  virtual void OnExecuteJavaScriptDone(const base::Value* value) OVERRIDE;

  void OnFetchImageDone(const std::string& id, const std::string& response);

 private:
  virtual void LoadURL(const GURL& url);
  virtual void FetchImage(const std::string& image_id, const std::string& item);

  // Injects JavaScript to distill a loaded page down to its important content,
  // e.g., extracting a news article from its surrounding boilerplate.
  void GetDistilledContent();

  const DistillerPageFactory& distiller_page_factory_;
  const DistillerURLFetcherFactory& distiller_url_fetcher_factory_;
  scoped_ptr<DistillerPage> distiller_page_;
  DistillerCallback distillation_cb_;

  std::map<std::string, DistillerURLFetcher* > image_fetchers_;

  scoped_ptr<DistilledPageProto> proto_;

  DISALLOW_COPY_AND_ASSIGN(DistillerImpl);
};

}  // namespace dom_distiller

#endif  // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_H_