OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ | 5 #ifndef CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ |
6 #define CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ | 6 #define CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 // TODO(jam): remove this file when all files have been converted. |
10 #include <string> | 10 #include "content/browser/tab_contents/interstitial_page.h" |
11 | |
12 #include "base/process_util.h" | |
13 #include "base/scoped_ptr.h" | |
14 #include "chrome/browser/renderer_host/render_view_host_delegate.h" | |
15 #include "chrome/common/notification_observer.h" | |
16 #include "chrome/common/notification_registrar.h" | |
17 #include "chrome/common/renderer_preferences.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "ui/gfx/size.h" | |
20 | |
21 class NavigationEntry; | |
22 class TabContents; | |
23 class TabContentsView; | |
24 | |
25 // This class is a base class for interstitial pages, pages that show some | |
26 // informative message asking for user validation before reaching the target | |
27 // page. (Navigating to a page served over bad HTTPS or a page containing | |
28 // malware are typical cases where an interstitial is required.) | |
29 // | |
30 // If specified in its constructor, this class creates a navigation entry so | |
31 // that when the interstitial shows, the current entry is the target URL. | |
32 // | |
33 // InterstitialPage instances take care of deleting themselves when closed | |
34 // through a navigation, the TabContents closing them or the tab containing them | |
35 // being closed. | |
36 | |
37 enum ResourceRequestAction { | |
38 BLOCK, | |
39 RESUME, | |
40 CANCEL | |
41 }; | |
42 | |
43 class InterstitialPage : public NotificationObserver, | |
44 public RenderViewHostDelegate { | |
45 public: | |
46 // The different state of actions the user can take in an interstitial. | |
47 enum ActionState { | |
48 NO_ACTION, // No action has been taken yet. | |
49 PROCEED_ACTION, // "Proceed" was selected. | |
50 DONT_PROCEED_ACTION // "Don't proceed" was selected. | |
51 }; | |
52 | |
53 // Creates an interstitial page to show in |tab|. |new_navigation| should be | |
54 // set to true when the interstitial is caused by loading a new page, in which | |
55 // case a temporary navigation entry is created with the URL |url| and | |
56 // added to the navigation controller (so the interstitial page appears as a | |
57 // new navigation entry). |new_navigation| should be false when the | |
58 // interstitial was triggered by a loading a sub-resource in a page. | |
59 InterstitialPage(TabContents* tab, bool new_navigation, const GURL& url); | |
60 virtual ~InterstitialPage(); | |
61 | |
62 // Shows the interstitial page in the tab. | |
63 virtual void Show(); | |
64 | |
65 // Hides the interstitial page. Warning: this deletes the InterstitialPage. | |
66 void Hide(); | |
67 | |
68 // Retrieves the InterstitialPage if any associated with the specified | |
69 // |tab_contents| (used by ui tests). | |
70 static InterstitialPage* GetInterstitialPage(TabContents* tab_contents); | |
71 | |
72 // Sub-classes should return the HTML that should be displayed in the page. | |
73 virtual std::string GetHTMLContents(); | |
74 | |
75 // Reverts to the page showing before the interstitial. | |
76 // Sub-classes should call this method when the user has chosen NOT to proceed | |
77 // to the target URL. | |
78 // Warning: if |new_navigation| was set to true in the constructor, 'this' | |
79 // will be deleted when this method returns. | |
80 virtual void DontProceed(); | |
81 | |
82 // Sub-classes should call this method when the user has chosen to proceed to | |
83 // the target URL. | |
84 // Warning: 'this' has been deleted when this method returns. | |
85 virtual void Proceed(); | |
86 | |
87 // Allows the user to navigate away by disabling the interstitial, canceling | |
88 // the pending request, and unblocking the hidden renderer. The interstitial | |
89 // will stay visible until the navigation completes. | |
90 void CancelForNavigation(); | |
91 | |
92 // Sizes the RenderViewHost showing the actual interstitial page contents. | |
93 void SetSize(const gfx::Size& size); | |
94 | |
95 ActionState action_taken() const { return action_taken_; } | |
96 | |
97 // Sets the focus to the interstitial. | |
98 void Focus(); | |
99 | |
100 // Focus the first (last if reverse is true) element in the interstitial page. | |
101 // Called when tab traversing. | |
102 void FocusThroughTabTraversal(bool reverse); | |
103 | |
104 virtual ViewType::Type GetRenderViewType() const; | |
105 virtual int GetBrowserWindowID() const; | |
106 | |
107 // See description above field. | |
108 void set_reload_on_dont_proceed(bool value) { | |
109 reload_on_dont_proceed_ = value; | |
110 } | |
111 bool reload_on_dont_proceed() const { return reload_on_dont_proceed_; } | |
112 | |
113 virtual void UpdateInspectorSetting(const std::string& key, | |
114 const std::string& value); | |
115 virtual void ClearInspectorSettings(); | |
116 | |
117 protected: | |
118 // NotificationObserver method: | |
119 virtual void Observe(NotificationType type, | |
120 const NotificationSource& source, | |
121 const NotificationDetails& details); | |
122 | |
123 // RenderViewHostDelegate implementation: | |
124 virtual View* GetViewDelegate(); | |
125 virtual const GURL& GetURL() const; | |
126 virtual void RenderViewGone(RenderViewHost* render_view_host, | |
127 base::TerminationStatus status, | |
128 int error_code); | |
129 virtual void DidNavigate(RenderViewHost* render_view_host, | |
130 const ViewHostMsg_FrameNavigate_Params& params); | |
131 virtual void UpdateTitle(RenderViewHost* render_view_host, | |
132 int32 page_id, | |
133 const std::wstring& title); | |
134 virtual void DomOperationResponse(const std::string& json_string, | |
135 int automation_id); | |
136 virtual RendererPreferences GetRendererPrefs(Profile* profile) const; | |
137 | |
138 // Invoked when the page sent a command through DOMAutomation. | |
139 virtual void CommandReceived(const std::string& command) {} | |
140 | |
141 // Invoked with the NavigationEntry that is going to be added to the | |
142 // navigation controller. | |
143 // Gives an opportunity to sub-classes to set states on the |entry|. | |
144 // Note that this is only called if the InterstitialPage was constructed with | |
145 // |create_navigation_entry| set to true. | |
146 virtual void UpdateEntry(NavigationEntry* entry) {} | |
147 | |
148 TabContents* tab() const { return tab_; } | |
149 const GURL& url() const { return url_; } | |
150 RenderViewHost* render_view_host() const { return render_view_host_; } | |
151 | |
152 // Creates the RenderViewHost containing the interstitial content. | |
153 // Overriden in unit tests. | |
154 virtual RenderViewHost* CreateRenderViewHost(); | |
155 | |
156 // Creates the TabContentsView that shows the interstitial RVH. | |
157 // Overriden in unit tests. | |
158 virtual TabContentsView* CreateTabContentsView(); | |
159 | |
160 private: | |
161 // AutomationProvider needs access to Proceed and DontProceed to simulate | |
162 // user actions. | |
163 friend class AutomationProvider; | |
164 | |
165 class InterstitialPageRVHViewDelegate; | |
166 | |
167 // Initializes tab_to_interstitial_page_ in a thread-safe manner. | |
168 // Should be called before accessing tab_to_interstitial_page_. | |
169 static void InitInterstitialPageMap(); | |
170 | |
171 // Disable the interstitial: | |
172 // - if it is not yet showing, then it won't be shown. | |
173 // - any command sent by the RenderViewHost will be ignored. | |
174 void Disable(); | |
175 | |
176 // Executes the passed action on the ResourceDispatcher (on the IO thread). | |
177 // Used to block/resume/cancel requests for the RenderViewHost hidden by this | |
178 // interstitial. | |
179 void TakeActionOnResourceDispatcher(ResourceRequestAction action); | |
180 | |
181 // The tab in which we are displayed. | |
182 TabContents* tab_; | |
183 | |
184 // The URL that is shown when the interstitial is showing. | |
185 GURL url_; | |
186 | |
187 // Whether this interstitial is shown as a result of a new navigation (in | |
188 // which case a transient navigation entry is created). | |
189 bool new_navigation_; | |
190 | |
191 // Whether we should discard the pending navigation entry when not proceeding. | |
192 // This is to deal with cases where |new_navigation_| is true but a new | |
193 // pending entry was created since this interstitial was shown and we should | |
194 // not discard it. | |
195 bool should_discard_pending_nav_entry_; | |
196 | |
197 // If true and the user chooses not to proceed the target NavigationController | |
198 // is reloaded. This is used when two NavigationControllers are merged | |
199 // (CopyStateFromAndPrune). | |
200 // The default is false. | |
201 bool reload_on_dont_proceed_; | |
202 | |
203 // Whether this interstitial is enabled. See Disable() for more info. | |
204 bool enabled_; | |
205 | |
206 // Whether the Proceed or DontProceed methods have been called yet. | |
207 ActionState action_taken_; | |
208 | |
209 // Notification magic. | |
210 NotificationRegistrar notification_registrar_; | |
211 | |
212 // The RenderViewHost displaying the interstitial contents. | |
213 RenderViewHost* render_view_host_; | |
214 | |
215 // The IDs for the Render[View|Process]Host hidden by this interstitial. | |
216 int original_child_id_; | |
217 int original_rvh_id_; | |
218 | |
219 // Whether or not we should change the title of the tab when hidden (to revert | |
220 // it to its original value). | |
221 bool should_revert_tab_title_; | |
222 | |
223 // Whether the ResourceDispatcherHost has been notified to cancel/resume the | |
224 // resource requests blocked for the RenderViewHost. | |
225 bool resource_dispatcher_host_notified_; | |
226 | |
227 // The original title of the tab that should be reverted to when the | |
228 // interstitial is hidden. | |
229 std::wstring original_tab_title_; | |
230 | |
231 // Our RenderViewHostViewDelegate, necessary for accelerators to work. | |
232 scoped_ptr<InterstitialPageRVHViewDelegate> rvh_view_delegate_; | |
233 | |
234 // We keep a map of the various blocking pages shown as the UI tests need to | |
235 // be able to retrieve them. | |
236 typedef std::map<TabContents*, InterstitialPage*> InterstitialPageMap; | |
237 static InterstitialPageMap* tab_to_interstitial_page_; | |
238 | |
239 // Settings passed to the renderer. | |
240 RendererPreferences renderer_preferences_; | |
241 | |
242 DISALLOW_COPY_AND_ASSIGN(InterstitialPage); | |
243 }; | |
244 | 11 |
245 #endif // CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ | 12 #endif // CHROME_BROWSER_TAB_CONTENTS_INTERSTITIAL_PAGE_H_ |
OLD | NEW |