tab_contents_wrapper.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/tab_contents/tab_contents_wrapper.h"
6
7#include "base/lazy_instance.h"
8#include "chrome/browser/password_manager/password_manager.h"
9#include "chrome/browser/password_manager_delegate_impl.h"
10#include "chrome/browser/tab_contents/tab_contents.h"
11
12static base::LazyInstance<PropertyAccessor<TabContentsWrapper*> >
13    g_tab_contents_wrapper_property_accessor(base::LINKER_INITIALIZED);
14
15////////////////////////////////////////////////////////////////////////////////
16// TabContentsWrapper, public:
17
18TabContentsWrapper::TabContentsWrapper(TabContents* contents)
19    : tab_contents_(contents) {
20  DCHECK(contents);
21  // Stash this in the property bag so it can be retrieved without having to
22  // go to a Browser.
23  property_accessor()->SetProperty(contents->property_bag(), this);
24
25  // Needed so that we initialize the password manager on first navigation.
26  tab_contents()->AddNavigationObserver(this);
27}
28
29TabContentsWrapper::~TabContentsWrapper() {
30  // Unregister observers (TabContents outlives supporting objects).
31  tab_contents()->RemoveNavigationObserver(password_manager_.get());
32}
33
34PropertyAccessor<TabContentsWrapper*>* TabContentsWrapper::property_accessor() {
35  return g_tab_contents_wrapper_property_accessor.Pointer();
36}
37
38TabContentsWrapper* TabContentsWrapper::Clone() {
39  TabContents* new_contents = tab_contents()->Clone();
40  TabContentsWrapper* new_wrapper = new TabContentsWrapper(new_contents);
41  // Instantiate the passowrd manager if it has been instantiated here.
42  if (password_manager_.get())
43    new_wrapper->GetPasswordManager();
44  return new_wrapper;
45}
46
47PasswordManager* TabContentsWrapper::GetPasswordManager() {
48  if (!password_manager_.get()) {
49    // Create the delegate then create the manager.
50    password_manager_delegate_.reset(
51        new PasswordManagerDelegateImpl(tab_contents()));
52    password_manager_.reset(
53        new PasswordManager(password_manager_delegate_.get()));
54    // Register the manager to receive navigation notifications.
55    tab_contents()->AddNavigationObserver(password_manager_.get());
56  }
57  return password_manager_.get();
58}
59
60////////////////////////////////////////////////////////////////////////////////
61// TabContentsWrapper, WebNavigationObserver implementation:
62
63void TabContentsWrapper::NavigateToPendingEntry() {
64  GetPasswordManager();
65  tab_contents()->RemoveNavigationObserver(this);
66}
67