| OLD | NEW |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 5 #ifndef CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
| 6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 6 #define CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
| 7 | 7 |
| 8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | |
| 10 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/gtest_prod_util.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/timer.h" | 12 #include "base/timer.h" |
| 13 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" | 13 #include "chrome/browser/instant/instant_page.h" |
| 14 #include "content/public/browser/notification_observer.h" | 14 #include "chrome/browser/instant/instant_service_observer.h" |
| 15 #include "content/public/browser/notification_registrar.h" | 15 #include "chrome/browser/instant/instant_web_contents_container.h" |
| 16 #include "content/public/browser/web_contents_delegate.h" | |
| 17 | 16 |
| 18 class GURL; | 17 class InstantService; |
| 19 class Profile; | |
| 20 | 18 |
| 21 namespace content { | 19 namespace content { |
| 22 struct OpenURLParams; | |
| 23 class WebContents; | 20 class WebContents; |
| 24 } | 21 } |
| 25 | 22 |
| 26 // InstantLoader is used to create and maintain a WebContents where we can | 23 // InstantLoader maintains a hidden WebContents into which it loads the default |
| 27 // preload a page into. It is used by InstantOverlay and InstantNTP to | 24 // search engine's Instant URL. InstantLoader is owned by InstantService. |
| 28 // preload an Instant page. | 25 class InstantLoader : public InstantPage::Delegate, |
| 29 class InstantLoader : public content::NotificationObserver, | 26 public InstantServiceObserver, |
| 30 public content::WebContentsDelegate, | 27 public InstantWebContentsContainer { |
| 31 public CoreTabHelperDelegate { | |
| 32 public: | 28 public: |
| 33 // InstantLoader calls these methods on its delegate in response to certain | 29 explicit InstantLoader(InstantService* service); |
| 34 // changes in the underlying contents. | |
| 35 class Delegate { | |
| 36 public: | |
| 37 // Called after someone has swapped in a different WebContents for ours. | |
| 38 virtual void OnSwappedContents() = 0; | |
| 39 | |
| 40 // Called when the underlying contents receive focus. | |
| 41 virtual void OnFocus() = 0; | |
| 42 | |
| 43 // Called when the mouse pointer is down. | |
| 44 virtual void OnMouseDown() = 0; | |
| 45 | |
| 46 // Called when the mouse pointer is released (or a drag event ends). | |
| 47 virtual void OnMouseUp() = 0; | |
| 48 | |
| 49 // Called to open a URL using the underlying contents (see | |
| 50 // WebContentsDelegate::OpenURLFromTab). The Delegate should return the | |
| 51 // WebContents the URL is opened in, or NULL if the URL wasn't opened | |
| 52 // immediately. | |
| 53 virtual content::WebContents* OpenURLFromTab( | |
| 54 content::WebContents* source, | |
| 55 const content::OpenURLParams& params) = 0; | |
| 56 | |
| 57 protected: | |
| 58 ~Delegate(); | |
| 59 }; | |
| 60 | |
| 61 explicit InstantLoader(Delegate* delegate); | |
| 62 virtual ~InstantLoader(); | 30 virtual ~InstantLoader(); |
| 63 | 31 |
| 64 // Creates a new WebContents in the context of |profile| that will be used to | 32 // Returns whether the |contents_| is known to support the Instant API. |
| 65 // load |instant_url|. The page is not actually loaded until Load() is | 33 bool supports_instant() const { return page_.supports_instant(); } |
| 66 // called. Uses |active_contents|, if non-NULL, to initialize the size of the | |
| 67 // new contents. |on_stale_callback| will be called after kStalePageTimeoutMS | |
| 68 // has elapsed after Load() being called. | |
| 69 void Init(const GURL& instant_url, | |
| 70 Profile* profile, | |
| 71 const content::WebContents* active_contents, | |
| 72 const base::Closure& on_stale_callback); | |
| 73 | 34 |
| 74 // Loads |instant_url_| in |contents_|. | 35 // Creates a new |contents_| and loads the Instant URL into it. If |contents_| |
| 75 void Load(); | 36 // is already valid, does nothing. |
| 37 void InitContents(); |
| 76 | 38 |
| 77 // Returns the contents currently held. May be NULL. | 39 // Gives up |contents_| to the caller, and creates a new one in its place. |
| 78 content::WebContents* contents() const { return contents_.get(); } | 40 scoped_ptr<content::WebContents> ReleaseContents(); |
| 79 | |
| 80 // Replaces the contents held with |contents|. Any existing contents is | |
| 81 // deleted. The expiration timer is not restarted. | |
| 82 void SetContents(scoped_ptr<content::WebContents> contents); | |
| 83 | |
| 84 // Releases the contents currently held. Must only be called if contents() is | |
| 85 // not NULL. | |
| 86 scoped_ptr<content::WebContents> ReleaseContents() WARN_UNUSED_RESULT; | |
| 87 | 41 |
| 88 private: | 42 private: |
| 89 // Overridden from content::NotificationObserver: | 43 FRIEND_TEST_ALL_PREFIXES(InstantTest, InstantLoaderRefresh); |
| 90 virtual void Observe(int type, | |
| 91 const content::NotificationSource& source, | |
| 92 const content::NotificationDetails& details) OVERRIDE; | |
| 93 | 44 |
| 94 // Overridden from CoreTabHelperDelegate: | 45 // Overridden from InstantPage::Delegate: |
| 95 virtual void SwapTabContents(content::WebContents* old_contents, | 46 virtual void RenderViewGone(const content::WebContents* contents) OVERRIDE; |
| 96 content::WebContents* new_contents) OVERRIDE; | 47 virtual void InitSearchBox(const content::WebContents* contents) OVERRIDE; |
| 48 virtual void InstantSupportDetermined( |
| 49 const content::WebContents* contents) OVERRIDE; |
| 50 virtual void SetSuggestion(const content::WebContents* contents, |
| 51 const InstantSuggestion& suggestion) OVERRIDE; |
| 52 virtual void NavigateToURL(const content::WebContents* contents, |
| 53 const GURL& url, |
| 54 content::PageTransition transition, |
| 55 WindowOpenDisposition disposition) OVERRIDE; |
| 56 virtual void ShowOverlay(const content::WebContents* contents, |
| 57 int height, |
| 58 bool is_height_in_pixels) OVERRIDE; |
| 59 virtual void StartKeyCapture(const content::WebContents* contents) OVERRIDE; |
| 60 virtual void StopKeyCapture(const content::WebContents* contents) OVERRIDE; |
| 61 virtual void DeleteMostVisitedItem(const content::WebContents* contents, |
| 62 const GURL& url) OVERRIDE; |
| 63 virtual void UndoMostVisitedItemDeletion(const content::WebContents* contents, |
| 64 const GURL& url) OVERRIDE; |
| 65 virtual void UndoAllMostVisitedItemDeletions( |
| 66 const content::WebContents* contents) OVERRIDE; |
| 97 | 67 |
| 98 // Overridden from content::WebContentsDelegate: | 68 // Overridden from InstantServiceObserver: |
| 99 virtual bool ShouldSuppressDialogs() OVERRIDE; | 69 virtual void InstantStatusChanged() OVERRIDE; |
| 100 virtual bool ShouldFocusPageAfterCrash() OVERRIDE; | 70 virtual void ThemeInfoChanged() OVERRIDE; |
| 101 virtual void LostCapture() OVERRIDE; | 71 virtual void MostVisitedItemsChanged() OVERRIDE; |
| 102 virtual void WebContentsFocused(content::WebContents* contents) OVERRIDE; | 72 virtual void InstantSupportDecided() OVERRIDE; |
| 103 virtual bool CanDownload(content::RenderViewHost* render_view_host, | |
| 104 int request_id, | |
| 105 const std::string& request_method) OVERRIDE; | |
| 106 virtual void HandleMouseDown() OVERRIDE; | |
| 107 virtual void HandleMouseUp() OVERRIDE; | |
| 108 virtual void HandlePointerActivate() OVERRIDE; | |
| 109 virtual void HandleGestureEnd() OVERRIDE; | |
| 110 virtual void DragEnded() OVERRIDE; | |
| 111 virtual bool OnGoToEntryOffset(int offset) OVERRIDE; | |
| 112 virtual content::WebContents* OpenURLFromTab( | |
| 113 content::WebContents* source, | |
| 114 const content::OpenURLParams& params) OVERRIDE; | |
| 115 | 73 |
| 116 Delegate* const delegate_; | 74 // Overridden from InstantWebContentsContainer: |
| 117 scoped_ptr<content::WebContents> contents_; | 75 virtual void CloseContents(content::WebContents* source) OVERRIDE; |
| 76 virtual void TearDownContents() OVERRIDE; |
| 118 | 77 |
| 119 // The URL we will be loading. | 78 void DeleteContents(); |
| 120 GURL instant_url_; | 79 void ReloadContents(); |
| 121 | 80 |
| 122 // Called when |stale_page_timer_| fires. | 81 InstantService* const service_; |
| 123 base::Closure on_stale_callback_; | 82 base::OneShotTimer<InstantLoader> stale_page_timer_; |
| 124 | |
| 125 // Used to mark when the page is stale. | |
| 126 base::Timer stale_page_timer_; | |
| 127 | |
| 128 // Used to get notifications about renderers. | |
| 129 content::NotificationRegistrar registrar_; | |
| 130 | 83 |
| 131 DISALLOW_COPY_AND_ASSIGN(InstantLoader); | 84 DISALLOW_COPY_AND_ASSIGN(InstantLoader); |
| 132 }; | 85 }; |
| 133 | 86 |
| 134 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ | 87 #endif // CHROME_BROWSER_INSTANT_INSTANT_LOADER_H_ |
| OLD | NEW |