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. 4 5 #include "chrome/browser/ui/panels/panel_host.h" 6 7 #include "base/bind.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/chrome_page_zoom.h" 12 #include "chrome/browser/extensions/window_controller.h" 13 #include "chrome/browser/favicon/favicon_tab_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 "chrome/common/extensions/extension_messages.h" 20 #include "content/public/browser/invalidate_type.h" 21 #include "content/public/browser/navigation_controller.h" 22 #include "content/public/browser/notification_service.h" 23 #include "content/public/browser/notification_source.h" 24 #include "content/public/browser/notification_types.h" 25 #include "content/public/browser/render_view_host.h" 26 #include "content/public/browser/site_instance.h" 27 #include "content/public/browser/user_metrics.h" 28 #include "content/public/browser/web_contents.h" 29 #include "extensions/browser/view_type_utils.h" 30 #include "ipc/ipc_message.h" 31 #include "ipc/ipc_message_macros.h" 32 #include "ui/gfx/image/image.h" 33 #include "ui/gfx/rect.h" 34 35 using content::UserMetricsAction; 36 37 PanelHost::PanelHost(Panel* panel, Profile* profile) 38 : panel_(panel), 39 profile_(profile), 40 extension_function_dispatcher_(profile, this), 41 weak_factory_(this) { 42 } 43 44 PanelHost::~PanelHost() { 45 } 46 47 void PanelHost::Init(const GURL& url) { 48 if (url.is_empty()) 49 return; 50 51 content::WebContents::CreateParams create_params( 52 profile_, content::SiteInstance::CreateForURL(profile_, url)); 53 web_contents_.reset(content::WebContents::Create(create_params)); 54 extensions::SetViewType(web_contents_.get(), extensions::VIEW_TYPE_PANEL); 55 web_contents_->SetDelegate(this); 56 content::WebContentsObserver::Observe(web_contents_.get()); 57 58 // Needed to give the web contents a Tab ID. Extension APIs 59 // expect web contents to have a Tab ID. 60 SessionTabHelper::CreateForWebContents(web_contents_.get()); 61 SessionTabHelper::FromWebContents(web_contents_.get())->SetWindowID( 62 panel_->session_id()); 63 64 FaviconTabHelper::CreateForWebContents(web_contents_.get()); 65 PrefsTabHelper::CreateForWebContents(web_contents_.get()); 66 67 web_contents_->GetController().LoadURL( 68 url, content::Referrer(), content::PAGE_TRANSITION_LINK, std::string()); 69 } 70 71 void PanelHost::DestroyWebContents() { 72 // Cannot do a web_contents_.reset() because web_contents_.get() will 73 // still return the pointer when we CHECK in WebContentsDestroyed (or if 74 // we get called back in the middle of web contents destruction, which 75 // WebView might do when it detects the web contents is destroyed). 76 content::WebContents* contents = web_contents_.release(); 77 delete contents; 78 } 79 80 gfx::Image PanelHost::GetPageIcon() const { 81 if (!web_contents_.get()) 82 return gfx::Image(); 83 84 FaviconTabHelper* favicon_tab_helper = 85 FaviconTabHelper::FromWebContents(web_contents_.get()); 86 CHECK(favicon_tab_helper); 87 return favicon_tab_helper->GetFavicon(); 88 } 89 90 content::WebContents* PanelHost::OpenURLFromTab( 91 content::WebContents* source, 92 const content::OpenURLParams& params) { 93 // These dispositions aren't really navigations. 94 if (params.disposition == SUPPRESS_OPEN || 95 params.disposition == SAVE_TO_DISK || 96 params.disposition == IGNORE_ACTION) 97 return NULL; 98 99 // Only allow clicks on links. 100 if (params.transition != content::PAGE_TRANSITION_LINK) 101 return NULL; 102 103 // Force all links to open in a new tab. 104 chrome::NavigateParams navigate_params(profile_, 105 params.url, 106 params.transition); 107 switch (params.disposition) { 108 case NEW_BACKGROUND_TAB: 109 case NEW_WINDOW: 110 case OFF_THE_RECORD: 111 navigate_params.disposition = params.disposition; 112 break; 113 default: 114 navigate_params.disposition = NEW_FOREGROUND_TAB; 115 break; 116 } 117 chrome::Navigate(&navigate_params); 118 return navigate_params.target_contents; 119 } 120 121 void PanelHost::NavigationStateChanged(const content::WebContents* source, 122 unsigned changed_flags) { 123 // Only need to update the title if the title changed while not loading, 124 // because the title is also updated when loading state changes. 125 if ((changed_flags & content::INVALIDATE_TYPE_TAB) || 126 ((changed_flags & content::INVALIDATE_TYPE_TITLE) && 127 !source->IsLoading())) 128 panel_->UpdateTitleBar(); 129 } 130 131 void PanelHost::AddNewContents(content::WebContents* source, 132 content::WebContents* new_contents, 133 WindowOpenDisposition disposition, 134 const gfx::Rect& initial_pos, 135 bool user_gesture, 136 bool* was_blocked) { 137 chrome::NavigateParams navigate_params(profile_, new_contents->GetURL(), 138 content::PAGE_TRANSITION_LINK); 139 navigate_params.target_contents = new_contents; 140 141 // Force all links to open in a new tab, even if they were trying to open a 142 // window. 143 navigate_params.disposition = 144 disposition == NEW_BACKGROUND_TAB ? disposition : NEW_FOREGROUND_TAB; 145 146 navigate_params.window_bounds = initial_pos; 147 navigate_params.user_gesture = user_gesture; 148 navigate_params.extension_app_id = panel_->extension_id(); 149 chrome::Navigate(&navigate_params); 150 } 151 152 void PanelHost::ActivateContents(content::WebContents* contents) { 153 panel_->Activate(); 154 } 155 156 void PanelHost::DeactivateContents(content::WebContents* contents) { 157 panel_->Deactivate(); 158 } 159 160 void PanelHost::LoadingStateChanged(content::WebContents* source) { 161 bool is_loading = source->IsLoading(); 162 panel_->LoadingStateChanged(is_loading); 163 } 164 165 void PanelHost::CloseContents(content::WebContents* source) { 166 panel_->Close(); 167 } 168 169 void PanelHost::MoveContents(content::WebContents* source, 170 const gfx::Rect& pos) { 171 panel_->SetBounds(pos); 172 } 173 174 bool PanelHost::IsPopupOrPanel(const content::WebContents* source) const { 175 return true; 176 } 177 178 void PanelHost::ContentsZoomChange(bool zoom_in) { 179 Zoom(zoom_in ? content::PAGE_ZOOM_IN : content::PAGE_ZOOM_OUT); 180 } 181 182 void PanelHost::HandleKeyboardEvent( 183 content::WebContents* source, 184 const content::NativeWebKeyboardEvent& event) { 185 return panel_->HandleKeyboardEvent(event); 186 } 187 188 void PanelHost::WebContentsFocused(content::WebContents* contents) { 189 panel_->WebContentsFocused(contents); 190 } 191 192 void PanelHost::ResizeDueToAutoResize(content::WebContents* web_contents, 193 const gfx::Size& new_size) { 194 panel_->OnContentsAutoResized(new_size); 195 } 196 197 void PanelHost::RenderViewCreated(content::RenderViewHost* render_view_host) { 198 extensions::WindowController* window = GetExtensionWindowController(); 199 render_view_host->Send(new ExtensionMsg_UpdateBrowserWindowId( 200 render_view_host->GetRoutingID(), window->GetWindowId())); 201 } 202 203 void PanelHost::RenderProcessGone(base::TerminationStatus status) { 204 CloseContents(web_contents_.get()); 205 } 206 207 void PanelHost::WebContentsDestroyed(content::WebContents* web_contents) { 208 // Web contents should only be destroyed by us. 209 CHECK(!web_contents_.get()); 210 211 // Close the panel after we return to the message loop (not immediately, 212 // otherwise, it may destroy this object before the stack has a chance 213 // to cleanly unwind.) 214 base::MessageLoop::current()->PostTask( 215 FROM_HERE, 216 base::Bind(&PanelHost::ClosePanel, weak_factory_.GetWeakPtr())); 217 } 218 219 void PanelHost::ClosePanel() { 220 panel_->Close(); 221 } 222 223 bool PanelHost::OnMessageReceived(const IPC::Message& message) { 224 bool handled = true; 225 IPC_BEGIN_MESSAGE_MAP(PanelHost, message) 226 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) 227 IPC_MESSAGE_UNHANDLED(handled = false) 228 IPC_END_MESSAGE_MAP() 229 return handled; 230 } 231 232 void PanelHost::OnRequest(const ExtensionHostMsg_Request_Params& params) { 233 if (!web_contents_.get()) 234 return; 235 236 extension_function_dispatcher_.Dispatch(params, 237 web_contents_->GetRenderViewHost()); 238 } 239 240 extensions::WindowController* PanelHost::GetExtensionWindowController() const { 241 return panel_->extension_window_controller(); 242 } 243 244 content::WebContents* PanelHost::GetAssociatedWebContents() const { 245 return web_contents_.get(); 246 } 247 248 void PanelHost::Reload() { 249 content::RecordAction(UserMetricsAction("Reload")); 250 web_contents_->GetController().Reload(true); 251 } 252 253 void PanelHost::ReloadIgnoringCache() { 254 content::RecordAction(UserMetricsAction("ReloadIgnoringCache")); 255 web_contents_->GetController().ReloadIgnoringCache(true); 256 } 257 258 void PanelHost::StopLoading() { 259 content::RecordAction(UserMetricsAction("Stop")); 260 web_contents_->Stop(); 261 } 262 263 void PanelHost::Zoom(content::PageZoom zoom) { 264 chrome_page_zoom::Zoom(web_contents_.get(), zoom); 265 } 266