| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/gfx/jpeg_codec.h" | 9 #include "base/gfx/jpeg_codec.h" |
| 10 #include "chrome/browser/profile.h" | 10 #include "chrome/browser/profile.h" |
| 11 #include "chrome/browser/thumbnail_store.h" | 11 #include "chrome/browser/thumbnail_store.h" |
| 12 #include "chrome/common/chrome_switches.h" | 12 #include "chrome/common/chrome_switches.h" |
| 13 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
| 16 #include "third_party/skia/include/core/SkBitmap.h" | 16 #include "third_party/skia/include/core/SkBitmap.h" |
| 17 | 17 |
| 18 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) | 18 DOMUIThumbnailSource::DOMUIThumbnailSource(Profile* profile) |
| 19 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), | 19 : DataSource(chrome::kChromeUIThumbnailPath, MessageLoop::current()), |
| 20 profile_(profile), | 20 profile_(profile), |
| 21 store_(profile->GetThumbnailStore()) { | 21 store_(profile->GetThumbnailStore()) { |
| 22 } | 22 } |
| 23 | 23 |
| 24 DOMUIThumbnailSource::~DOMUIThumbnailSource() { | |
| 25 store_->CancelPendingRequests(pending_requests_); | |
| 26 } | |
| 27 | |
| 28 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, | 24 void DOMUIThumbnailSource::StartDataRequest(const std::string& path, |
| 29 int request_id) { | 25 int request_id) { |
| 30 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) { | 26 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kThumbnailStore)) { |
| 31 RefCountedBytes* data = NULL; | 27 RefCountedBytes* data = NULL; |
| 32 | 28 |
| 33 ThumbnailStore::GetStatus res = store_->GetPageThumbnail(GURL(path), &data); | 29 if (store_->GetPageThumbnail(GURL(path), &data)) { |
| 34 if (res == ThumbnailStore::SUCCESS) { | |
| 35 // Got the thumbnail. | 30 // Got the thumbnail. |
| 36 SendResponse(request_id, data); | 31 SendResponse(request_id, data); |
| 37 } else if (res == ThumbnailStore::FAIL) { | 32 } else { |
| 38 // Don't have the thumbnail so return the default thumbnail. | 33 // Don't have the thumbnail so return the default thumbnail. |
| 39 if (!default_thumbnail_.get()) { | 34 if (!default_thumbnail_.get()) { |
| 40 default_thumbnail_ = new RefCountedBytes; | 35 default_thumbnail_ = new RefCountedBytes; |
| 41 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( | 36 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( |
| 42 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); | 37 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); |
| 43 } | 38 } |
| 44 SendResponse(request_id, default_thumbnail_); | 39 SendResponse(request_id, default_thumbnail_); |
| 45 } else if (res == ThumbnailStore::ASYNC) { | |
| 46 // Getting the redirect list for the url. Will return thumbnail later. | |
| 47 ThumbnailStore::ThumbnailDataCallback* cb = | |
| 48 NewCallback(this, &DOMUIThumbnailSource::ReturnData); | |
| 49 pending_requests_.insert(request_id); | |
| 50 store_->GetPageThumbnailAsync(GURL(path), request_id, cb); | |
| 51 } | 40 } |
| 52 return; | 41 return; |
| 53 } // end --thumbnail-store switch | 42 } // end --thumbnail-store switch |
| 54 | 43 |
| 55 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 44 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 56 if (hs) { | 45 if (hs) { |
| 57 HistoryService::Handle handle = hs->GetPageThumbnail( | 46 HistoryService::Handle handle = hs->GetPageThumbnail( |
| 58 GURL(path), | 47 GURL(path), |
| 59 &cancelable_consumer_, | 48 &cancelable_consumer_, |
| 60 NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable)); | 49 NewCallback(this, &DOMUIThumbnailSource::OnThumbnailDataAvailable)); |
| 61 // Attach the ChromeURLDataManager request ID to the history request. | 50 // Attach the ChromeURLDataManager request ID to the history request. |
| 62 cancelable_consumer_.SetClientData(hs, handle, request_id); | 51 cancelable_consumer_.SetClientData(hs, handle, request_id); |
| 63 } else { | 52 } else { |
| 64 // Tell the caller that no thumbnail is available. | 53 // Tell the caller that no thumbnail is available. |
| 65 SendResponse(request_id, NULL); | 54 SendResponse(request_id, NULL); |
| 66 } | 55 } |
| 67 } | 56 } |
| 68 | 57 |
| 69 void DOMUIThumbnailSource::ReturnData(int request_id, | |
| 70 scoped_refptr<RefCountedBytes> data) { | |
| 71 pending_requests_.erase(request_id); | |
| 72 if (data.get() && !data->data.empty()) { | |
| 73 SendResponse(request_id, data); | |
| 74 } else { | |
| 75 if (!default_thumbnail_.get()) { | |
| 76 default_thumbnail_ = new RefCountedBytes; | |
| 77 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( | |
| 78 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); | |
| 79 } | |
| 80 | |
| 81 SendResponse(request_id, default_thumbnail_); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void DOMUIThumbnailSource::OnThumbnailDataAvailable( | 58 void DOMUIThumbnailSource::OnThumbnailDataAvailable( |
| 86 HistoryService::Handle request_handle, | 59 HistoryService::Handle request_handle, |
| 87 scoped_refptr<RefCountedBytes> data) { | 60 scoped_refptr<RefCountedBytes> data) { |
| 88 HistoryService* hs = | 61 HistoryService* hs = |
| 89 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 62 profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 90 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); | 63 int request_id = cancelable_consumer_.GetClientData(hs, request_handle); |
| 91 // Forward the data along to the networking system. | 64 // Forward the data along to the networking system. |
| 92 if (data.get() && !data->data.empty()) { | 65 if (data.get() && !data->data.empty()) { |
| 93 SendResponse(request_id, data); | 66 SendResponse(request_id, data); |
| 94 } else { | 67 } else { |
| 95 if (!default_thumbnail_.get()) { | 68 if (!default_thumbnail_.get()) { |
| 96 default_thumbnail_ = new RefCountedBytes; | 69 default_thumbnail_ = new RefCountedBytes; |
| 97 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( | 70 ResourceBundle::GetSharedInstance().LoadImageResourceBytes( |
| 98 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); | 71 IDR_DEFAULT_THUMBNAIL, &default_thumbnail_->data); |
| 99 } | 72 } |
| 100 | 73 |
| 101 SendResponse(request_id, default_thumbnail_); | 74 SendResponse(request_id, default_thumbnail_); |
| 102 } | 75 } |
| 103 } | 76 } |
| OLD | NEW |