tab_contents_wrapper.h 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#ifndef CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_WRAPPER_H_
6#define CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_WRAPPER_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/scoped_ptr.h"
11#include "base/compiler_specific.h"
12#include "chrome/browser/tab_contents/tab_contents.h"
13#include "chrome/browser/tab_contents/tab_contents_observer.h"
14#include "chrome/common/notification_registrar.h"
15
16class Extension;
17class FindManager;
18class NavigationController;
19class PasswordManager;
20class PasswordManagerDelegate;
21class TabContentsWrapperDelegate;
22
23// Wraps TabContents and all of its supporting objects in order to control
24// their ownership and lifetime, while allowing TabContents to remain generic
25// and re-usable in other projects.
26// TODO(pinkerton): Eventually, this class will become TabContents as far as
27// the browser front-end is concerned, and the current TabContents will be
28// renamed to something like WebPage or WebView (ben's suggestions).
29class TabContentsWrapper : public NotificationObserver,
30                           public TabContentsObserver {
31 public:
32  // Takes ownership of |contents|, which must be heap-allocated (as it lives
33  // in a scoped_ptr) and can not be NULL.
34  explicit TabContentsWrapper(TabContents* contents);
35  ~TabContentsWrapper();
36
37  // Used to retrieve this object from |tab_contents_|, which is placed in
38  // its property bag to avoid adding additional interfaces.
39  static PropertyAccessor<TabContentsWrapper*>* property_accessor();
40
41  // Create a TabContentsWrapper with the same state as this one. The returned
42  // heap-allocated pointer is owned by the caller.
43  TabContentsWrapper* Clone();
44
45  // Helper to retrieve the existing instance that wraps a given TabContents.
46  // Returns NULL if there is no such existing instance.
47  static TabContentsWrapper* GetCurrentWrapperForContents(
48      TabContents* contents);
49
50  TabContentsWrapperDelegate* delegate() const { return delegate_; }
51  void set_delegate(TabContentsWrapperDelegate* d) { delegate_ = d; }
52
53  TabContents* tab_contents() const { return tab_contents_.get(); }
54  NavigationController& controller() const {
55    return tab_contents()->controller();
56  }
57  TabContentsView* view() const { return tab_contents()->view(); }
58  RenderViewHost* render_view_host() const {
59    return tab_contents()->render_view_host();
60  }
61  Profile* profile() const { return tab_contents()->profile(); }
62
63  // Convenience methods until extensions are removed from TabContents.
64  void SetExtensionAppById(const std::string& extension_app_id) {
65    tab_contents()->SetExtensionAppById(extension_app_id);
66  }
67  const Extension* extension_app() const {
68    return tab_contents()->extension_app();
69  }
70  bool is_app() const { return tab_contents()->is_app(); }
71
72  bool is_starred() const { return is_starred_; }
73
74  // Returns the PasswordManager, creating it if necessary.
75  PasswordManager* GetPasswordManager();
76
77  // Returns the FindManager, creating it if necessary.
78  FindManager* GetFindManager();
79
80  // Overrides -----------------------------------------------------------------
81
82  // TabContentsObserver overrides:
83  virtual void NavigateToPendingEntry() OVERRIDE;
84  virtual void DidNavigateMainFramePostCommit(
85      const NavigationController::LoadCommittedDetails& details,
86      const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE;
87
88  // NotificationObserver overrides:
89  virtual void Observe(NotificationType type,
90                       const NotificationSource& source,
91                       const NotificationDetails& details) OVERRIDE;
92
93 private:
94  // Internal helpers ----------------------------------------------------------
95
96  // Updates the starred state from the bookmark bar model. If the state has
97  // changed, the delegate is notified.
98  void UpdateStarredStateForCurrentURL();
99
100  // Data for core operation ---------------------------------------------------
101
102  // Delegate for notifying our owner about stuff. Not owned by us.
103  TabContentsWrapperDelegate* delegate_;
104
105  // Registers and unregisters us for notifications.
106  NotificationRegistrar registrar_;
107
108  // Data for current page -----------------------------------------------------
109
110  // Whether the current URL is starred.
111  bool is_starred_;
112
113  // Supporting objects --------------------------------------------------------
114
115  // PasswordManager and its delegate, lazily created. The delegate must
116  // outlive the manager, per documentation in password_manager.h.
117  scoped_ptr<PasswordManagerDelegate> password_manager_delegate_;
118  scoped_ptr<PasswordManager> password_manager_;
119
120  // FindManager, lazily created.
121  scoped_ptr<FindManager> find_manager_;
122
123  // TabContents (MUST BE LAST) ------------------------------------------------
124
125  // The supporting objects need to outlive the TabContents dtor (as they may
126  // be called upon during its execution). As a result, this must come last
127  // in the list.
128  scoped_ptr<TabContents> tab_contents_;
129
130  DISALLOW_COPY_AND_ASSIGN(TabContentsWrapper);
131};
132
133#endif  // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_WRAPPER_H_
134