OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_ALTERNATE_NAV_URL_FETCHER_H_ | 5 #ifndef CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_ |
6 #define CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_ | 6 #define CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "content/common/net/url_fetcher.h" | 12 #include "content/public/common/url_fetcher_delegate.h" |
13 #include "content/public/browser/notification_observer.h" | 13 #include "content/public/browser/notification_observer.h" |
14 #include "content/public/browser/notification_registrar.h" | 14 #include "content/public/browser/notification_registrar.h" |
15 #include "googleurl/src/gurl.h" | 15 #include "googleurl/src/gurl.h" |
16 | 16 |
17 class NavigationController; | 17 class NavigationController; |
18 | 18 |
19 // Attempts to get the HEAD of a host name and displays an info bar if the | 19 // Attempts to get the HEAD of a host name and displays an info bar if the |
20 // request was successful. This is used for single-word queries where we can't | 20 // request was successful. This is used for single-word queries where we can't |
21 // tell if the entry was a search or an intranet hostname. The autocomplete bar | 21 // tell if the entry was a search or an intranet hostname. The autocomplete bar |
22 // assumes it's a query and issues an AlternateNavURLFetcher to display a "did | 22 // assumes it's a query and issues an AlternateNavURLFetcher to display a "did |
23 // you mean" infobar suggesting a navigation. | 23 // you mean" infobar suggesting a navigation. |
24 // | 24 // |
25 // The memory management of this object is a bit tricky. The location bar view | 25 // The memory management of this object is a bit tricky. The location bar view |
26 // will create us and be responsible for us until we attach as an observer | 26 // will create us and be responsible for us until we attach as an observer |
27 // after a pending load starts (it will delete us if this doesn't happen). | 27 // after a pending load starts (it will delete us if this doesn't happen). |
28 // Once this pending load starts, we're responsible for deleting ourselves. | 28 // Once this pending load starts, we're responsible for deleting ourselves. |
29 // We'll do this in the following cases: | 29 // We'll do this in the following cases: |
30 // * The tab is navigated again once we start listening (so the fetch is no | 30 // * The tab is navigated again once we start listening (so the fetch is no |
31 // longer useful) | 31 // longer useful) |
32 // * The tab is closed before we show an infobar | 32 // * The tab is closed before we show an infobar |
33 // * The intranet fetch fails | 33 // * The intranet fetch fails |
34 // * None of the above apply, so we successfully show an infobar | 34 // * None of the above apply, so we successfully show an infobar |
35 class AlternateNavURLFetcher : public content::NotificationObserver, | 35 class AlternateNavURLFetcher : public content::NotificationObserver, |
36 public URLFetcher::Delegate { | 36 public content::URLFetcherDelegate { |
37 public: | 37 public: |
38 enum State { | 38 enum State { |
39 NOT_STARTED, | 39 NOT_STARTED, |
40 IN_PROGRESS, | 40 IN_PROGRESS, |
41 SUCCEEDED, | 41 SUCCEEDED, |
42 FAILED, | 42 FAILED, |
43 }; | 43 }; |
44 | 44 |
45 explicit AlternateNavURLFetcher(const GURL& alternate_nav_url); | 45 explicit AlternateNavURLFetcher(const GURL& alternate_nav_url); |
46 virtual ~AlternateNavURLFetcher(); | 46 virtual ~AlternateNavURLFetcher(); |
47 | 47 |
48 State state() const { return state_; } | 48 State state() const { return state_; } |
49 | 49 |
50 private: | 50 private: |
51 // content::NotificationObserver | 51 // content::NotificationObserver |
52 virtual void Observe(int type, | 52 virtual void Observe(int type, |
53 const content::NotificationSource& source, | 53 const content::NotificationSource& source, |
54 const content::NotificationDetails& details) OVERRIDE; | 54 const content::NotificationDetails& details) OVERRIDE; |
55 | 55 |
56 // URLFetcher::Delegate | 56 // content::URLFetcherDelegate |
57 virtual void OnURLFetchComplete(const URLFetcher* source, | 57 virtual void OnURLFetchComplete(const URLFetcher* source) OVERRIDE; |
58 const GURL& url, | |
59 const net::URLRequestStatus& status, | |
60 int response_code, | |
61 const net::ResponseCookies& cookies, | |
62 const std::string& data) OVERRIDE; | |
63 | 58 |
64 // Sets |controller_| to the supplied pointer and begins fetching | 59 // Sets |controller_| to the supplied pointer and begins fetching |
65 // |alternate_nav_url_|. | 60 // |alternate_nav_url_|. |
66 void StartFetch(NavigationController* controller); | 61 void StartFetch(NavigationController* controller); |
67 | 62 |
68 // Sets |state_| to either SUCCEEDED or FAILED depending on the result of the | 63 // Sets |state_| to either SUCCEEDED or FAILED depending on the result of the |
69 // fetch. | 64 // fetch. |
70 void SetStatusFromURLFetch(const GURL& url, | 65 void SetStatusFromURLFetch(const GURL& url, |
71 const net::URLRequestStatus& status, | 66 const net::URLRequestStatus& status, |
72 int response_code); | 67 int response_code); |
73 | 68 |
74 // Displays the infobar if all conditions are met (the page has loaded and | 69 // Displays the infobar if all conditions are met (the page has loaded and |
75 // the fetch of the alternate URL succeeded). Unless we're still waiting on | 70 // the fetch of the alternate URL succeeded). Unless we're still waiting on |
76 // one of the above conditions to finish, this will also delete us, as whether | 71 // one of the above conditions to finish, this will also delete us, as whether |
77 // or not we show an infobar, there is no reason to live further. | 72 // or not we show an infobar, there is no reason to live further. |
78 void ShowInfobarIfPossible(); | 73 void ShowInfobarIfPossible(); |
79 | 74 |
80 GURL alternate_nav_url_; | 75 GURL alternate_nav_url_; |
81 scoped_ptr<URLFetcher> fetcher_; | 76 scoped_ptr<URLFetcher> fetcher_; |
82 NavigationController* controller_; | 77 NavigationController* controller_; |
83 State state_; | 78 State state_; |
84 bool navigated_to_entry_; | 79 bool navigated_to_entry_; |
85 | 80 |
86 content::NotificationRegistrar registrar_; | 81 content::NotificationRegistrar registrar_; |
87 | 82 |
88 DISALLOW_COPY_AND_ASSIGN(AlternateNavURLFetcher); | 83 DISALLOW_COPY_AND_ASSIGN(AlternateNavURLFetcher); |
89 }; | 84 }; |
90 | 85 |
91 #endif // CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_ | 86 #endif // CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_ |
OLD | NEW |