alternate_nav_url_fetcher.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
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
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  virtual ~AlternateNavURLFetcher();
43
44  State state() const { return state_; }
45
46 private:
47  // NotificationObserver
48  virtual void Observe(NotificationType type,
49                       const NotificationSource& source,
50                       const NotificationDetails& details);
51
52  // URLFetcher::Delegate
53  virtual void OnURLFetchComplete(const URLFetcher* source,
54                                  const GURL& url,
55                                  const net::URLRequestStatus& status,
56                                  int response_code,
57                                  const ResponseCookies& cookies,
58                                  const std::string& data);
59
60  // LinkInfoBarDelegate
61  virtual SkBitmap* GetIcon() const;
62  virtual Type GetInfoBarType() const;
63  virtual string16 GetMessageTextWithOffset(size_t* link_offset) const;
64  virtual string16 GetLinkText() const;
65  virtual bool LinkClicked(WindowOpenDisposition disposition);
66  virtual void InfoBarClosed();
67
68  // Sets |state_| to either SUCCEEDED or FAILED depending on the result of the
69  // fetch.
70  void SetStatusFromURLFetch(const GURL& url,
71                             const net::URLRequestStatus& status,
72                             int response_code);
73
74  // Displays the infobar if all conditions are met (the page has loaded and
75  // the fetch of the alternate URL succeeded).
76  void ShowInfobarIfPossible();
77
78  GURL alternate_nav_url_;
79  scoped_ptr<URLFetcher> fetcher_;
80  NavigationController* controller_;
81  State state_;
82  bool navigated_to_entry_;
83
84  // The TabContents the InfoBarDelegate was added to.
85  TabContents* infobar_contents_;
86
87  NotificationRegistrar registrar_;
88
89  DISALLOW_COPY_AND_ASSIGN(AlternateNavURLFetcher);
90};
91
92#endif  // CHROME_BROWSER_ALTERNATE_NAV_URL_FETCHER_H_
93