| 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/ui/app_list/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" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/browser/extensions/crx_installer.h" | 13 #include "chrome/browser/extensions/crx_installer.h" |
| 14 #include "chrome/browser/extensions/install_tracker.h" | 14 #include "chrome/browser/extensions/install_tracker.h" |
| 15 #include "chrome/browser/image_decoder.h" | 15 #include "chrome/browser/image_decoder.h" |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 | 99 |
| 100 DISALLOW_COPY_AND_ASSIGN(IconFetcher); | 100 DISALLOW_COPY_AND_ASSIGN(IconFetcher); |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 DriveAppConverter::DriveAppConverter(Profile* profile, | 103 DriveAppConverter::DriveAppConverter(Profile* profile, |
| 104 const drive::DriveAppInfo& app_info, | 104 const drive::DriveAppInfo& app_info, |
| 105 const FinishedCallback& finished_callback) | 105 const FinishedCallback& finished_callback) |
| 106 : profile_(profile), | 106 : profile_(profile), |
| 107 app_info_(app_info), | 107 app_info_(app_info), |
| 108 app_(NULL), | 108 app_(NULL), |
| 109 is_new_install_(false), |
| 109 finished_callback_(finished_callback) { | 110 finished_callback_(finished_callback) { |
| 110 DCHECK(profile_); | 111 DCHECK(profile_); |
| 111 } | 112 } |
| 112 | 113 |
| 113 DriveAppConverter::~DriveAppConverter() { | 114 DriveAppConverter::~DriveAppConverter() { |
| 114 PostInstallCleanUp(); | 115 PostInstallCleanUp(); |
| 115 } | 116 } |
| 116 | 117 |
| 117 void DriveAppConverter::Start() { | 118 void DriveAppConverter::Start() { |
| 118 if (app_info_.app_name.empty() || | 119 DCHECK(!IsStarted()); |
| 119 !app_info_.create_url.is_valid()) { | 120 |
| 121 if (app_info_.app_name.empty() || !app_info_.create_url.is_valid()) { |
| 120 finished_callback_.Run(this, false); | 122 finished_callback_.Run(this, false); |
| 121 return; | 123 return; |
| 122 } | 124 } |
| 123 | 125 |
| 124 web_app_.title = base::UTF8ToUTF16(app_info_.app_name); | 126 web_app_.title = base::UTF8ToUTF16(app_info_.app_name); |
| 125 web_app_.app_url = app_info_.create_url; | 127 web_app_.app_url = app_info_.create_url; |
| 126 | 128 |
| 127 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes, | 129 const std::set<int> allowed_sizes(extension_misc::kExtensionIconSizes, |
| 128 extension_misc::kExtensionIconSizes + | 130 extension_misc::kExtensionIconSizes + |
| 129 extension_misc::kNumExtensionIconSizes); | 131 extension_misc::kNumExtensionIconSizes); |
| 130 std::set<int> pending_sizes; | 132 std::set<int> pending_sizes; |
| 131 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) { | 133 for (size_t i = 0; i < app_info_.app_icons.size(); ++i) { |
| 132 const int icon_size = app_info_.app_icons[i].first; | 134 const int icon_size = app_info_.app_icons[i].first; |
| 133 if (allowed_sizes.find(icon_size) == allowed_sizes.end() || | 135 if (allowed_sizes.find(icon_size) == allowed_sizes.end() || |
| 134 pending_sizes.find(icon_size) != pending_sizes.end()) { | 136 pending_sizes.find(icon_size) != pending_sizes.end()) { |
| 135 continue; | 137 continue; |
| 136 } | 138 } |
| 137 | 139 |
| 138 pending_sizes.insert(icon_size); | 140 pending_sizes.insert(icon_size); |
| 139 const GURL& icon_url = app_info_.app_icons[i].second; | 141 const GURL& icon_url = app_info_.app_icons[i].second; |
| 140 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size); | 142 IconFetcher* fetcher = new IconFetcher(this, icon_url, icon_size); |
| 141 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|. | 143 fetchers_.push_back(fetcher); // Pass ownership to |fetchers|. |
| 142 fetcher->Start(); | 144 fetcher->Start(); |
| 143 } | 145 } |
| 144 | 146 |
| 145 if (fetchers_.empty()) | 147 if (fetchers_.empty()) |
| 146 StartInstall(); | 148 StartInstall(); |
| 147 } | 149 } |
| 148 | 150 |
| 151 bool DriveAppConverter::IsStarted() const { |
| 152 return !fetchers_.empty() || crx_installer_; |
| 153 } |
| 154 |
| 149 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) { | 155 void DriveAppConverter::OnIconFetchComplete(const IconFetcher* fetcher) { |
| 150 const SkBitmap& icon = fetcher->icon(); | 156 const SkBitmap& icon = fetcher->icon(); |
| 151 if (!icon.empty() && icon.width() != 0) { | 157 if (!icon.empty() && icon.width() != 0) { |
| 152 WebApplicationInfo::IconInfo icon_info; | 158 WebApplicationInfo::IconInfo icon_info; |
| 153 icon_info.url = fetcher->icon_url(); | 159 icon_info.url = fetcher->icon_url(); |
| 154 icon_info.data = icon; | 160 icon_info.data = icon; |
| 155 icon_info.width = icon.width(); | 161 icon_info.width = icon.width(); |
| 156 icon_info.height = icon.height(); | 162 icon_info.height = icon.height(); |
| 157 web_app_.icons.push_back(icon_info); | 163 web_app_.icons.push_back(icon_info); |
| 158 } | 164 } |
| 159 | 165 |
| 160 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); | 166 fetchers_.erase(std::find(fetchers_.begin(), fetchers_.end(), fetcher)); |
| 161 | 167 |
| 162 if (fetchers_.empty()) | 168 if (fetchers_.empty()) |
| 163 StartInstall(); | 169 StartInstall(); |
| 164 } | 170 } |
| 165 | 171 |
| 166 void DriveAppConverter::StartInstall() { | 172 void DriveAppConverter::StartInstall() { |
| 167 DCHECK(!crx_installer_); | 173 DCHECK(!crx_installer_); |
| 168 crx_installer_ = extensions::CrxInstaller::CreateSilent( | 174 crx_installer_ = extensions::CrxInstaller::CreateSilent( |
| 169 extensions::ExtensionSystem::Get(profile_)->extension_service()); | 175 extensions::ExtensionSystem::Get(profile_)->extension_service()); |
| 176 crx_installer_->set_creation_flags(extensions::Extension::FROM_BOOKMARK); |
| 177 // The converted url app should not be syncable. Drive apps go with the user's |
| 178 // account and url apps will be created when needed. Syncing those apps could |
| 179 // hit an edge case where the synced url apps become orphans when the user has |
| 180 // corresponding chrome apps. |
| 181 crx_installer_->set_do_not_sync(true); |
| 182 |
| 170 extensions::InstallTracker::Get(profile_)->AddObserver(this); | 183 extensions::InstallTracker::Get(profile_)->AddObserver(this); |
| 171 crx_installer_->InstallWebApp(web_app_); | 184 crx_installer_->InstallWebApp(web_app_); |
| 172 } | 185 } |
| 173 | 186 |
| 174 void DriveAppConverter::PostInstallCleanUp() { | 187 void DriveAppConverter::PostInstallCleanUp() { |
| 175 if (!crx_installer_) | 188 if (!crx_installer_) |
| 176 return; | 189 return; |
| 177 | 190 |
| 178 extensions::InstallTracker::Get(profile_)->RemoveObserver(this); | 191 extensions::InstallTracker::Get(profile_)->RemoveObserver(this); |
| 179 crx_installer_ = NULL; | 192 crx_installer_ = NULL; |
| 180 } | 193 } |
| 181 | 194 |
| 182 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id, | 195 void DriveAppConverter::OnFinishCrxInstall(const std::string& extension_id, |
| 183 bool success) { | 196 bool success) { |
| 184 if (!crx_installer_->extension() || | 197 if (!crx_installer_->extension() || |
| 185 crx_installer_->extension()->id() != extension_id) { | 198 crx_installer_->extension()->id() != extension_id) { |
| 186 return; | 199 return; |
| 187 } | 200 } |
| 188 | 201 |
| 189 app_ = crx_installer_->extension(); | 202 app_ = crx_installer_->extension(); |
| 203 is_new_install_ = success && crx_installer_->current_version().empty(); |
| 204 PostInstallCleanUp(); |
| 205 |
| 190 finished_callback_.Run(this, success); | 206 finished_callback_.Run(this, success); |
| 191 | 207 // |finished_callback_| could delete this. |
| 192 PostInstallCleanUp(); | |
| 193 } | 208 } |
| OLD | NEW |