alternate_nav_url_fetcher.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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_ALTERNATE_NAV_URL_FETCHER_H_
6#define CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_
7
8#include <string>
9
10#include "chrome/browser/tab_contents/infobar_delegate.h"
11#include "chrome/common/net/url_fetcher.h"
12#include "chrome/common/notification_observer.h"
13#include "chrome/common/notification_registrar.h"
14
15class NavigationController;
16
17// Attempts to get the HEAD of a host name and displays an info bar if the
18// request was successful. This is used for single-word queries where we can't
19// tell if the entry was a search or an intranet hostname. The autocomplete bar
20// assumes it's a query and issues an AlternateNavURLFetcher to display a "did
21// you mean" infobar suggesting a navigation.
22//
23// The memory management of this object is a bit tricky. The location bar view
24// will create us and be responsible for us until we attach as an observer
25// after a pending load starts (it will delete us if this doesn't happen).
26// Once this pending load starts, we're responsible for deleting ourselves.
27// We'll do this when the load commits, or when the navigation controller
28// itself is deleted.
29class AlternateNavURLFetcher : public NotificationObserver,
30                               public URLFetcher::Delegate,
31                               public LinkInfoBarDelegate {
32 public:
33  enum State {
34    NOT_STARTED,
35    IN_PROGRESS,
36    SUCCEEDED,
37    FAILED,
38  };
39
40  explicit AlternateNavURLFetcher(const GURL& alternate_nav_url);
41
42  State state() const { return state_; }
43
44  // NotificationObserver
45  virtual void Observe(NotificationType type,
46                       const NotificationSource& source,
47                       const NotificationDetails& details);
48
49  // URLFetcher::Delegate
50  virtual void OnURLFetchComplete(const URLFetcher* source,
51                                  const GURL& url,
52                                  const URLRequestStatus& status,
53                                  int response_code,
54                                  const ResponseCookies& cookies,
55                                  const std::string& data);
56
57  // LinkInfoBarDelegate
58  virtual std::wstring GetMessageTextWithOffset(size_t* link_offset) const;
59  virtual std::wstring GetLinkText() const;
60  virtual SkBitmap* GetIcon() const;
61  virtual bool LinkClicked(WindowOpenDisposition disposition);
62  virtual void InfoBarClosed();
63
64 private:
65  // Sets |state_| to either SUCCEEDED or FAILED depending on the result of the
66  // fetch.
67  void SetStatusFromURLFetch(const GURL& url,
68                             const URLRequestStatus& status,
69                             int response_code);
70
71  // Displays the infobar if all conditions are met (the page has loaded and
72  // the fetch of the alternate URL succeeded).
73  void ShowInfobarIfPossible();
74
75  GURL alternate_nav_url_;
76  scoped_ptr<URLFetcher> fetcher_;
77  NavigationController* controller_;
78  State state_;
79  bool navigated_to_entry_;
80
81  // The TabContents the InfoBarDelegate was added to.
82  TabContents* infobar_contents_;
83
84  NotificationRegistrar registrar_;
85
86  DISALLOW_COPY_AND_ASSIGN(AlternateNavURLFetcher);
87};
88
89#endif  // CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_
90