Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / components / dom_distiller / core / distiller_page.h
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/memory/scoped_ptr.h"
11 #include "base/values.h"
12 #include "url/gurl.h"
13
14 namespace dom_distiller {
15
16 // Injects JavaScript into a page, and uses it to extract and return long-form
17 // content. The class can be reused to load and distill multiple pages,
18 // following the state transitions described along with the class's states.
19 class DistillerPage {
20  public:
21   class Delegate {
22    public:
23     virtual ~Delegate() {}
24     virtual void OnLoadURLDone() {}
25     virtual void OnExecuteJavaScriptDone(const GURL& page_url,
26                                          const base::Value* value) {}
27   };
28
29   explicit DistillerPage(Delegate* delegate);
30
31   virtual ~DistillerPage();
32
33
34   // Initializes a |DistillerPage|. It must be called before any
35   // other functions, and must only be called once.
36   void Init();
37
38   // Loads a URL. |OnLoadURLDone| is called when the load completes or fails.
39   // May be called when the distiller is idle or a page is available.
40   void LoadURL(const GURL& url);
41   virtual void OnLoadURLDone();
42   virtual void OnLoadURLFailed();
43
44   // Injects and executes JavaScript in the context of a loaded page. |LoadURL|
45   // must complete before this function is called. May be called only when
46   // a page is available.
47   void ExecuteJavaScript(const std::string& script);
48
49   // Called when the JavaScript execution completes. |page_url| is the url of
50   // the distilled page. |value| contains data returned by the script.
51   virtual void OnExecuteJavaScriptDone(const GURL& page_url,
52                                        const base::Value* value);
53
54  protected:
55   enum State {
56     // No context has yet been set in which to load or distill a page.
57     NO_CONTEXT,
58     // The page distiller has been initialized and is idle.
59     IDLE,
60     // A page is currently loading.
61     LOADING_PAGE,
62     // A page has loaded within the specified context.
63     PAGE_AVAILABLE,
64     // There was an error processing the page.
65     PAGELOAD_FAILED,
66     // JavaScript is executing within the context of the page. When the
67     // JavaScript completes, the state will be returned to |PAGE_AVAILABLE|.
68     EXECUTING_JAVASCRIPT
69   };
70
71   // Called by |Init| to do plaform-specific initialization work set up an
72   // environment in which a page can be loaded.
73   virtual void InitImpl() = 0;
74
75   // Called by |LoadURL| to carry out platform-specific instructions to load a
76   // page.
77   virtual void LoadURLImpl(const GURL& gurl) = 0;
78
79   // Called by |ExecuteJavaScript| to carry out platform-specific instructions
80   // to inject and execute JavaScript within the context of the loaded page.
81   virtual void ExecuteJavaScriptImpl(const std::string& script) = 0;
82
83
84
85   // The current state of the |DistillerPage|, initially |NO_CONTEXT|.
86   State state_;
87
88  private:
89   Delegate* delegate_;
90   DISALLOW_COPY_AND_ASSIGN(DistillerPage);
91 };
92
93 // Factory for generating a |DistillerPage|.
94 class DistillerPageFactory {
95  public:
96   virtual ~DistillerPageFactory();
97
98   virtual scoped_ptr<DistillerPage> CreateDistillerPage(
99       DistillerPage::Delegate* delegate) const = 0;
100 };
101
102 }  // namespace dom_distiller
103
104 #endif  // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_