| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_UI_SEARCH_INSTANT_TAB_H_ | 5 #ifndef CHROME_BROWSER_UI_SEARCH_INSTANT_TAB_H_ |
| 6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_TAB_H_ | 6 #define CHROME_BROWSER_UI_SEARCH_INSTANT_TAB_H_ |
| 7 | 7 |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/gtest_prod_util.h" | 9 #include "base/gtest_prod_util.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "chrome/browser/ui/search/search_model_observer.h" | 11 #include "chrome/browser/ui/search/search_model_observer.h" |
| 12 #include "content/public/browser/web_contents_observer.h" | 12 #include "content/public/browser/web_contents_observer.h" |
| 13 #include "ui/base/page_transition_types.h" | 13 #include "ui/base/page_transition_types.h" |
| 14 | 14 |
| 15 class GURL; | 15 class GURL; |
| 16 | 16 |
| 17 namespace content { | 17 namespace content { |
| 18 class RenderFrameHost; | 18 class RenderFrameHost; |
| 19 class WebContents; | 19 class WebContents; |
| 20 } | 20 } |
| 21 | 21 |
| 22 // InstantTab is used to exchange messages with a page that implements the | 22 // InstantTab is used to exchange messages with a page that implements the |
| 23 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch). | 23 // Instant/Embedded Search API (http://dev.chromium.org/embeddedsearch). |
| 24 class InstantTab : public content::WebContentsObserver, | 24 class InstantTab : public content::WebContentsObserver, |
| 25 public SearchModelObserver { | 25 public SearchModelObserver { |
| 26 public: | 26 public: |
| 27 // InstantTab calls its delegate in response to messages received from the | 27 // InstantTab calls its delegate in response to messages received from the |
| 28 // page. Each method is called with the |contents| corresponding to the page | 28 // page. Each method is called with the |contents| corresponding to the page |
| 29 // we are observing. | 29 // we are observing. |
| 30 class Delegate { | 30 class Delegate { |
| 31 public: | 31 public: |
| 32 // Called upon determination of Instant API support. Either in response to | 32 // Called upon determination of Instant API support. Either in response to |
| 33 // the page loading or because we received some other message. | 33 // the page loading or because we received some other message. |
| 34 virtual void InstantSupportDetermined(const content::WebContents* contents, | 34 virtual void InstantSupportDetermined(const content::WebContents* contents, |
| 35 bool supports_instant) = 0; | 35 bool supports_instant) = 0; |
| 36 | 36 |
| 37 // Called when the page is about to navigate to |url|. | 37 // Called when the page is about to navigate to |url|. |
| 38 virtual void InstantTabAboutToNavigateMainFrame( | 38 virtual void InstantTabAboutToNavigateMainFrame( |
| 39 const content::WebContents* contents, | 39 const content::WebContents* contents, |
| 40 const GURL& url) = 0; | 40 const GURL& url) = 0; |
| 41 | 41 |
| 42 protected: | 42 protected: |
| 43 virtual ~Delegate(); | 43 virtual ~Delegate(); |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 explicit InstantTab(Delegate* delegate); | 46 explicit InstantTab(Delegate* delegate, content::WebContents* web_contents); |
| 47 | 47 |
| 48 ~InstantTab() override; | 48 ~InstantTab() override; |
| 49 | 49 |
| 50 // Sets |web_contents| as the page to communicate with. |web_contents| may be | 50 // Sets up communication with the WebContents passed to the constructor. Does |
| 51 // NULL, which effectively stops all communication. | 51 // nothing if that WebContents was null. Should not be called more than once. |
| 52 void Init(content::WebContents* web_contents); | 52 void Init(); |
| 53 | |
| 54 // Returns true if the page is known to support the Instant API. This starts | |
| 55 // out false, and is set to true whenever we get any message from the page. | |
| 56 // Once true, it never becomes false (the page isn't expected to drop API | |
| 57 // support suddenly). | |
| 58 bool supports_instant() const; | |
| 59 | |
| 60 // Returns true if the page is the local NTP (i.e. its URL is | |
| 61 // chrome::kChromeSearchLocalNTPURL). | |
| 62 bool IsLocal() const; | |
| 63 | 53 |
| 64 private: | 54 private: |
| 65 FRIEND_TEST_ALL_PREFIXES(InstantTabTest, IsLocal); | |
| 66 FRIEND_TEST_ALL_PREFIXES(InstantTabTest, | |
| 67 DetermineIfPageSupportsInstant_Local); | |
| 68 FRIEND_TEST_ALL_PREFIXES(InstantTabTest, | |
| 69 DetermineIfPageSupportsInstant_NonLocal); | |
| 70 FRIEND_TEST_ALL_PREFIXES(InstantTabTest, | |
| 71 PageURLDoesntBelongToInstantRenderer); | |
| 72 FRIEND_TEST_ALL_PREFIXES(InstantTabTest, PageSupportsInstant); | |
| 73 | |
| 74 // Overridden from content::WebContentsObserver: | 55 // Overridden from content::WebContentsObserver: |
| 75 void DidCommitProvisionalLoadForFrame( | 56 void DidCommitProvisionalLoadForFrame( |
| 76 content::RenderFrameHost* render_frame_host, | 57 content::RenderFrameHost* render_frame_host, |
| 77 const GURL& url, | 58 const GURL& url, |
| 78 ui::PageTransition transition_type) override; | 59 ui::PageTransition transition_type) override; |
| 79 | 60 |
| 80 // Overridden from SearchModelObserver: | 61 // Overridden from SearchModelObserver: |
| 81 void ModelChanged(const SearchModel::State& old_state, | 62 void ModelChanged(const SearchModel::State& old_state, |
| 82 const SearchModel::State& new_state) override; | 63 const SearchModel::State& new_state) override; |
| 83 | 64 |
| 84 // Update the status of Instant support. | |
| 85 void InstantSupportDetermined(bool supports_instant); | 65 void InstantSupportDetermined(bool supports_instant); |
| 86 | 66 |
| 87 void ClearContents(); | 67 Delegate* const delegate_; |
| 88 | 68 |
| 89 Delegate* const delegate_; | 69 content::WebContents* pending_web_contents_; |
| 90 | 70 |
| 91 DISALLOW_COPY_AND_ASSIGN(InstantTab); | 71 DISALLOW_COPY_AND_ASSIGN(InstantTab); |
| 92 }; | 72 }; |
| 93 | 73 |
| 94 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_TAB_H_ | 74 #endif // CHROME_BROWSER_UI_SEARCH_INSTANT_TAB_H_ |
| OLD | NEW |