dom_view.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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/views/dom_view.h" 6 7#include "chrome/browser/tab_contents/tab_contents.h" 8#include "ipc/ipc_message.h" 9#include "views/focus/focus_manager.h" 10 11#if defined(TOUCH_UI) 12#include "chrome/browser/ui/views/tab_contents/tab_contents_view_views.h" 13#endif 14 15DOMView::DOMView() : tab_contents_(NULL), initialized_(false) { 16 SetFocusable(true); 17} 18 19DOMView::~DOMView() { 20 if (native_view()) 21 Detach(); 22} 23 24bool DOMView::Init(Profile* profile, SiteInstance* instance) { 25 if (initialized_) 26 return true; 27 28 initialized_ = true; 29 tab_contents_.reset(CreateTabContents(profile, instance)); 30 // Attach the native_view now if the view is already added to Widget. 31 if (GetWidget()) 32 AttachTabContents(); 33 return true; 34} 35 36TabContents* DOMView::CreateTabContents(Profile* profile, 37 SiteInstance* instance) { 38 return new TabContents(profile, instance, MSG_ROUTING_NONE, NULL, NULL); 39} 40 41void DOMView::LoadURL(const GURL& url) { 42 DCHECK(initialized_); 43 tab_contents_->controller().LoadURL(url, GURL(), PageTransition::START_PAGE); 44} 45 46bool DOMView::SkipDefaultKeyEventProcessing(const views::KeyEvent& e) { 47 // Don't move the focus to the next view when tab is pressed, we want the 48 // key event to be propagated to the render view for doing the tab traversal 49 // there. 50 return views::FocusManager::IsTabTraversalKeyEvent(e); 51} 52 53void DOMView::Focus() { 54 tab_contents_->Focus(); 55} 56 57void DOMView::ViewHierarchyChanged(bool is_add, views::View* parent, 58 views::View* child) { 59 // Attach the native_view when this is added to Widget if 60 // the native view has not been attached yet and tab_contents_ exists. 61 views::NativeViewHost::ViewHierarchyChanged(is_add, parent, child); 62 if (is_add && GetWidget() && !native_view() && tab_contents_.get()) 63 AttachTabContents(); 64 else if (!is_add && child == this && native_view()) 65 Detach(); 66} 67 68void DOMView::AttachTabContents() { 69#if defined(TOUCH_UI) 70 AttachToView(static_cast<TabContentsViewViews*>(tab_contents_->view())); 71#else 72 Attach(tab_contents_->GetNativeView()); 73#endif 74} 75