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_
10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
14 namespace dom_distiller {
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.
23 virtual ~Delegate() {}
24 virtual void OnLoadURLDone() {}
25 virtual void OnExecuteJavaScriptDone(const GURL& page_url,
26 const base::Value* value) {}
29 explicit DistillerPage(Delegate* delegate);
31 virtual ~DistillerPage();
34 // Initializes a |DistillerPage|. It must be called before any
35 // other functions, and must only be called once.
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();
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);
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);
56 // No context has yet been set in which to load or distill a page.
58 // The page distiller has been initialized and is idle.
60 // A page is currently loading.
62 // A page has loaded within the specified context.
64 // There was an error processing the page.
66 // JavaScript is executing within the context of the page. When the
67 // JavaScript completes, the state will be returned to |PAGE_AVAILABLE|.
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;
75 // Called by |LoadURL| to carry out platform-specific instructions to load a
77 virtual void LoadURLImpl(const GURL& gurl) = 0;
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;
85 // The current state of the |DistillerPage|, initially |NO_CONTEXT|.
90 DISALLOW_COPY_AND_ASSIGN(DistillerPage);
93 // Factory for generating a |DistillerPage|.
94 class DistillerPageFactory {
96 virtual ~DistillerPageFactory();
98 virtual scoped_ptr<DistillerPage> CreateDistillerPage(
99 DistillerPage::Delegate* delegate) const = 0;
102 } // namespace dom_distiller
104 #endif // COMPONENTS_DOM_DISTILLER_CORE_DISTILLER_PAGE_H_