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 COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
6#define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
7
8#include <string>
9
10#include "base/callback.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/memory/weak_ptr.h"
13#include "base/values.h"
14#include "third_party/dom_distiller_js/dom_distiller.pb.h"
15#include "ui/gfx/size.h"
16#include "url/gurl.h"
17
18namespace dom_distiller {
19
20class SourcePageHandle {
21 public:
22  virtual ~SourcePageHandle() {}
23};
24
25// Injects JavaScript into a page, and uses it to extract and return long-form
26// content. The class can be reused to load and distill multiple pages,
27// following the state transitions described along with the class's states.
28// Constructing a DistillerPage should be cheap, as some of the instances can be
29// thrown away without ever being used.
30class DistillerPage {
31 public:
32  typedef base::Callback<
33      void(scoped_ptr<proto::DomDistillerResult> distilled_page,
34           bool distillation_successful)> DistillerPageCallback;
35
36  DistillerPage();
37  virtual ~DistillerPage();
38
39  // Loads a URL. |OnDistillationDone| is called when the load completes or
40  // fails. May be called when the distiller is idle. Callers can assume that,
41  // for a given |url| and |options|, any DistillerPage implementation will
42  // extract the same content.
43  void DistillPage(const GURL& url,
44                   const proto::DomDistillerOptions options,
45                   const DistillerPageCallback& callback);
46
47  // Called when the JavaScript execution completes. |page_url| is the url of
48  // the distilled page. |value| contains data returned by the script.
49  virtual void OnDistillationDone(const GURL& page_url,
50                                  const base::Value* value);
51
52 protected:
53  // Called by |DistillPage| to carry out platform-specific instructions to load
54  // and distill the |url| using the provided |script|. The extracted content
55  // should be the same regardless of the DistillerPage implementation.
56  virtual void DistillPageImpl(const GURL& url, const std::string& script) = 0;
57
58 private:
59  bool ready_;
60  DistillerPageCallback distiller_page_callback_;
61  DISALLOW_COPY_AND_ASSIGN(DistillerPage);
62};
63
64// Factory for generating a |DistillerPage|.
65class DistillerPageFactory {
66 public:
67  virtual ~DistillerPageFactory();
68
69  // Constructs and returns a new DistillerPage. The implementation of this
70  // should be very cheap, since the pages can be thrown away without being
71  // used.
72  virtual scoped_ptr<DistillerPage> CreateDistillerPage(
73      const gfx::Size& render_view_size) const = 0;
74  virtual scoped_ptr<DistillerPage> CreateDistillerPageWithHandle(
75      scoped_ptr<SourcePageHandle> handle) const = 0;
76};
77
78}  // namespace dom_distiller
79
80#endif  // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
81