| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" | 5 #include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" |
| 6 | 6 |
| 7 #include "app/resource_bundle.h" | 7 #include "app/resource_bundle.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/history/top_sites.h" | 10 #include "chrome/browser/history/top_sites.h" |
| 11 #include "chrome/common/url_constants.h" | 11 #include "chrome/common/url_constants.h" |
| 12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 13 #include "grit/theme_resources.h" | 13 #include "grit/theme_resources.h" |
| 14 | 14 |
| 15 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) | 15 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) |
| 16 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), | 16 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), |
| 17 profile_(profile) { | 17 profile_(profile) { |
| 18 if (history::TopSites::IsEnabled()) { | 18 // Set TopSites now as Profile isn't thread safe. |
| 19 // Set TopSites now as Profile isn't thread safe. | 19 top_sites_ = profile_->GetTopSites(); |
| 20 top_sites_ = profile_->GetTopSites(); | |
| 21 } | |
| 22 } | 20 } |
| 23 | 21 |
| 24 DOMUIThumbnailSource::~DOMUIThumbnailSource() { | 22 DOMUIThumbnailSource::~DOMUIThumbnailSource() { |
| 25 } | 23 } |
| 26 | 24 |
| 27 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, | 25 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, |
| 28 bool is_off_the_record, | 26 bool is_off_the_record, |
| 29 int request_id) { | 27 int request_id) { |
| 30 if (top_sites_.get()) { | 28 scoped_refptr<RefCountedBytes> data; |
| 31 scoped_refptr<RefCountedBytes> data; | 29 if (top_sites_->GetPageThumbnail(GURL(path), &data)) { |
| 32 if (top_sites_->GetPageThumbnail(GURL(path), &data)) { | 30 // We have the thumbnail. |
| 33 // We have the thumbnail. | 31 SendResponse(request_id, data.get()); |
| 34 SendResponse(request_id, data.get()); | |
| 35 } else { | |
| 36 SendDefaultThumbnail(request_id); | |
| 37 } | |
| 38 return; | |
| 39 } | |
| 40 | |
| 41 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | |
| 42 if (hs) { | |
| 43 HistoryService::Handle handle = hs->GetPageThumbnail( | |
| 44 GURL(path), | |
| 45 &cancelable_consumer_, | |
| 46 NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable)); | |
| 47 // Attach the ChromeURLDataManager request ID to the history request. | |
| 48 cancelable_consumer_.SetClientData(hs, handle, request_id); | |
| 49 } else { | 32 } else { |
| 50 // Tell the caller that no thumbnail is available. | 33 SendDefaultThumbnail(request_id); |
| 51 SendResponse(request_id, NULL); | |
| 52 } | 34 } |
| 53 } | 35 } |
| 54 | 36 |
| 55 std::string DOMUIThumbnailSource::GetMimeType(const std::string&) const { | 37 std::string DOMUIThumbnailSource::GetMimeType(const std::string&) const { |
| 56 // We need to explicitly return a mime type, otherwise if the user tries to | 38 // We need to explicitly return a mime type, otherwise if the user tries to |
| 57 // drag the image they get no extension. | 39 // drag the image they get no extension. |
| 58 return "image/png"; | 40 return "image/png"; |
| 59 } | 41 } |
| 60 | 42 |
| 61 MessageLoop* DOMUIThumbnailSource::MessageLoopForRequestPath( | 43 MessageLoop* DOMUIThumbnailSource::MessageLoopForRequestPath( |
| 62 const std::string& path) const { | 44 const std::string& path) const { |
| 63 // TopSites can be accessed from the IO thread. | 45 // TopSites can be accessed from the IO thread. |
| 64 return top_sites_.get() ? NULL : DataSource::MessageLoopForRequestPath(path); | 46 return top_sites_.get() ? NULL : DataSource::MessageLoopForRequestPath(path); |
| 65 } | 47 } |
| 66 | 48 |
| 67 void DOMUIThumbnailSource::SendDefaultThumbnail(int request_id) { | 49 void DOMUIThumbnailSource::SendDefaultThumbnail(int request_id) { |
| 68 // Use placeholder thumbnail. | 50 // Use placeholder thumbnail. |
| 69 if (!default_thumbnail_.get()) { | 51 if (!default_thumbnail_.get()) { |
| 70 default_thumbnail_ = | 52 default_thumbnail_ = |
| 71 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( | 53 ResourceBundle::GetSharedInstance().LoadDataResourceBytes( |
| 72 IDR_DEFAULT_THUMBNAIL); | 54 IDR_DEFAULT_THUMBNAIL); |
| 73 } | 55 } |
| 74 SendResponse(request_id, default_thumbnail_); | 56 SendResponse(request_id, default_thumbnail_); |
| 75 } | 57 } |
| 76 | |
| 77 void DOMUIThumbnailSource::OnThumbnailDataAvailable( | |
| 78 HistoryService::Handle request_handle, | |
| 79 scoped_refptr<RefCountedBytes> data) { | |
| 80 int request_id = cancelable_consumer_.GetClientDataForCurrentRequest(); | |
| 81 // Forward the data along to the networking system. | |
| 82 if (data.get() && !data->data.empty()) { | |
| 83 SendResponse(request_id, data); | |
| 84 } else { | |
| 85 SendDefaultThumbnail(request_id); | |
| 86 } | |
| 87 } | |
| OLD | NEW |