1868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 2868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// found in the LICENSE file. 4868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 5868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/notifications/sync_notifier/notification_bitmap_fetcher.h" 6868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 7868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/profiles/profile.h" 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "content/public/browser/browser_thread.h" 9868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "net/url_request/url_fetcher.h" 107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "net/url_request/url_request_status.h" 11868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 12868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)namespace notifier { 13868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NotificationBitmapFetcher::NotificationBitmapFetcher( 15868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const GURL& url, 16868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) NotificationBitmapFetcherDelegate* delegate) 17868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) : url_(url), delegate_(delegate) {} 18868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 19868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)NotificationBitmapFetcher::~NotificationBitmapFetcher() {} 20868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NotificationBitmapFetcher::Start(Profile* profile) { 227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) url_fetcher_.reset( 237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) net::URLFetcher::Create(url_, net::URLFetcher::GET, this)); 247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // The RequestContext is coming from the current profile. 257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // TODO(petewil): Make sure this is the right profile to use. 267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // It seems to work, but we might prefer to use a blank profile with 277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // no cookies. 287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) url_fetcher_->SetRequestContext(profile->GetRequestContext()); 29868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) url_fetcher_->Start(); 30868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 31868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 32868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Methods inherited from URLFetcherDelegate. 33868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 34868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NotificationBitmapFetcher::OnURLFetchComplete( 35868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const net::URLFetcher* source) { 36868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 37868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS) { 39868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) OnDecodeImageFailed(NULL); 40868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) return; 41868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) } 42868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) std::string image_data; 447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) source->GetResponseAsString(&image_data); 45868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Create an ImageDecoder with the data and assign it to the refptr. 46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) image_decoder_ = new ImageDecoder(this, image_data, 47868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) ImageDecoder::DEFAULT_CODEC); 48868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 49868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Call start to begin decoding. The ImageDecoder will call OnImageDecoded 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // with the data when it is done. 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) scoped_refptr<base::MessageLoopProxy> task_runner = 52868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) content::BrowserThread::GetMessageLoopProxyForThread( 53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) content::BrowserThread::UI); 54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) image_decoder_->Start(task_runner); 55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 57868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NotificationBitmapFetcher::OnURLFetchDownloadProgress( 58868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const net::URLFetcher* source, int64 current, int64 total) { 59868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Do nothing here. 60868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)// Methods inherited from ImageDecoder::Delegate. 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NotificationBitmapFetcher::OnImageDecoded( 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ImageDecoder* decoder, const SkBitmap& decoded_image) { 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Make a copy of the bitmap which we pass back to the UI thread. 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) bitmap_.reset(new SkBitmap()); 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) decoded_image.deepCopyTo(bitmap_.get(), decoded_image.getConfig()); 69868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Report success. 71868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) delegate_->OnFetchComplete(url_, bitmap_.get()); 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 73868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 74868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)void NotificationBitmapFetcher::OnDecodeImageFailed( 75868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) const ImageDecoder* decoder) { 76868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 77868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Report failure. 78868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) delegate_->OnFetchComplete(url_, NULL); 79868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} 80868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 81868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)} // namespace notifier 82