1 // Copyright 2014 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 "apps/shell/browser/shell_app_window.h" 6 7 #include "apps/shell/browser/shell_extension_web_contents_observer.h" 8 #include "content/public/browser/navigation_controller.h" 9 #include "content/public/browser/render_view_host.h" 10 #include "content/public/browser/web_contents.h" 11 #include "extensions/browser/view_type_utils.h" 12 #include "extensions/common/extension_messages.h" 13 #include "ipc/ipc_message_macros.h" 14 15 using content::BrowserContext; 16 using content::WebContents; 17 18 namespace apps { 19 ShellAppWindow()20ShellAppWindow::ShellAppWindow() {} 21 ~ShellAppWindow()22ShellAppWindow::~ShellAppWindow() {} 23 Init(BrowserContext * context,gfx::Size initial_size)24void ShellAppWindow::Init(BrowserContext* context, gfx::Size initial_size) { 25 extension_function_dispatcher_.reset( 26 new extensions::ExtensionFunctionDispatcher(context, this)); 27 28 // Create the web contents with an initial size to avoid a resize. 29 WebContents::CreateParams create_params(context); 30 create_params.initial_size = initial_size; 31 web_contents_.reset(WebContents::Create(create_params)); 32 33 content::WebContentsObserver::Observe(web_contents_.get()); 34 35 // Add support for extension startup. 36 extensions::ShellExtensionWebContentsObserver::CreateForWebContents( 37 web_contents_.get()); 38 39 extensions::SetViewType(web_contents_.get(), 40 extensions::VIEW_TYPE_APP_WINDOW); 41 } 42 LoadURL(const GURL & url)43void ShellAppWindow::LoadURL(const GURL& url) { 44 content::NavigationController::LoadURLParams params(url); 45 web_contents_->GetController().LoadURLWithParams(params); 46 web_contents_->Focus(); 47 } 48 GetNativeWindow()49aura::Window* ShellAppWindow::GetNativeWindow() { 50 return web_contents_->GetNativeView(); 51 } 52 GetRenderViewRoutingID()53int ShellAppWindow::GetRenderViewRoutingID() { 54 return web_contents_->GetRenderViewHost()->GetRoutingID(); 55 } 56 OnMessageReceived(const IPC::Message & message)57bool ShellAppWindow::OnMessageReceived(const IPC::Message& message) { 58 bool handled = true; 59 IPC_BEGIN_MESSAGE_MAP(ShellAppWindow, message) 60 IPC_MESSAGE_HANDLER(ExtensionHostMsg_Request, OnRequest) 61 IPC_MESSAGE_UNHANDLED(handled = false) 62 IPC_END_MESSAGE_MAP() 63 return handled; 64 } 65 GetAssociatedWebContents() const66content::WebContents* ShellAppWindow::GetAssociatedWebContents() const { 67 return web_contents_.get(); 68 } 69 OnRequest(const ExtensionHostMsg_Request_Params & params)70void ShellAppWindow::OnRequest(const ExtensionHostMsg_Request_Params& params) { 71 extension_function_dispatcher_->Dispatch(params, 72 web_contents_->GetRenderViewHost()); 73 } 74 75 } // namespace apps 76