| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/app_list/drive/drive_app_converter.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 #include <set> | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chrome/browser/extensions/crx_installer.h" | |
| 14 #include "chrome/browser/extensions/install_tracker.h" | |
| 15 #include "chrome/browser/image_decoder.h" | |
| 16 #include "chrome/browser/profiles/profile.h" | |
| 17 #include "content/public/browser/browser_thread.h" | |
| 18 #include "extensions/browser/extension_system.h" | |
| 19 #include "extensions/common/constants.h" | |
| 20 #include "net/base/load_flags.h" | |
| 21 #include "net/http/http_status_code.h" | |
| 22 #include "net/url_request/url_fetcher.h" | |
| 23 #include "net/url_request/url_fetcher_delegate.h" | |
| 24 #include "net/url_request/url_request_status.h" | |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | |
| 26 | |
| 27 using content::BrowserThread; | |
| 28 | |
| 29 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon | |
| 30 // url is passed from a DriveAppInfo and should follow icon url definition | |
| 31 // in Drive API: | |
| 32 // https://developers.google.com/drive/v2/reference/apps#resource | |
| 33 // Each icon url represents a single image associated with a certain size. | |
| 34 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate, | |
| 35 public ImageDecoder::Delegate { | |
| 36 public: | |
| 37 IconFetcher(DriveAppConverter* converter, | |
| 38 const GURL& icon_url, | |
| 39 int expected_size) | |
| 40 : converter_(converter), | |
| 41 icon_url_(icon_url), | |
| 42 expected_size_(expected_size) {} | |
| 43 virtual ~IconFetcher() { | |
| 44 if (image_decoder_.get()) | |
| 45 image_decoder_->set_delegate(NULL); | |
| 46 } | |
| 47 | |
| 48 void Start() { | |
| 49 fetcher_.reset( | |
| 50 net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this)); | |
| 51 fetcher_->SetRequestContext(converter_->profile_->GetRequestContext()); | |
| 52 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 53 fetcher_->Start(); | |
| 54 } | |
| 55 | |
| 56 const GURL& icon_url() const { return icon_url_; } | |
| 57 const SkBitmap& icon() const { return icon_; } | |
| 58 | |
| 59 private: | |
| 60 // net::URLFetcherDelegate overrides: | |
| 61 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE { | |
| 62 CHECK_EQ(fetcher_.get(), source); | |
| 63 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass()); | |
| 64 | |
| 65 if (!fetcher->GetStatus().is_success() || | |
| 66 fetcher->GetResponseCode() != net::HTTP_OK) { | |
| 67 converter_->OnIconFetchComplete(this); | |
| 68 return; | |
| 69 } | |
| 70 | |
| 71 std::string unsafe_icon_data; | |
| 72 fetcher->GetResponseAsString(&unsafe_icon_data); | |
| 73 | |
| 74 image_decoder_ = | |
| 75 new ImageDecoder(this, unsafe_icon_data, ImageDecoder::DEFAULT_CODEC); | |
| 76 image_decoder_->Start( | |
| 77 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::UI)); | |
| 78 } | |
| 79 | |
| 80 // ImageDecoder::Delegate overrides: | |
| 81 virtual void OnImageDecoded(const ImageDecoder* decoder, | |
| 82 const SkBitmap& decoded_image) OVERRIDE { | |
| 83 if (decoded_image.width() == expected_size_) | |
| 84 icon_ = decoded_image; | |
| 85 converter_->OnIconFetchComplete(this); | |
| 86 } | |
| 87 | |
| 88 virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE { | |
| 89 converter_->OnIconFetchComplete(this); | |
| 90 } | |
| 91 | |
| 92 DriveAppConverter* converter_; | |
| 93 const GURL icon_url_; | |
| 94 const int expected_size_; | |
| 95 | |
| 96 scoped_ptr<net::URLFetcher> fetcher_; | |
| 97 scoped_refptr<ImageDecoder> image_decoder_; | |
| 98 SkBitmap icon_; | |
| 99 | |
| 100 DISALLOW_COPY_AND_ASSIGN(IconFetcher); | |
| 101 }; | |
| 102 | |
| 103 DriveAppConverter::DriveAppConverter(Profile* profile, | |
| 104 const drive::DriveAppInfo& app_info, | |
| 105 const FinishedCallback& finished_callback) | |
| 106 : profile_(profile), | |
| 107 app_info_(app_info), | |
| 108 app_(NULL), | |
| 109 finished_callback_(finished_callback) { | |
| 110 DCHECK(profile_); | |
| 111 } | |
| 112 | |
| 113 DriveAppConverter::~DriveAppConverter() { | |
| 114 PostInstallCleanUp(); | |
| 115 } | |
| 116 | |
| 117 void DriveAppConverter::Start() { | |
| 118 if (app_info_.app_name.empty() || | |
| 119 !app_info_.create_url.is_valid()) { | |
| 120 finished_callback_.Run(this, false); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 web_app_.title = base::UTF8ToUTF16(app_info_.app_name); | |
| 125 web_app_.app_url = app_info_.create_url; | |
| 126 | |
| 127 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes, | |
| 128 extension_misc::kExtensionIconSizes + | |
| 129 extension_misc::kNumExtensionIconSizes); | |
| 130 std::set<int> pending_sizes; | |
| 131 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) { | |
| 132 const int icon_size = app_info_.app_icons[i].first; | |
| 133 if (allowed_sizes.find(icon_size) == allowed_sizes.end() || | |
| 134 pending_sizes.find(icon_size) != pending_sizes.end()) { | |
| 135 continue; | |
| 136 } | |
| 137 | |
| 138 pending_sizes.insert(icon_size); | |
| 139 const GURL& icon_url = app_info_.app_icons[i].second; | |
| 140 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size); | |
| 141 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|. | |
| 142 fetcher->Start(); | |
| 143 } | |
| 144 | |
| 145 if (fetchers_.empty()) | |
| 146 StartInstall(); | |
| 147 } | |
| 148 | |
| 149 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) { | |
| 150 const SkBitmap& icon = fetcher->icon(); | |
| 151 if (!icon.empty() && icon.width() != 0) { | |
| 152 WebApplicationInfo::IconInfo icon_info; | |
| 153 icon_info.url = fetcher->icon_url(); | |
| 154 icon_info.data = icon; | |
| 155 icon_info.width = icon.width(); | |
| 156 icon_info.height = icon.height(); | |
| 157 web_app_.icons.push_back(icon_info); | |
| 158 } | |
| 159 | |
| 160 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); | |
| 161 | |
| 162 if (fetchers_.empty()) | |
| 163 StartInstall(); | |
| 164 } | |
| 165 | |
| 166 void DriveAppConverter::StartInstall() { | |
| 167 DCHECK(!crx_installer_); | |
| 168 crx_installer_ = extensions::CrxInstaller::CreateSilent( | |
| 169 extensions::ExtensionSystem::Get(profile_)->extension_service()); | |
| 170 extensions::InstallTracker::Get(profile_)->AddObserver(this); | |
| 171 crx_installer_->InstallWebApp(web_app_); | |
| 172 } | |
| 173 | |
| 174 void DriveAppConverter::PostInstallCleanUp() { | |
| 175 if (!crx_installer_) | |
| 176 return; | |
| 177 | |
| 178 extensions::InstallTracker::Get(profile_)->RemoveObserver(this); | |
| 179 crx_installer_ = NULL; | |
| 180 } | |
| 181 | |
| 182 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id, | |
| 183 bool success) { | |
| 184 if (!crx_installer_->extension() || | |
| 185 crx_installer_->extension()->id() != extension_id) { | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 app_ = crx_installer_->extension(); | |
| 190 finished_callback_.Run(this, success); | |
| 191 | |
| 192 PostInstallCleanUp(); | |
| 193 } | |
| OLD | NEW |