Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013 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. | |
| 4 | |
| 5 #include "chrome/browser/extensions/extension_message_bubble_controller.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/metrics/histogram.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "chrome/browser/extensions/extension_message_bubble.h" | |
| 11 #include "chrome/browser/extensions/extension_prefs.h" | |
| 12 #include "chrome/browser/extensions/extension_service.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/browser_finder.h" | |
| 15 #include "chrome/common/url_constants.h" | |
| 16 #include "content/public/browser/user_metrics.h" | |
| 17 #include "grit/chromium_strings.h" | |
| 18 #include "grit/generated_resources.h" | |
| 19 #include "ui/base/l10n/l10n_util.h" | |
| 20 | |
| 21 namespace extensions { | |
| 22 | |
| 23 //////////////////////////////////////////////////////////////////////////////// | |
| 24 // ExtensionMessageBubbleController | |
| 25 | |
| 26 ExtensionMessageBubbleController::ExtensionMessageBubbleController( | |
| 27 Delegate* delegate, Profile* profile) | |
| 28 : service_(extensions::ExtensionSystem::Get(profile)->extension_service()), | |
| 29 profile_(profile), | |
| 30 user_action_(ACTION_BOUNDARY), | |
| 31 delegate_(delegate), | |
| 32 initialized_(false), | |
| 33 has_notified_(false) { | |
| 34 } | |
| 35 | |
| 36 ExtensionMessageBubbleController::~ExtensionMessageBubbleController() { | |
| 37 } | |
| 38 | |
| 39 bool ExtensionMessageBubbleController::ShouldShow() { | |
| 40 if (has_notified_) | |
| 41 return false; | |
| 42 | |
| 43 ExtensionIdList* list = GetOrCreateExtensionList(); | |
| 44 has_notified_ = true; | |
| 45 return !list->empty(); | |
|
not at google - send to devlin
2013/12/12 21:17:19
this method can be shorter if you do the has_notif
Finnur
2013/12/13 21:29:49
Good point.
| |
| 46 } | |
| 47 | |
| 48 std::vector<string16> | |
| 49 ExtensionMessageBubbleController::GetExtensionList() { | |
| 50 ExtensionIdList* list = GetOrCreateExtensionList(); | |
| 51 if (list->empty()) | |
| 52 return std::vector<string16>(); | |
| 53 | |
| 54 std::vector<string16> return_value; | |
| 55 for (ExtensionIdList::const_iterator it = list->begin(); | |
| 56 it != list->end(); ++it) { | |
| 57 const Extension* extension = service_->GetInstalledExtension(*it); | |
| 58 if (extension) { | |
| 59 return_value.push_back(UTF8ToUTF16(extension->name())); | |
| 60 } else { | |
| 61 return_value.push_back( | |
| 62 ASCIIToUTF16(std::string("(unknown name) ") + *it)); | |
| 63 // TODO(finnur): Add this as a string to the grd, for next milestone. | |
| 64 } | |
| 65 } | |
| 66 return return_value; | |
| 67 } | |
| 68 | |
| 69 void ExtensionMessageBubbleController::Show(ExtensionMessageBubble* bubble) { | |
| 70 // Wire up all the callbacks, to get notified what actions the user took. | |
| 71 base::Closure dismiss_button_callback = | |
| 72 base::Bind(&ExtensionMessageBubbleController::OnBubbleDismiss, | |
| 73 base::Unretained(this)); | |
| 74 base::Closure action_button_callback = | |
| 75 base::Bind(&ExtensionMessageBubbleController::OnBubbleAction, | |
| 76 base::Unretained(this)); | |
| 77 base::Closure link_callback = | |
| 78 base::Bind(&ExtensionMessageBubbleController::OnLinkClicked, | |
| 79 base::Unretained(this)); | |
| 80 bubble->OnActionButtonClicked(action_button_callback); | |
| 81 bubble->OnDismissButtonClicked(dismiss_button_callback); | |
| 82 bubble->OnLinkClicked(link_callback); | |
| 83 | |
| 84 bubble->Show(); | |
| 85 } | |
| 86 | |
| 87 void ExtensionMessageBubbleController::OnBubbleAction() { | |
| 88 DCHECK_EQ(ACTION_BOUNDARY, user_action_); | |
| 89 user_action_ = ACTION_EXECUTE; | |
| 90 | |
| 91 delegate_->LogAction(ACTION_EXECUTE); | |
| 92 delegate_->PerformAction(*GetOrCreateExtensionList()); | |
| 93 AcknowledgeExtensions(); | |
| 94 } | |
| 95 | |
| 96 void ExtensionMessageBubbleController::OnBubbleDismiss() { | |
| 97 DCHECK_EQ(ACTION_BOUNDARY, user_action_); | |
| 98 user_action_ = ACTION_DISMISS; | |
| 99 | |
| 100 delegate_->LogAction(ACTION_DISMISS); | |
| 101 AcknowledgeExtensions(); | |
| 102 } | |
| 103 | |
| 104 void ExtensionMessageBubbleController::OnLinkClicked() { | |
| 105 DCHECK_EQ(ACTION_BOUNDARY, user_action_); | |
| 106 user_action_ = ACTION_LEARN_MORE; | |
| 107 | |
| 108 delegate_->LogAction(ACTION_LEARN_MORE); | |
| 109 Browser* browser = | |
| 110 chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop()); | |
| 111 if (browser) { | |
| 112 browser->OpenURL( | |
| 113 content::OpenURLParams(delegate_->GetLearnMoreUrl(), | |
| 114 content::Referrer(), | |
| 115 NEW_FOREGROUND_TAB, | |
| 116 content::PAGE_TRANSITION_LINK, | |
| 117 false)); | |
| 118 } | |
| 119 AcknowledgeExtensions(); | |
| 120 } | |
| 121 | |
| 122 void ExtensionMessageBubbleController::AcknowledgeExtensions() { | |
| 123 ExtensionIdList* list = GetOrCreateExtensionList(); | |
| 124 for (ExtensionIdList::const_iterator it = list->begin(); | |
| 125 it != list->end(); ++it) | |
| 126 delegate_->AcknowledgeExtension(*it, user_action_); | |
| 127 } | |
| 128 | |
| 129 ExtensionIdList* ExtensionMessageBubbleController::GetOrCreateExtensionList() { | |
| 130 if (!initialized_) { | |
| 131 scoped_ptr<const ExtensionSet> extension_set( | |
| 132 service_->GenerateInstalledExtensionsSet()); | |
| 133 for (ExtensionSet::const_iterator it = extension_set->begin(); | |
| 134 it != extension_set->end(); ++it) { | |
| 135 std::string id = (*it)->id(); | |
| 136 if (!delegate_->ShouldIncludeExtension(id)) | |
| 137 continue; | |
| 138 extension_list_.push_back(id); | |
| 139 } | |
| 140 | |
| 141 delegate_->LogExtensionCount(extension_list_.size()); | |
| 142 initialized_ = true; | |
| 143 } | |
| 144 | |
| 145 return &extension_list_; | |
| 146 } | |
| 147 | |
| 148 } // namespace extensions | |
| OLD | NEW |