Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / apps / drive / drive_app_converter.cc
blobe1df7c9e24b579a8dd8abea63e1434beea0c8158
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.
5 #include "chrome/browser/apps/drive/drive_app_converter.h"
7 #include <algorithm>
8 #include <set>
10 #include "base/callback_helpers.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/browser/extensions/crx_installer.h"
15 #include "chrome/browser/extensions/install_tracker.h"
16 #include "chrome/browser/image_decoder.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "extensions/browser/extension_system.h"
20 #include "extensions/common/constants.h"
21 #include "net/base/load_flags.h"
22 #include "net/http/http_status_code.h"
23 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_fetcher_delegate.h"
25 #include "net/url_request/url_request_status.h"
26 #include "third_party/skia/include/core/SkBitmap.h"
28 using content::BrowserThread;
30 // IconFetcher downloads |icon_url| using |converter|'s profile. The icon
31 // url is passed from a DriveAppInfo and should follow icon url definition
32 // in Drive API:
33 // https://developers.google.com/drive/v2/reference/apps#resource
34 // Each icon url represents a single image associated with a certain size.
35 class DriveAppConverter::IconFetcher : public net::URLFetcherDelegate,
36 public ImageDecoder::ImageRequest {
37 public:
38 IconFetcher(DriveAppConverter* converter,
39 const GURL& icon_url,
40 int expected_size)
41 : converter_(converter),
42 icon_url_(icon_url),
43 expected_size_(expected_size) {}
44 ~IconFetcher() override {}
46 void Start() {
47 fetcher_ = net::URLFetcher::Create(icon_url_, net::URLFetcher::GET, this);
48 fetcher_->SetRequestContext(converter_->profile_->GetRequestContext());
49 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
50 fetcher_->Start();
53 const GURL& icon_url() const { return icon_url_; }
54 const SkBitmap& icon() const { return icon_; }
56 private:
57 // net::URLFetcherDelegate overrides:
58 void OnURLFetchComplete(const net::URLFetcher* source) override {
59 CHECK_EQ(fetcher_.get(), source);
60 scoped_ptr<net::URLFetcher> fetcher(fetcher_.Pass());
62 if (!fetcher->GetStatus().is_success() ||
63 fetcher->GetResponseCode() != net::HTTP_OK) {
64 converter_->OnIconFetchComplete(this);
65 return;
68 std::string unsafe_icon_data;
69 fetcher->GetResponseAsString(&unsafe_icon_data);
71 ImageDecoder::Start(this, unsafe_icon_data);
74 // ImageDecoder::ImageRequest overrides:
75 void OnImageDecoded(const SkBitmap& decoded_image) override {
76 if (decoded_image.width() == expected_size_)
77 icon_ = decoded_image;
78 converter_->OnIconFetchComplete(this);
81 void OnDecodeImageFailed() override { converter_->OnIconFetchComplete(this); }
83 DriveAppConverter* converter_;
84 const GURL icon_url_;
85 const int expected_size_;
87 scoped_ptr<net::URLFetcher> fetcher_;
88 SkBitmap icon_;
90 DISALLOW_COPY_AND_ASSIGN(IconFetcher);
93 DriveAppConverter::DriveAppConverter(Profile* profile,
94 const drive::DriveAppInfo& drive_app_info,
95 const FinishedCallback& finished_callback)
96 : profile_(profile),
97 drive_app_info_(drive_app_info),
98 extension_(NULL),
99 is_new_install_(false),
100 finished_callback_(finished_callback) {
101 DCHECK(profile_);
104 DriveAppConverter::~DriveAppConverter() {
105 PostInstallCleanUp();
108 void DriveAppConverter::Start() {
109 DCHECK(!IsStarted());
111 if (drive_app_info_.app_name.empty() ||
112 !drive_app_info_.create_url.is_valid()) {
113 base::ResetAndReturn(&finished_callback_).Run(this, false);
114 return;
117 web_app_.title = base::UTF8ToUTF16(drive_app_info_.app_name);
118 web_app_.app_url = drive_app_info_.create_url;
120 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes,
121 extension_misc::kExtensionIconSizes +
122 extension_misc::kNumExtensionIconSizes);
123 std::set<int> pending_sizes;
124 for (size_t i = 0; i < drive_app_info_.app_icons.size(); ++i) {
125 const int icon_size = drive_app_info_.app_icons[i].first;
126 if (allowed_sizes.find(icon_size) == allowed_sizes.end() ||
127 pending_sizes.find(icon_size) != pending_sizes.end()) {
128 continue;
131 pending_sizes.insert(icon_size);
132 const GURL& icon_url = drive_app_info_.app_icons[i].second;
133 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size);
134 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|.
135 fetcher->Start();
138 if (fetchers_.empty())
139 StartInstall();
142 bool DriveAppConverter::IsStarted() const {
143 return !fetchers_.empty() || crx_installer_.get();
146 bool DriveAppConverter::IsInstalling(const std::string& app_id) const {
147 return crx_installer_.get() && crx_installer_->extension() &&
148 crx_installer_->extension()->id() == app_id;
151 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) {
152 const SkBitmap& icon = fetcher->icon();
153 if (!icon.empty() && icon.width() != 0) {
154 WebApplicationInfo::IconInfo icon_info;
155 icon_info.url = fetcher->icon_url();
156 icon_info.data = icon;
157 icon_info.width = icon.width();
158 icon_info.height = icon.height();
159 web_app_.icons.push_back(icon_info);
162 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher));
164 if (fetchers_.empty())
165 StartInstall();
168 void DriveAppConverter::StartInstall() {
169 DCHECK(!crx_installer_.get());
170 crx_installer_ = extensions::CrxInstaller::CreateSilent(
171 extensions::ExtensionSystem::Get(profile_)->extension_service());
172 // The converted url app should not be syncable. Drive apps go with the user's
173 // account and url apps will be created when needed. Syncing those apps could
174 // hit an edge case where the synced url apps become orphans when the user has
175 // corresponding chrome apps.
176 crx_installer_->set_do_not_sync(true);
178 extensions::InstallTracker::Get(profile_)->AddObserver(this);
179 crx_installer_->InstallWebApp(web_app_);
182 void DriveAppConverter::PostInstallCleanUp() {
183 if (!crx_installer_.get())
184 return;
186 extensions::InstallTracker::Get(profile_)->RemoveObserver(this);
187 crx_installer_ = NULL;
190 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id,
191 bool success) {
192 if (!crx_installer_->extension() ||
193 crx_installer_->extension()->id() != extension_id) {
194 return;
197 extension_ = crx_installer_->extension();
198 is_new_install_ = success && crx_installer_->current_version().empty();
199 PostInstallCleanUp();
201 base::ResetAndReturn(&finished_callback_).Run(this, success);
202 // |finished_callback_| could delete this.