Add abhijeet.k@samsung.com to AUTHORS list.
[chromium-blink-merge.git] / components / dom_distiller / core / distiller_page.h
blobe80bc59535a5ed2b950a322b69709aea6f503010
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.
5 #ifndef COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
6 #define COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_
8 #include <string>
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/geometry/size.h"
16 #include "url/gurl.h"
18 namespace dom_distiller {
20 class SourcePageHandle {
21 public:
22 virtual ~SourcePageHandle() {}
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.
30 class DistillerPage {
31 public:
32 typedef base::Callback<
33 void(scoped_ptr<proto::DomDistillerResult> distilled_page,
34 bool distillation_successful)> DistillerPageCallback;
36 DistillerPage();
37 virtual ~DistillerPage();
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);
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);
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;
58 // The value returned between the JavaScript and the DistillerPage can be
59 // either a dictionary with all the content, or a stringified version.
60 virtual bool StringifyOutput() = 0;
62 // If true, forces the creation of a new window context to evaluate the
63 // JavaScript.
64 virtual bool CreateNewContext() = 0;
66 private:
67 bool ready_;
68 DistillerPageCallback distiller_page_callback_;
69 DISALLOW_COPY_AND_ASSIGN(DistillerPage);
72 // Factory for generating a |DistillerPage|.
73 class DistillerPageFactory {
74 public:
75 virtual ~DistillerPageFactory();
77 // Constructs and returns a new DistillerPage. The implementation of this
78 // should be very cheap, since the pages can be thrown away without being
79 // used.
80 virtual scoped_ptr<DistillerPage> CreateDistillerPage(
81 const gfx::Size& render_view_size) const = 0;
82 virtual scoped_ptr<DistillerPage> CreateDistillerPageWithHandle(
83 scoped_ptr<SourcePageHandle> handle) const = 0;
86 } // namespace dom_distiller
88 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_