• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <string>
10 
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "chrome/browser/printing/print_view_manager.h"
15 #include "content/browser/tab_contents/tab_contents.h"
16 #include "content/browser/tab_contents/tab_contents_observer.h"
17 #include "content/common/notification_registrar.h"
18 
19 namespace prerender {
20 class PrerenderObserver;
21 }
22 
23 namespace printing {
24 class PrintPreviewMessageHandler;
25 }
26 
27 class AutocompleteHistoryManager;
28 class AutofillManager;
29 class AutomationTabHelper;
30 class DownloadTabHelper;
31 class Extension;
32 class ExtensionTabHelper;
33 class ExtensionWebNavigationTabObserver;
34 class FileSelectObserver;
35 class FindTabHelper;
36 class NavigationController;
37 class PasswordManager;
38 class PasswordManagerDelegate;
39 class SearchEngineTabHelper;
40 class TabContentsWrapperDelegate;
41 class TranslateTabHelper;
42 
43 // Wraps TabContents and all of its supporting objects in order to control
44 // their ownership and lifetime, while allowing TabContents to remain generic
45 // and re-usable in other projects.
46 // TODO(pinkerton): Eventually, this class will become TabContents as far as
47 // the browser front-end is concerned, and the current TabContents will be
48 // renamed to something like WebPage or WebView (ben's suggestions).
49 class TabContentsWrapper : public NotificationObserver,
50                            public TabContentsObserver {
51  public:
52   // Takes ownership of |contents|, which must be heap-allocated (as it lives
53   // in a scoped_ptr) and can not be NULL.
54   explicit TabContentsWrapper(TabContents* contents);
55   ~TabContentsWrapper();
56 
57   // Used to retrieve this object from |tab_contents_|, which is placed in
58   // its property bag to avoid adding additional interfaces.
59   static PropertyAccessor<TabContentsWrapper*>* property_accessor();
60 
61   static void RegisterUserPrefs(PrefService* prefs);
62 
63   // Initial title assigned to NavigationEntries from Navigate.
64   static string16 GetDefaultTitle();
65 
66   // Returns a human-readable description the tab's loading state.
67   string16 GetStatusText() const;
68 
69   // Create a TabContentsWrapper with the same state as this one. The returned
70   // heap-allocated pointer is owned by the caller.
71   TabContentsWrapper* Clone();
72 
73   // Helper to retrieve the existing instance that wraps a given TabContents.
74   // Returns NULL if there is no such existing instance.
75   // NOTE: This is not intended for general use. It is intended for situations
76   // like callbacks from content/ where only a TabContents is available. In the
77   // general case, please do NOT use this; plumb TabContentsWrapper through the
78   // chrome/ code instead of TabContents.
79   static TabContentsWrapper* GetCurrentWrapperForContents(
80       TabContents* contents);
81 
delegate()82   TabContentsWrapperDelegate* delegate() const { return delegate_; }
set_delegate(TabContentsWrapperDelegate * d)83   void set_delegate(TabContentsWrapperDelegate* d) { delegate_ = d; }
84 
tab_contents()85   TabContents* tab_contents() const { return tab_contents_.get(); }
controller()86   NavigationController& controller() const {
87     return tab_contents()->controller();
88   }
view()89   TabContentsView* view() const { return tab_contents()->view(); }
render_view_host()90   RenderViewHost* render_view_host() const {
91     return tab_contents()->render_view_host();
92   }
profile()93   Profile* profile() const { return tab_contents()->profile(); }
is_starred()94   bool is_starred() const { return is_starred_; }
95 
96   // Tab Helpers ---------------------------------------------------------------
97 
autocomplete_history_manager()98   AutocompleteHistoryManager* autocomplete_history_manager() {
99     return autocomplete_history_manager_.get();
100   }
101 
autofill_manager()102   AutofillManager* autofill_manager() { return autofill_manager_.get(); }
103 
104   // Used only for testing/automation.
automation_tab_helper()105   AutomationTabHelper* automation_tab_helper() {
106     return automation_tab_helper_.get();
107   }
108 
download_tab_helper()109   DownloadTabHelper* download_tab_helper() {
110     return download_tab_helper_.get();
111   }
112 
extension_tab_helper()113   ExtensionTabHelper* extension_tab_helper() {
114     return extension_tab_helper_.get();
115   }
116 
find_tab_helper()117   FindTabHelper* find_tab_helper() { return find_tab_helper_.get(); }
118 
password_manager()119   PasswordManager* password_manager() { return password_manager_.get(); }
120 
print_view_manager()121   printing::PrintViewManager* print_view_manager() {
122     return print_view_manager_.get();
123   }
124 
search_engine_tab_helper()125   SearchEngineTabHelper* search_engine_tab_helper() {
126     return search_engine_tab_helper_.get();
127   }
128 
translate_tab_helper()129   TranslateTabHelper* translate_tab_helper() {
130     return translate_tab_helper_.get();
131   }
132 
133   // Overrides -----------------------------------------------------------------
134 
135   // TabContentsObserver overrides:
136   virtual void DidNavigateMainFramePostCommit(
137       const NavigationController::LoadCommittedDetails& details,
138       const ViewHostMsg_FrameNavigate_Params& params) OVERRIDE;
139   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
140 
141   // NotificationObserver overrides:
142   virtual void Observe(NotificationType type,
143                        const NotificationSource& source,
144                        const NotificationDetails& details) OVERRIDE;
145 
146  private:
147   // Internal helpers ----------------------------------------------------------
148 
149   // Message handlers.
150   void OnPageContents(const GURL& url,
151                       int32 page_id,
152                       const string16& contents);
153   void OnJSOutOfMemory();
154   void OnRegisterProtocolHandler(const std::string& protocol,
155                                  const GURL& url,
156                                  const string16& title);
157   void OnMsgThumbnail(const GURL& url,
158                       const ThumbnailScore& score,
159                       const SkBitmap& bitmap);
160 
161   // Updates the starred state from the bookmark bar model. If the state has
162   // changed, the delegate is notified.
163   void UpdateStarredStateForCurrentURL();
164 
165   // Data for core operation ---------------------------------------------------
166 
167   // Delegate for notifying our owner about stuff. Not owned by us.
168   TabContentsWrapperDelegate* delegate_;
169 
170   // Registers and unregisters us for notifications.
171   NotificationRegistrar registrar_;
172 
173   // Data for current page -----------------------------------------------------
174 
175   // Whether the current URL is starred.
176   bool is_starred_;
177 
178   // Tab Helpers ---------------------------------------------------------------
179   // (These provide API for callers and have a getter function listed in the
180   // "Tab Helpers" section in the member functions area, above.)
181 
182   scoped_ptr<AutocompleteHistoryManager> autocomplete_history_manager_;
183   scoped_ptr<AutofillManager> autofill_manager_;
184   scoped_ptr<AutomationTabHelper> automation_tab_helper_;
185   scoped_ptr<DownloadTabHelper> download_tab_helper_;
186   scoped_ptr<ExtensionTabHelper> extension_tab_helper_;
187   scoped_ptr<FindTabHelper> find_tab_helper_;
188 
189   // PasswordManager and its delegate. The delegate must outlive the manager,
190   // per documentation in password_manager.h.
191   scoped_ptr<PasswordManagerDelegate> password_manager_delegate_;
192   scoped_ptr<PasswordManager> password_manager_;
193 
194   // Handles print job for this contents.
195   scoped_ptr<printing::PrintViewManager> print_view_manager_;
196 
197   scoped_ptr<SearchEngineTabHelper> search_engine_tab_helper_;
198   scoped_ptr<TranslateTabHelper> translate_tab_helper_;
199 
200   // Per-tab observers ---------------------------------------------------------
201   // (These provide no API for callers; objects that need to exist 1:1 with tabs
202   // and silently do their thing live here.)
203 
204   scoped_ptr<FileSelectObserver> file_select_observer_;
205   scoped_ptr<prerender::PrerenderObserver> prerender_observer_;
206   scoped_ptr<printing::PrintPreviewMessageHandler> print_preview_;
207   scoped_ptr<ExtensionWebNavigationTabObserver> webnavigation_observer_;
208 
209   // TabContents (MUST BE LAST) ------------------------------------------------
210 
211   // The supporting objects need to outlive the TabContents dtor (as they may
212   // be called upon during its execution). As a result, this must come last
213   // in the list.
214   scoped_ptr<TabContents> tab_contents_;
215 
216   DISALLOW_COPY_AND_ASSIGN(TabContentsWrapper);
217 };
218 
219 #endif  // CHROME_BROWSER_UI_TAB_CONTENTS_TAB_CONTENTS_WRAPPER_H_
220