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 "content/browser/tab_contents/tab_contents.h"
8#include "views/focus/focus_manager.h"
9
10#if defined(TOUCH_UI)
11#include "chrome/browser/ui/views/tab_contents/tab_contents_view_touch.h"
12#endif
13
14DOMView::DOMView() : tab_contents_(NULL), initialized_(false) {
15  SetFocusable(true);
16}
17
18DOMView::~DOMView() {
19  if (native_view())
20    Detach();
21}
22
23bool DOMView::Init(Profile* profile, SiteInstance* instance) {
24  if (initialized_)
25    return true;
26
27  initialized_ = true;
28  tab_contents_.reset(CreateTabContents(profile, instance));
29  // Attach the native_view now if the view is already added to Widget.
30  if (GetWidget())
31    AttachTabContents();
32
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::OnFocus() {
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<TabContentsViewTouch*>(tab_contents_->view()));
71#else
72  Attach(tab_contents_->GetNativeView());
73#endif
74}
75