1 // Copyright (c) 2012 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 #include "chrome/browser/ui/panels/panel_host.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/browser/chrome_notification_types.h"
11 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
12 #include "chrome/browser/extensions/window_controller.h"
13 #include "chrome/browser/favicon/favicon_helper.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/sessions/session_tab_helper.h"
16 #include "chrome/browser/ui/browser_navigator.h"
17 #include "chrome/browser/ui/panels/panel.h"
18 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
19 #include "components/favicon/content/content_favicon_driver.h"
20 #include "components/ui/zoom/page_zoom.h"
21 #include "components/ui/zoom/zoom_controller.h"
22 #include "content/public/browser/invalidate_type.h"
23 #include "content/public/browser/navigation_controller.h"
24 #include "content/public/browser/notification_service.h"
25 #include "content/public/browser/notification_source.h"
26 #include "content/public/browser/notification_types.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/site_instance.h"
29 #include "content/public/browser/user_metrics.h"
30 #include "content/public/browser/web_contents.h"
31 #include "extensions/browser/view_type_utils.h"
32 #include "extensions/common/extension_messages.h"
33 #include "ipc/ipc_message.h"
34 #include "ipc/ipc_message_macros.h"
35 #include "ui/gfx/geometry/rect.h"
36 #include "ui/gfx/image/image.h"
38 using base::UserMetricsAction
;
40 PanelHost::PanelHost(Panel
* panel
, Profile
* profile
)
43 extension_function_dispatcher_(profile
, this),
47 PanelHost::~PanelHost() {
50 void PanelHost::Init(const GURL
& url
) {
54 content::WebContents::CreateParams
create_params(
55 profile_
, content::SiteInstance::CreateForURL(profile_
, url
));
56 web_contents_
.reset(content::WebContents::Create(create_params
));
57 extensions::SetViewType(web_contents_
.get(), extensions::VIEW_TYPE_PANEL
);
58 web_contents_
->SetDelegate(this);
59 // web_contents_ may be passed to PageZoom::Zoom(), so it needs
61 ui_zoom::ZoomController::CreateForWebContents(web_contents_
.get());
62 content::WebContentsObserver::Observe(web_contents_
.get());
64 // Needed to give the web contents a Tab ID. Extension APIs
65 // expect web contents to have a Tab ID.
66 SessionTabHelper::CreateForWebContents(web_contents_
.get());
67 SessionTabHelper::FromWebContents(web_contents_
.get())->SetWindowID(
68 panel_
->session_id());
70 favicon::CreateContentFaviconDriverForWebContents(web_contents_
.get());
71 PrefsTabHelper::CreateForWebContents(web_contents_
.get());
72 extensions::ChromeExtensionWebContentsObserver::CreateForWebContents(
75 web_contents_
->GetController().LoadURL(
76 url
, content::Referrer(), ui::PAGE_TRANSITION_LINK
, std::string());
79 void PanelHost::DestroyWebContents() {
80 // Cannot do a web_contents_.reset() because web_contents_.get() will
81 // still return the pointer when we CHECK in WebContentsDestroyed (or if
82 // we get called back in the middle of web contents destruction, which
83 // WebView might do when it detects the web contents is destroyed).
84 content::WebContents
* contents
= web_contents_
.release();
88 gfx::Image
PanelHost::GetPageIcon() const {
89 if (!web_contents_
.get())
92 favicon::FaviconDriver
* favicon_driver
=
93 favicon::ContentFaviconDriver::FromWebContents(web_contents_
.get());
94 CHECK(favicon_driver
);
95 return favicon_driver
->GetFavicon();
98 content::WebContents
* PanelHost::OpenURLFromTab(
99 content::WebContents
* source
,
100 const content::OpenURLParams
& params
) {
101 // These dispositions aren't really navigations.
102 if (params
.disposition
== SUPPRESS_OPEN
||
103 params
.disposition
== SAVE_TO_DISK
||
104 params
.disposition
== IGNORE_ACTION
)
107 // Only allow clicks on links.
108 if (params
.transition
!= ui::PAGE_TRANSITION_LINK
)
111 // Force all links to open in a new tab.
112 chrome::NavigateParams
navigate_params(profile_
,
115 switch (params
.disposition
) {
116 case NEW_BACKGROUND_TAB
:
119 navigate_params
.disposition
= params
.disposition
;
122 navigate_params
.disposition
= NEW_FOREGROUND_TAB
;
125 chrome::Navigate(&navigate_params
);
126 return navigate_params
.target_contents
;
129 void PanelHost::NavigationStateChanged(content::WebContents
* source
,
130 content::InvalidateTypes changed_flags
) {
131 // Only need to update the title if the title changed while not loading,
132 // because the title is also updated when loading state changes.
133 if ((changed_flags
& content::INVALIDATE_TYPE_TAB
) ||
134 ((changed_flags
& content::INVALIDATE_TYPE_TITLE
) &&
135 !source
->IsLoading()))
136 panel_
->UpdateTitleBar();
139 void PanelHost::AddNewContents(content::WebContents
* source
,
140 content::WebContents
* new_contents
,
141 WindowOpenDisposition disposition
,
142 const gfx::Rect
& initial_rect
,
145 chrome::NavigateParams
navigate_params(profile_
, new_contents
->GetURL(),
146 ui::PAGE_TRANSITION_LINK
);
147 navigate_params
.target_contents
= new_contents
;
149 // Force all links to open in a new tab, even if they were trying to open a
151 navigate_params
.disposition
=
152 disposition
== NEW_BACKGROUND_TAB
? disposition
: NEW_FOREGROUND_TAB
;
154 navigate_params
.window_bounds
= initial_rect
;
155 navigate_params
.user_gesture
= user_gesture
;
156 navigate_params
.extension_app_id
= panel_
->extension_id();
157 chrome::Navigate(&navigate_params
);
160 void PanelHost::ActivateContents(content::WebContents
* contents
) {
164 void PanelHost::DeactivateContents(content::WebContents
* contents
) {
165 panel_
->Deactivate();
168 void PanelHost::LoadingStateChanged(content::WebContents
* source
,
169 bool to_different_document
) {
170 bool is_loading
= source
->IsLoading() && to_different_document
;
171 panel_
->LoadingStateChanged(is_loading
);
174 void PanelHost::CloseContents(content::WebContents
* source
) {
178 void PanelHost::MoveContents(content::WebContents
* source
,
179 const gfx::Rect
& pos
) {
180 panel_
->SetBounds(pos
);
183 bool PanelHost::IsPopupOrPanel(const content::WebContents
* source
) const {
187 void PanelHost::ContentsZoomChange(bool zoom_in
) {
188 Zoom(zoom_in
? content::PAGE_ZOOM_IN
: content::PAGE_ZOOM_OUT
);
191 void PanelHost::HandleKeyboardEvent(
192 content::WebContents
* source
,
193 const content::NativeWebKeyboardEvent
& event
) {
194 return panel_
->HandleKeyboardEvent(event
);
197 void PanelHost::ResizeDueToAutoResize(content::WebContents
* web_contents
,
198 const gfx::Size
& new_size
) {
199 panel_
->OnContentsAutoResized(new_size
);
202 void PanelHost::RenderViewCreated(content::RenderViewHost
* render_view_host
) {
203 extensions::WindowController
* window
= GetExtensionWindowController();
204 render_view_host
->Send(new ExtensionMsg_UpdateBrowserWindowId(
205 render_view_host
->GetRoutingID(), window
->GetWindowId()));
208 void PanelHost::RenderProcessGone(base::TerminationStatus status
) {
209 CloseContents(web_contents_
.get());
212 void PanelHost::WebContentsDestroyed() {
213 // Web contents should only be destroyed by us.
214 CHECK(!web_contents_
.get());
216 // Close the panel after we return to the message loop (not immediately,
217 // otherwise, it may destroy this object before the stack has a chance
218 // to cleanly unwind.)
219 base::MessageLoop::current()->PostTask(
221 base::Bind(&PanelHost::ClosePanel
, weak_factory_
.GetWeakPtr()));
224 void PanelHost::ClosePanel() {
228 bool PanelHost::OnMessageReceived(const IPC::Message
& message
) {
230 IPC_BEGIN_MESSAGE_MAP(PanelHost
, message
)
231 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request
, OnRequest
)
232 IPC_MESSAGE_UNHANDLED(handled
= false)
233 IPC_END_MESSAGE_MAP()
237 void PanelHost::OnRequest(const ExtensionHostMsg_Request_Params
& params
) {
238 if (!web_contents_
.get())
241 extension_function_dispatcher_
.Dispatch(params
,
242 web_contents_
->GetRenderViewHost());
245 extensions::WindowController
* PanelHost::GetExtensionWindowController() const {
246 return panel_
->extension_window_controller();
249 content::WebContents
* PanelHost::GetAssociatedWebContents() const {
250 return web_contents_
.get();
253 void PanelHost::Reload() {
254 content::RecordAction(UserMetricsAction("Reload"));
255 web_contents_
->GetController().Reload(true);
258 void PanelHost::ReloadIgnoringCache() {
259 content::RecordAction(UserMetricsAction("ReloadIgnoringCache"));
260 web_contents_
->GetController().ReloadIgnoringCache(true);
263 void PanelHost::StopLoading() {
264 content::RecordAction(UserMetricsAction("Stop"));
265 web_contents_
->Stop();
268 void PanelHost::Zoom(content::PageZoom zoom
) {
269 ui_zoom::PageZoom::Zoom(web_contents_
.get(), zoom
);