OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/apps/drive/drive_app_converter.h" | 5 #include "chrome/browser/apps/drive/drive_app_converter.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
26 | 26 |
27 using content::BrowserThread; | 27 using content::BrowserThread; |
28 | 28 |
29 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon | 29 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon |
30 // url is passed from a DriveAppInfo and should follow icon url definition | 30 // url is passed from a DriveAppInfo and should follow icon url definition |
31 // in Drive API: | 31 // in Drive API: |
32 // https://developers.google.com/drive/v2/reference/apps#resource | 32 // https://developers.google.com/drive/v2/reference/apps#resource |
33 // Each icon url represents a single image associated with a certain size. | 33 // Each icon url represents a single image associated with a certain size. |
34 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate, | 34 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate, |
35 public ImageDecoder::Delegate { | 35 public ImageDecoder::ImageRequest { |
36 public: | 36 public: |
37 IconFetcher(DriveAppConverter* converter, | 37 IconFetcher(DriveAppConverter* converter, |
38 const GURL& icon_url, | 38 const GURL& icon_url, |
39 int expected_size) | 39 int expected_size) |
40 : converter_(converter), | 40 : ImageRequest( |
| 41 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)), |
| 42 converter_(converter), |
41 icon_url_(icon_url), | 43 icon_url_(icon_url), |
42 expected_size_(expected_size) {} | 44 expected_size_(expected_size) {} |
43 ~IconFetcher() override { | 45 ~IconFetcher() override {} |
44 if (image_decoder_.get()) | |
45 image_decoder_->set_delegate(NULL); | |
46 } | |
47 | 46 |
48 void Start() { | 47 void Start() { |
49 fetcher_.reset( | 48 fetcher_.reset( |
50 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this)); | 49 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this)); |
51 fetcher_->SetRequestContext(converter_->profile_->GetRequestContext()); | 50 fetcher_->SetRequestContext(converter_->profile_->GetRequestContext()); |
52 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 51 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
53 fetcher_->Start(); | 52 fetcher_->Start(); |
54 } | 53 } |
55 | 54 |
56 const GURL& icon_url() const { return icon_url_; } | 55 const GURL& icon_url() const { return icon_url_; } |
57 const SkBitmap& icon() const { return icon_; } | 56 const SkBitmap& icon() const { return icon_; } |
58 | 57 |
59 private: | 58 private: |
60 // net::URLFetcherDelegate overrides: | 59 // net::URLFetcherDelegate overrides: |
61 void OnURLFetchComplete(const net::URLFetcher* source) override { | 60 void OnURLFetchComplete(const net::URLFetcher* source) override { |
62 CHECK_EQ(fetcher_.get(), source); | 61 CHECK_EQ(fetcher_.get(), source); |
63 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); | 62 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); |
64 | 63 |
65 if (!fetcher->GetStatus().is_success() || | 64 if (!fetcher->GetStatus().is_success() || |
66 fetcher->GetResponseCode() != net::HTTP_OK) { | 65 fetcher->GetResponseCode() != net::HTTP_OK) { |
67 converter_->OnIconFetchComplete(this); | 66 converter_->OnIconFetchComplete(this); |
68 return; | 67 return; |
69 } | 68 } |
70 | 69 |
71 std::string unsafe_icon_data; | 70 std::string unsafe_icon_data; |
72 fetcher->GetResponseAsString(&unsafe_icon_data); | 71 fetcher->GetResponseAsString(&unsafe_icon_data); |
73 | 72 |
74 image_decoder_ = | 73 ImageDecoder::Start(this, unsafe_icon_data, ImageDecoder::DEFAULT_CODEC, |
75 new ImageDecoder(this, unsafe_icon_data, ImageDecoder::DEFAULT_CODEC); | 74 false); |
76 image_decoder_->Start( | |
77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)); | |
78 } | 75 } |
79 | 76 |
80 // ImageDecoder::Delegate overrides: | 77 // ImageDecoder::ImageRequest overrides: |
81 void OnImageDecoded(const ImageDecoder* decoder, | 78 void OnImageDecoded(const SkBitmap& decoded_image) override { |
82 const SkBitmap& decoded_image) override { | |
83 if (decoded_image.width() == expected_size_) | 79 if (decoded_image.width() == expected_size_) |
84 icon_ = decoded_image; | 80 icon_ = decoded_image; |
85 converter_->OnIconFetchComplete(this); | 81 converter_->OnIconFetchComplete(this); |
86 } | 82 } |
87 | 83 |
88 void OnDecodeImageFailed(const ImageDecoder* decoder) override { | 84 void OnDecodeImageFailed() override { converter_->OnIconFetchComplete(this); } |
89 converter_->OnIconFetchComplete(this); | |
90 } | |
91 | 85 |
92 DriveAppConverter* converter_; | 86 DriveAppConverter* converter_; |
93 const GURL icon_url_; | 87 const GURL icon_url_; |
94 const int expected_size_; | 88 const int expected_size_; |
95 | 89 |
96 scoped_ptr<net::URLFetcher> fetcher_; | 90 scoped_ptr<net::URLFetcher> fetcher_; |
97 scoped_refptr<ImageDecoder> image_decoder_; | |
98 SkBitmap icon_; | 91 SkBitmap icon_; |
99 | 92 |
100 DISALLOW_COPY_AND_ASSIGN(IconFetcher); | 93 DISALLOW_COPY_AND_ASSIGN(IconFetcher); |
101 }; | 94 }; |
102 | 95 |
103 DriveAppConverter::DriveAppConverter(Profile* profile, | 96 DriveAppConverter::DriveAppConverter(Profile* profile, |
104 const drive::DriveAppInfo& drive_app_info, | 97 const drive::DriveAppInfo& drive_app_info, |
105 const FinishedCallback& finished_callback) | 98 const FinishedCallback& finished_callback) |
106 : profile_(profile), | 99 : profile_(profile), |
107 drive_app_info_(drive_app_info), | 100 drive_app_info_(drive_app_info), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 return; | 197 return; |
205 } | 198 } |
206 | 199 |
207 extension_ = crx_installer_->extension(); | 200 extension_ = crx_installer_->extension(); |
208 is_new_install_ = success && crx_installer_->current_version().empty(); | 201 is_new_install_ = success && crx_installer_->current_version().empty(); |
209 PostInstallCleanUp(); | 202 PostInstallCleanUp(); |
210 | 203 |
211 finished_callback_.Run(this, success); | 204 finished_callback_.Run(this, success); |
212 // |finished_callback_| could delete this. | 205 // |finished_callback_| could delete this. |
213 } | 206 } |
OLD | NEW |