1 // Copyright 2015 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 IOS_COMPONENTS_SECURITY_INTERSTITIALS_IOS_SECURITY_INTERSTITIAL_PAGE_H_
6 #define IOS_COMPONENTS_SECURITY_INTERSTITIALS_IOS_SECURITY_INTERSTITIAL_PAGE_H_
7 
8 #include <string>
9 
10 #include "base/macros.h"
11 #include "base/strings/string16.h"
12 #include "ios/components/security_interstitials/ios_blocking_page_controller_client.h"
13 #include "ios/web/public/security/web_interstitial_delegate.h"
14 #include "url/gurl.h"
15 
16 namespace base {
17 class DictionaryValue;
18 }
19 
20 namespace web {
21 class WebFrame;
22 class WebInterstitial;
23 class WebState;
24 }  // namespace web
25 
26 namespace security_interstitials {
27 
28 class IOSSecurityInterstitialPage : public web::WebInterstitialDelegate {
29  public:
30   IOSSecurityInterstitialPage(web::WebState* web_state,
31                               const GURL& request_url,
32                               IOSBlockingPageControllerClient* client);
33   ~IOSSecurityInterstitialPage() override;
34 
35   // Creates an interstitial and shows it.
36   void Show();
37 
38   // web::WebInterstitialDelegate implementation.
39   std::string GetHtmlContents() const override;
40 
41   // Whether a URL should be displayed on this interstitial page. This is
42   // respected by committed interstitials only.
43   virtual bool ShouldDisplayURL() const;
44 
45   // Handles JS commands from the interstitial page. Overridden in subclasses
46   // to handle actions specific to the type of interstitial.
47   virtual void HandleScriptCommand(const base::DictionaryValue& message,
48                                    const GURL& origin_url,
49                                    bool user_is_interacting,
50                                    web::WebFrame* sender_frame) = 0;
51 
52  protected:
53   // Returns true if the interstitial should create a new navigation item.
54   virtual bool ShouldCreateNewNavigation() const = 0;
55 
56   // Populates the strings used to generate the HTML from the template.
57   virtual void PopulateInterstitialStrings(
58       base::DictionaryValue* load_time_data) const = 0;
59 
60   // Gives an opportunity for child classes to react to Show() having run. The
61   // |web_interstitial_| will now have a value.
62   virtual void AfterShow() = 0;
63 
64   // Returns the formatted host name for the request url.
65   base::string16 GetFormattedHostName() const;
66 
web_state()67   web::WebState* web_state() const { return web_state_; }
request_url()68   const GURL& request_url() const { return request_url_; }
web_interstitial()69   web::WebInterstitial* web_interstitial() const { return web_interstitial_; }
70 
71  private:
72   // The WebState with which this interstitial page is associated. Not
73   // available in the destructor since the it can be destroyed before this
74   // class is destroyed.
75   web::WebState* web_state_;
76   const GURL request_url_;
77 
78   // Once non-null, the |web_interstitial_| takes ownership of this
79   // IOSSecurityInterstitialPage instance.
80   web::WebInterstitial* web_interstitial_;
81 
82   // Used to interact with the embedder. Unowned pointer; must outlive |this|
83   // instance.
84   IOSBlockingPageControllerClient* const client_ = nullptr;
85 
86   DISALLOW_COPY_AND_ASSIGN(IOSSecurityInterstitialPage);
87 };
88 
89 }  // namespace security_interstitials
90 
91 #endif  // IOS_COMPONENTS_SECURITY_INTERSTITIALS_IOS_SECURITY_INTERSTITIAL_PAGE_H_
92