File: distiller_page_web_contents.h

package info (click to toggle)
chromium 141.0.7390.107-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,246,132 kB
  • sloc: cpp: 35,264,965; ansic: 7,169,920; javascript: 4,250,185; python: 1,460,635; asm: 950,788; xml: 751,751; pascal: 187,972; sh: 89,459; perl: 88,691; objc: 79,953; sql: 53,924; cs: 44,622; fortran: 24,137; makefile: 22,313; tcl: 15,277; php: 14,018; yacc: 8,995; ruby: 7,553; awk: 3,720; lisp: 3,096; lex: 1,330; ada: 727; jsp: 228; sed: 36
file content (135 lines) | stat: -rw-r--r-- 4,995 bytes parent folder | download | duplicates (4)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_DISTILLER_PAGE_WEB_CONTENTS_H_
#define COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_DISTILLER_PAGE_WEB_CONTENTS_H_

#include <memory>
#include <string>

#include "base/memory/raw_ptr.h"
#include "base/supports_user_data.h"
#include "components/dom_distiller/core/distiller_page.h"
#include "components/dom_distiller/core/dom_distiller_constants.h"
#include "content/public/browser/navigation_throttle_registry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_observer.h"
#include "url/gurl.h"

namespace dom_distiller {

class SourcePageHandleWebContents : public SourcePageHandle {
 public:
  SourcePageHandleWebContents(content::WebContents* web_contents, bool owned);
  ~SourcePageHandleWebContents() override;

  // Retreives the WebContents. The SourcePageHandleWebContents keeps ownership.
  content::WebContents* web_contents() { return web_contents_; }

 private:
  // The WebContents this class holds.
  raw_ptr<content::WebContents, DanglingUntriaged> web_contents_;
  // Whether this owns |web_contents_|.
  bool owned_;
};

class DistillerPageWebContentsFactory : public DistillerPageFactory {
 public:
  explicit DistillerPageWebContentsFactory(
      content::BrowserContext* browser_context)
      : browser_context_(browser_context) {}
  ~DistillerPageWebContentsFactory() override = default;

  std::unique_ptr<DistillerPage> CreateDistillerPage(
      const gfx::Size& render_view_size) const override;
  std::unique_ptr<DistillerPage> CreateDistillerPageWithHandle(
      std::unique_ptr<SourcePageHandle> handle) const override;

 private:
  raw_ptr<content::BrowserContext> browser_context_;
};

class DistillerPageWebContents : public DistillerPage,
                                 public content::WebContentsDelegate,
                                 public content::WebContentsObserver {
 public:
  // Possibly create and add a NavigationThrottle for the given web contents.
  static void MaybeCreateAndAddNavigationThrottle(
      content::NavigationThrottleRegistry& registry);

  DistillerPageWebContents(content::BrowserContext* browser_context,
                           const gfx::Size& render_view_size,
                           std::unique_ptr<SourcePageHandleWebContents>
                               optional_web_contents_handle);
  ~DistillerPageWebContents() override;

  // DistillerPage implementation.
  bool ShouldFetchOfflineData() override;
  DistillerType GetDistillerType() override;

  // content::WebContentsDelegate implementation.
  gfx::Size GetSizeForNewRenderView(
      content::WebContents* web_contents) override;

  // content::WebContentsObserver implementation.
  void DOMContentLoaded(content::RenderFrameHost* render_frame_host) override;
  void DidFinishNavigation(
      content::NavigationHandle* navigation_handle) override;

  DistillerPageWebContents(const DistillerPageWebContents&) = delete;
  DistillerPageWebContents& operator=(const DistillerPageWebContents&) = delete;

 protected:
  void DistillPageImpl(const GURL& url, const std::string& script) override;

 private:
  friend class TestDistillerPageWebContents;

  enum State {
    // The page distiller is idle.
    IDLE,
    // A page is currently loading.
    LOADING_PAGE,
    // There was an error processing the page.
    PAGELOAD_FAILED,
    // JavaScript is executing within the context of the page. When the
    // JavaScript completes, the state will be returned to |IDLE|.
    EXECUTING_JAVASCRIPT
  };

  // Creates a new WebContents, adds |this| as an observer, and loads the
  // |url|.
  virtual void CreateNewWebContents(const GURL& url);

  // Injects and executes JavaScript in the context of a loaded page. This
  // must only be called after the page has successfully loaded.
  void ExecuteJavaScript();

  // Called when the distillation is done or if the page load failed.
  void OnWebContentsDistillationDone(const GURL& page_url,
                                     const base::TimeTicks& javascript_start,
                                     base::Value value);

  content::RenderFrameHost& TargetRenderFrameHost();

  // Returns the UserData associated with the underlying WebContents.
  base::SupportsUserData::Data* GetUserDataForTesting();

  // The current state of the |DistillerPage|, initially |IDLE|.
  State state_;

  // The JavaScript to inject to extract content.
  std::string script_;

  std::unique_ptr<SourcePageHandleWebContents> source_page_handle_;

  raw_ptr<content::BrowserContext> browser_context_;
  gfx::Size render_view_size_;
  base::WeakPtrFactory<DistillerPageWebContents> weak_factory_{this};
};

}  // namespace dom_distiller

#endif  // COMPONENTS_DOM_DISTILLER_CONTENT_BROWSER_DISTILLER_PAGE_WEB_CONTENTS_H_