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