Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/installable/installable_manager.h" | 5 #include "chrome/browser/installable/installable_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 10 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 54 if (size.width() >= kIconMinimumSizeInPx && | 54 if (size.width() >= kIconMinimumSizeInPx && |
| 55 size.height() >= kIconMinimumSizeInPx) { | 55 size.height() >= kIconMinimumSizeInPx) { |
| 56 return true; | 56 return true; |
| 57 } | 57 } |
| 58 } | 58 } |
| 59 } | 59 } |
| 60 | 60 |
| 61 return false; | 61 return false; |
| 62 } | 62 } |
| 63 | 63 |
| 64 // Returns true if |params| specifies a full PWA check. | |
| 65 bool IsParamsForPwaCheck(const InstallableParams& params) { | |
| 66 return params.check_installable && params.fetch_valid_primary_icon; | |
| 67 } | |
| 68 | |
| 64 } // namespace | 69 } // namespace |
| 65 | 70 |
| 66 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InstallableManager); | 71 DEFINE_WEB_CONTENTS_USER_DATA_KEY(InstallableManager); |
| 67 | 72 |
| 68 struct InstallableManager::ManifestProperty { | 73 struct InstallableManager::ManifestProperty { |
| 69 InstallableStatusCode error = NO_ERROR_DETECTED; | 74 InstallableStatusCode error = NO_ERROR_DETECTED; |
| 70 GURL url; | 75 GURL url; |
| 71 content::Manifest manifest; | 76 content::Manifest manifest; |
| 72 bool fetched = false; | 77 bool fetched = false; |
| 73 }; | 78 }; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 87 InstallableStatusCode error = NO_ERROR_DETECTED; | 92 InstallableStatusCode error = NO_ERROR_DETECTED; |
| 88 GURL url; | 93 GURL url; |
| 89 std::unique_ptr<SkBitmap> icon; | 94 std::unique_ptr<SkBitmap> icon; |
| 90 bool fetched; | 95 bool fetched; |
| 91 | 96 |
| 92 private: | 97 private: |
| 93 // This class contains a std::unique_ptr and therefore must be move-only. | 98 // This class contains a std::unique_ptr and therefore must be move-only. |
| 94 DISALLOW_COPY_AND_ASSIGN(IconProperty); | 99 DISALLOW_COPY_AND_ASSIGN(IconProperty); |
| 95 }; | 100 }; |
| 96 | 101 |
| 97 | |
| 98 InstallableManager::InstallableManager(content::WebContents* web_contents) | 102 InstallableManager::InstallableManager(content::WebContents* web_contents) |
| 99 : content::WebContentsObserver(web_contents), | 103 : content::WebContentsObserver(web_contents), |
| 100 manifest_(new ManifestProperty()), | 104 manifest_(new ManifestProperty()), |
| 101 installable_(new InstallableProperty()), | 105 installable_(new InstallableProperty()), |
| 106 page_status_(InstallableMetrics::NOT_STARTED), | |
| 107 menu_open_count_(0), | |
| 108 menu_item_count_(0), | |
| 102 is_active_(false), | 109 is_active_(false), |
| 103 weak_factory_(this) { } | 110 is_pwa_check_complete_(false), |
| 111 weak_factory_(this) {} | |
| 104 | 112 |
| 105 InstallableManager::~InstallableManager() = default; | 113 InstallableManager::~InstallableManager() = default; |
| 106 | 114 |
| 107 // static | 115 // static |
| 108 bool InstallableManager::IsContentSecure(content::WebContents* web_contents) { | 116 bool InstallableManager::IsContentSecure(content::WebContents* web_contents) { |
| 109 if (!web_contents) | 117 if (!web_contents) |
| 110 return false; | 118 return false; |
| 111 | 119 |
| 112 // Whitelist localhost. Check the VisibleURL to match what the | 120 // Whitelist localhost. Check the VisibleURL to match what the |
| 113 // SecurityStateTabHelper looks at. | 121 // SecurityStateTabHelper looks at. |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 133 // Return immediately if we're already working on a task. The new task will be | 141 // Return immediately if we're already working on a task. The new task will be |
| 134 // looked at once the current task is finished. | 142 // looked at once the current task is finished. |
| 135 tasks_.push_back({params, callback}); | 143 tasks_.push_back({params, callback}); |
| 136 if (is_active_) | 144 if (is_active_) |
| 137 return; | 145 return; |
| 138 | 146 |
| 139 is_active_ = true; | 147 is_active_ = true; |
| 140 StartNextTask(); | 148 StartNextTask(); |
| 141 } | 149 } |
| 142 | 150 |
| 151 void InstallableManager::RecordMenuOpenHistogram() { | |
| 152 if (is_pwa_check_complete_) | |
| 153 InstallableMetrics::RecordMenuOpenHistogram(page_status_); | |
| 154 else | |
| 155 ++menu_open_count_; | |
| 156 } | |
| 157 | |
| 158 void InstallableManager::RecordMenuItemAddToHomescreenHistogram() { | |
| 159 if (is_pwa_check_complete_) | |
| 160 InstallableMetrics::RecordMenuItemAddToHomescreenHistogram(page_status_); | |
| 161 else | |
| 162 ++menu_item_count_; | |
| 163 } | |
| 164 | |
| 165 void InstallableManager::RecordQueuedMetricsOnTaskCompletion( | |
| 166 const InstallableParams& params, | |
| 167 bool check_passed) { | |
| 168 // Don't do anything if we've already finished the PWA check or if this check | |
| 169 // was not for the full PWA params. Once a full check is completed, metrics | |
| 170 // will be directly recorded in Record*Histogram since | |
| 171 // |is_pwa_check_complete_| will be true. | |
| 172 if (is_pwa_check_complete_ || !IsParamsForPwaCheck(params)) | |
| 173 return; | |
| 174 | |
| 175 is_pwa_check_complete_ = true; | |
| 176 page_status_ = check_passed ? InstallableMetrics::COMPLETE_PWA | |
| 177 : InstallableMetrics::COMPLETE_NON_PWA; | |
| 178 | |
| 179 // Compute what the status would have been for any queued calls to | |
| 180 // Record*Histogram, and record appropriately. | |
| 181 InstallableMetrics::InstallabilityCheckStatus prev_status = | |
| 182 check_passed ? InstallableMetrics::IN_PROGRESS_PWA | |
| 183 : InstallableMetrics::IN_PROGRESS_NON_PWA; | |
| 184 for (; menu_open_count_ > 0; --menu_open_count_) | |
| 185 InstallableMetrics::RecordMenuOpenHistogram(prev_status); | |
| 186 | |
| 187 for (; menu_item_count_ > 0; --menu_item_count_) | |
| 188 InstallableMetrics::RecordMenuItemAddToHomescreenHistogram(prev_status); | |
| 189 | |
| 190 DCHECK_EQ(0, menu_open_count_); | |
| 191 DCHECK_EQ(0, menu_item_count_); | |
|
pkotwicz
2017/03/31 05:07:44
Personally, I would remove the DCHECKs. In my expe
benwells
2017/03/31 05:35:28
+1, these DCHECKS don't add much value.
dominickn
2017/04/03 00:06:46
Done.
| |
| 192 } | |
| 193 | |
| 143 InstallableManager::IconParams InstallableManager::ParamsForPrimaryIcon( | 194 InstallableManager::IconParams InstallableManager::ParamsForPrimaryIcon( |
| 144 const InstallableParams& params) const { | 195 const InstallableParams& params) const { |
| 145 return std::make_tuple(params.ideal_primary_icon_size_in_px, | 196 return std::make_tuple(params.ideal_primary_icon_size_in_px, |
| 146 params.minimum_primary_icon_size_in_px, | 197 params.minimum_primary_icon_size_in_px, |
| 147 IconPurpose::ANY); | 198 IconPurpose::ANY); |
| 148 } | 199 } |
| 149 | 200 |
| 150 InstallableManager::IconParams InstallableManager::ParamsForBadgeIcon( | 201 InstallableManager::IconParams InstallableManager::ParamsForBadgeIcon( |
| 151 const InstallableParams& params) const { | 202 const InstallableParams& params) const { |
| 152 return std::make_tuple(params.ideal_badge_icon_size_in_px, | 203 return std::make_tuple(params.ideal_badge_icon_size_in_px, |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 225 (!params.fetch_valid_badge_icon || | 276 (!params.fetch_valid_badge_icon || |
| 226 IsIconFetched(ParamsForBadgeIcon(params))); | 277 IsIconFetched(ParamsForBadgeIcon(params))); |
| 227 } | 278 } |
| 228 | 279 |
| 229 void InstallableManager::Reset() { | 280 void InstallableManager::Reset() { |
| 230 // Prevent any outstanding callbacks to or from this object from being called. | 281 // Prevent any outstanding callbacks to or from this object from being called. |
| 231 weak_factory_.InvalidateWeakPtrs(); | 282 weak_factory_.InvalidateWeakPtrs(); |
| 232 tasks_.clear(); | 283 tasks_.clear(); |
| 233 icons_.clear(); | 284 icons_.clear(); |
| 234 | 285 |
| 286 // We may have reset prior to completion, in which case menu_open_count_ or | |
|
pkotwicz
2017/03/31 05:07:44
Nit: |menu_open_count_| and |menu_item_count_| wit
dominickn
2017/04/03 00:06:46
Done.
| |
| 287 // menu_item_count_ will be nonzero. If we completed, then | |
| 288 // these values cannot be anything except 0. | |
| 289 page_status_ = InstallableMetrics::NOT_STARTED; | |
| 290 is_pwa_check_complete_ = false; | |
| 291 | |
| 292 for (; menu_open_count_ > 0; --menu_open_count_) { | |
| 293 InstallableMetrics::RecordMenuOpenHistogram( | |
| 294 InstallableMetrics::STOPPED_BEFORE_COMPLETION); | |
| 295 } | |
| 296 | |
| 297 for (; menu_item_count_ > 0; --menu_item_count_) { | |
| 298 InstallableMetrics::RecordMenuItemAddToHomescreenHistogram( | |
| 299 InstallableMetrics::STOPPED_BEFORE_COMPLETION); | |
| 300 } | |
| 301 | |
| 302 DCHECK_EQ(0, menu_open_count_); | |
| 303 DCHECK_EQ(0, menu_item_count_); | |
| 304 | |
| 235 manifest_.reset(new ManifestProperty()); | 305 manifest_.reset(new ManifestProperty()); |
| 236 installable_.reset(new InstallableProperty()); | 306 installable_.reset(new InstallableProperty()); |
| 237 | 307 |
| 238 is_active_ = false; | 308 is_active_ = false; |
| 239 } | 309 } |
| 240 | 310 |
| 241 void InstallableManager::SetManifestDependentTasksComplete() { | 311 void InstallableManager::SetManifestDependentTasksComplete() { |
| 242 DCHECK(!tasks_.empty()); | 312 DCHECK(!tasks_.empty()); |
| 243 const InstallableParams& params = tasks_[0].first; | 313 const InstallableParams& params = tasks_[0].first; |
| 244 | 314 |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 285 DCHECK(is_active_); | 355 DCHECK(is_active_); |
| 286 WorkOnTask(); | 356 WorkOnTask(); |
| 287 } | 357 } |
| 288 | 358 |
| 289 void InstallableManager::WorkOnTask() { | 359 void InstallableManager::WorkOnTask() { |
| 290 DCHECK(!tasks_.empty()); | 360 DCHECK(!tasks_.empty()); |
| 291 const Task& task = tasks_[0]; | 361 const Task& task = tasks_[0]; |
| 292 const InstallableParams& params = task.first; | 362 const InstallableParams& params = task.first; |
| 293 | 363 |
| 294 InstallableStatusCode code = GetErrorCode(params); | 364 InstallableStatusCode code = GetErrorCode(params); |
| 295 if (code != NO_ERROR_DETECTED || IsComplete(params)) { | 365 bool check_passed = (code == NO_ERROR_DETECTED); |
| 366 if (!check_passed || IsComplete(params)) { | |
| 367 RecordQueuedMetricsOnTaskCompletion(params, check_passed); | |
| 296 RunCallback(task, code); | 368 RunCallback(task, code); |
| 297 tasks_.erase(tasks_.begin()); | 369 tasks_.erase(tasks_.begin()); |
| 298 StartNextTask(); | 370 StartNextTask(); |
| 299 return; | 371 return; |
| 300 } | 372 } |
| 301 | 373 |
| 302 if (!manifest_->fetched) { | 374 if (!manifest_->fetched) { |
| 303 FetchManifest(); | 375 FetchManifest(); |
| 304 } else if (params.check_installable && !installable_->fetched) { | 376 } else if (params.check_installable && !installable_->fetched) { |
| 305 CheckInstallable(); | 377 CheckInstallable(); |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 499 return manifest_->url; | 571 return manifest_->url; |
| 500 } | 572 } |
| 501 | 573 |
| 502 const content::Manifest& InstallableManager::manifest() const { | 574 const content::Manifest& InstallableManager::manifest() const { |
| 503 return manifest_->manifest; | 575 return manifest_->manifest; |
| 504 } | 576 } |
| 505 | 577 |
| 506 bool InstallableManager::is_installable() const { | 578 bool InstallableManager::is_installable() const { |
| 507 return installable_->installable; | 579 return installable_->installable; |
| 508 } | 580 } |
| OLD | NEW |