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