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#include "chrome/browser/favicon_service.h"
6
7#include "chrome/browser/history/history.h"
8#include "chrome/browser/history/history_backend.h"
9#include "chrome/browser/profiles/profile.h"
10#include "chrome/browser/ui/webui/chrome_web_ui_factory.h"
11#include "chrome/common/url_constants.h"
12
13FaviconService::FaviconService(Profile* profile) : profile_(profile) {
14}
15
16FaviconService::Handle FaviconService::GetFavicon(
17    const GURL& icon_url,
18    history::IconType icon_type,
19    CancelableRequestConsumerBase* consumer,
20    FaviconDataCallback* callback) {
21  GetFaviconRequest* request = new GetFaviconRequest(callback);
22  AddRequest(request, consumer);
23  HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
24  if (hs)
25    hs->GetFavicon(request, icon_url, icon_type);
26  else
27    ForwardEmptyResultAsync(request);
28  return request->handle();
29}
30
31FaviconService::Handle FaviconService::UpdateFaviconMappingAndFetch(
32    const GURL& page_url,
33    const GURL& icon_url,
34    history::IconType icon_type,
35    CancelableRequestConsumerBase* consumer,
36    FaviconService::FaviconDataCallback* callback) {
37  GetFaviconRequest* request = new GetFaviconRequest(callback);
38  AddRequest(request, consumer);
39  HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
40  if (hs)
41    hs->UpdateFaviconMappingAndFetch(request, page_url, icon_url, icon_type);
42  else
43    ForwardEmptyResultAsync(request);
44  return request->handle();
45}
46
47FaviconService::Handle FaviconService::GetFaviconForURL(
48    const GURL& page_url,
49    int icon_types,
50    CancelableRequestConsumerBase* consumer,
51    FaviconDataCallback* callback) {
52  GetFaviconRequest* request = new GetFaviconRequest(callback);
53  AddRequest(request, consumer);
54  FaviconService::Handle handle = request->handle();
55  if (page_url.SchemeIs(chrome::kChromeUIScheme) ||
56      page_url.SchemeIs(chrome::kExtensionScheme)) {
57    ChromeWebUIFactory::GetInstance()->GetFaviconForURL(
58        profile_, request, page_url);
59  } else {
60    HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
61    if (hs)
62      hs->GetFaviconForURL(request, page_url, icon_types);
63    else
64      ForwardEmptyResultAsync(request);
65  }
66  return handle;
67}
68
69void FaviconService::SetFaviconOutOfDateForPage(const GURL& page_url) {
70  HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
71  if (hs)
72    hs->SetFaviconOutOfDateForPage(page_url);
73}
74
75void FaviconService::SetImportedFavicons(
76    const std::vector<history::ImportedFaviconUsage>& favicon_usage) {
77  HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
78  if (hs)
79    hs->SetImportedFavicons(favicon_usage);
80}
81
82void FaviconService::SetFavicon(const GURL& page_url,
83                                const GURL& icon_url,
84                                const std::vector<unsigned char>& image_data,
85                                history::IconType icon_type) {
86  HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
87  if (hs)
88    hs->SetFavicon(page_url, icon_url, image_data, icon_type);
89}
90
91FaviconService::~FaviconService() {
92}
93
94void FaviconService::ForwardEmptyResultAsync(GetFaviconRequest* request) {
95  request->ForwardResultAsync(FaviconDataCallback::TupleType(
96      request->handle(), history::FaviconData()));
97}
98