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