Home | History | Annotate | Download | only in tab_contents
      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/tab_contents/tab_contents_container.h"
      6 
      7 #include "chrome/browser/ui/view_ids.h"
      8 #include "chrome/browser/ui/views/tab_contents/native_tab_contents_container.h"
      9 #include "content/browser/renderer_host/render_view_host.h"
     10 #include "content/browser/renderer_host/render_widget_host_view.h"
     11 #include "content/browser/tab_contents/interstitial_page.h"
     12 #include "content/browser/tab_contents/tab_contents.h"
     13 #include "content/common/notification_details.h"
     14 #include "content/common/notification_source.h"
     15 #include "ui/base/accessibility/accessible_view_state.h"
     16 
     17 
     18 ////////////////////////////////////////////////////////////////////////////////
     19 // TabContentsContainer, public:
     20 
     21 TabContentsContainer::~TabContentsContainer() {
     22   if (tab_contents_)
     23     RemoveObservers();
     24 }
     25 
     26 ////////////////////////////////////////////////////////////////////////////////
     27 // TabContentsContainer, NotificationObserver implementation:
     28 
     29 void TabContentsContainer::Observe(NotificationType type,
     30                                    const NotificationSource& source,
     31                                    const NotificationDetails& details) {
     32   if (type == NotificationType::RENDER_VIEW_HOST_CHANGED) {
     33     RenderViewHostSwitchedDetails* switched_details =
     34         Details<RenderViewHostSwitchedDetails>(details).ptr();
     35     RenderViewHostChanged(switched_details->old_host,
     36                           switched_details->new_host);
     37   } else if (type == NotificationType::TAB_CONTENTS_DESTROYED) {
     38     TabContentsDestroyed(Source<TabContents>(source).ptr());
     39   } else {
     40     NOTREACHED();
     41   }
     42 }
     43 
     44 ////////////////////////////////////////////////////////////////////////////////
     45 // TabContentsContainer, View overrides:
     46 
     47 void TabContentsContainer::GetAccessibleState(ui::AccessibleViewState* state) {
     48   state->role = ui::AccessibilityTypes::ROLE_WINDOW;
     49 }
     50 
     51 ////////////////////////////////////////////////////////////////////////////////
     52 // TabContentsContainer, private:
     53 
     54 void TabContentsContainer::AddObservers() {
     55   // TabContents can change their RenderViewHost and hence the HWND that is
     56   // shown and getting focused.  We need to keep track of that so we install
     57   // the focus subclass on the shown HWND so we intercept focus change events.
     58   registrar_.Add(this,
     59                  NotificationType::RENDER_VIEW_HOST_CHANGED,
     60                  Source<NavigationController>(&tab_contents_->controller()));
     61 
     62   registrar_.Add(this,
     63                  NotificationType::TAB_CONTENTS_DESTROYED,
     64                  Source<TabContents>(tab_contents_));
     65 }
     66 
     67 void TabContentsContainer::RemoveObservers() {
     68   registrar_.RemoveAll();
     69 }
     70 
     71 void TabContentsContainer::TabContentsDestroyed(TabContents* contents) {
     72   // Sometimes, a TabContents is destroyed before we know about it. This allows
     73   // us to clean up our state in case this happens.
     74   DCHECK(contents == tab_contents_);
     75   ChangeTabContents(NULL);
     76 }
     77 
     78 void TabContentsContainer::RenderWidgetHostViewChanged(
     79     RenderWidgetHostView* new_view) {
     80   if (new_view)
     81     new_view->set_reserved_contents_rect(cached_reserved_rect_);
     82 }
     83