Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_message_bubble_controller.cc
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_service.h"
12 #include "chrome/browser/ui/browser.h"
13 #include "chrome/browser/ui/browser_finder.h"
14 #include "chrome/common/url_constants.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "extensions/browser/extension_prefs.h"
17 #include "extensions/browser/extension_system.h"
18 #include "grit/chromium_strings.h"
19 #include "grit/generated_resources.h"
20 #include "ui/base/l10n/l10n_util.h"
21
22 namespace extensions {
23
24 ////////////////////////////////////////////////////////////////////////////////
25 // ExtensionMessageBubbleController::Delegate
26
27 ExtensionMessageBubbleController::Delegate::Delegate() {
28 }
29
30 ExtensionMessageBubbleController::Delegate::~Delegate() {
31 }
32
33 ////////////////////////////////////////////////////////////////////////////////
34 // ExtensionMessageBubbleController
35
36 ExtensionMessageBubbleController::ExtensionMessageBubbleController(
37     Delegate* delegate, Profile* profile)
38     : service_(extensions::ExtensionSystem::Get(profile)->extension_service()),
39       profile_(profile),
40       user_action_(ACTION_BOUNDARY),
41       delegate_(delegate),
42       initialized_(false) {
43 }
44
45 ExtensionMessageBubbleController::~ExtensionMessageBubbleController() {
46 }
47
48 std::vector<base::string16>
49 ExtensionMessageBubbleController::GetExtensionList() {
50   ExtensionIdList* list = GetOrCreateExtensionList();
51   if (list->empty())
52     return std::vector<base::string16>();
53
54   std::vector<base::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(base::UTF8ToUTF16(extension->name()));
60     } else {
61       return_value.push_back(
62           base::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 const ExtensionIdList& ExtensionMessageBubbleController::GetExtensionIdList() {
70   return *GetOrCreateExtensionList();
71 }
72
73 void ExtensionMessageBubbleController::Show(ExtensionMessageBubble* bubble) {
74   // Wire up all the callbacks, to get notified what actions the user took.
75   base::Closure dismiss_button_callback =
76       base::Bind(&ExtensionMessageBubbleController::OnBubbleDismiss,
77       base::Unretained(this));
78   base::Closure action_button_callback =
79       base::Bind(&ExtensionMessageBubbleController::OnBubbleAction,
80       base::Unretained(this));
81   base::Closure link_callback =
82       base::Bind(&ExtensionMessageBubbleController::OnLinkClicked,
83       base::Unretained(this));
84   bubble->OnActionButtonClicked(action_button_callback);
85   bubble->OnDismissButtonClicked(dismiss_button_callback);
86   bubble->OnLinkClicked(link_callback);
87
88   bubble->Show();
89 }
90
91 void ExtensionMessageBubbleController::OnBubbleAction() {
92   DCHECK_EQ(ACTION_BOUNDARY, user_action_);
93   user_action_ = ACTION_EXECUTE;
94
95   delegate_->LogAction(ACTION_EXECUTE);
96   delegate_->PerformAction(*GetOrCreateExtensionList());
97   AcknowledgeExtensions();
98 }
99
100 void ExtensionMessageBubbleController::OnBubbleDismiss() {
101   DCHECK_EQ(ACTION_BOUNDARY, user_action_);
102   user_action_ = ACTION_DISMISS;
103
104   delegate_->LogAction(ACTION_DISMISS);
105   AcknowledgeExtensions();
106 }
107
108 void ExtensionMessageBubbleController::OnLinkClicked() {
109   DCHECK_EQ(ACTION_BOUNDARY, user_action_);
110   user_action_ = ACTION_LEARN_MORE;
111
112   delegate_->LogAction(ACTION_LEARN_MORE);
113   Browser* browser =
114       chrome::FindBrowserWithProfile(profile_, chrome::GetActiveDesktop());
115   if (browser) {
116     browser->OpenURL(
117         content::OpenURLParams(delegate_->GetLearnMoreUrl(),
118                                content::Referrer(),
119                                NEW_FOREGROUND_TAB,
120                                content::PAGE_TRANSITION_LINK,
121                                false));
122   }
123   AcknowledgeExtensions();
124 }
125
126 void ExtensionMessageBubbleController::AcknowledgeExtensions() {
127   ExtensionIdList* list = GetOrCreateExtensionList();
128   for (ExtensionIdList::const_iterator it = list->begin();
129        it != list->end(); ++it)
130     delegate_->AcknowledgeExtension(*it, user_action_);
131 }
132
133 ExtensionIdList* ExtensionMessageBubbleController::GetOrCreateExtensionList() {
134   if (!service_)
135     return &extension_list_;  // Can occur during testing.
136
137   if (!initialized_) {
138     scoped_ptr<const ExtensionSet> extension_set(
139         service_->GenerateInstalledExtensionsSet());
140     for (ExtensionSet::const_iterator it = extension_set->begin();
141          it != extension_set->end(); ++it) {
142       std::string id = (*it)->id();
143       if (!delegate_->ShouldIncludeExtension(id))
144         continue;
145       extension_list_.push_back(id);
146     }
147
148     delegate_->LogExtensionCount(extension_list_.size());
149     initialized_ = true;
150   }
151
152   return &extension_list_;
153 }
154
155 }  // namespace extensions