Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / dev_mode_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/dev_mode_bubble_controller.h"
6
7 #include "base/bind.h"
8 #include "base/lazy_instance.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/extensions/extension_action_manager.h"
13 #include "chrome/browser/extensions/extension_message_bubble.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/extensions/extension_toolbar_model.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/browser.h"
18 #include "chrome/browser/ui/browser_finder.h"
19 #include "chrome/common/chrome_version_info.h"
20 #include "chrome/common/url_constants.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/user_metrics.h"
23 #include "extensions/browser/extension_prefs.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/feature_switch.h"
26 #include "grit/chromium_strings.h"
27 #include "grit/generated_resources.h"
28 #include "ui/base/l10n/l10n_util.h"
29
30 namespace extensions {
31
32 namespace {
33
34 base::LazyInstance<std::set<Profile*> > g_shown_for_profiles =
35     LAZY_INSTANCE_INITIALIZER;
36
37 ////////////////////////////////////////////////////////////////////////////////
38 // DevModeBubbleDelegate
39
40 DevModeBubbleDelegate::DevModeBubbleDelegate(Profile* profile)
41     : profile_(profile),
42       service_(ExtensionSystem::Get(profile)->extension_service()) {}
43
44 DevModeBubbleDelegate::~DevModeBubbleDelegate() {
45 }
46
47 bool DevModeBubbleDelegate::ShouldIncludeExtension(
48     const std::string& extension_id) {
49   const Extension* extension = service_->GetExtensionById(extension_id, false);
50   if (!extension)
51     return false;
52   return DevModeBubbleController::IsDevModeExtension(extension);
53 }
54
55 void DevModeBubbleDelegate::AcknowledgeExtension(
56     const std::string& extension_id,
57     ExtensionMessageBubbleController::BubbleAction user_action) {
58 }
59
60 void DevModeBubbleDelegate::PerformAction(const ExtensionIdList& list) {
61   for (size_t i = 0; i < list.size(); ++i)
62     service_->DisableExtension(list[i], Extension::DISABLE_USER_ACTION);
63 }
64
65 void DevModeBubbleDelegate::OnClose() {
66   ExtensionToolbarModel* toolbar_model = ExtensionToolbarModel::Get(profile_);
67   if (toolbar_model)
68     toolbar_model->StopHighlighting();
69 }
70
71 base::string16 DevModeBubbleDelegate::GetTitle() const {
72   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_TITLE);
73 }
74
75 base::string16 DevModeBubbleDelegate::GetMessageBody() const {
76   return l10n_util::GetStringUTF16(IDS_EXTENSIONS_DISABLE_DEVELOPER_MODE_BODY);
77 }
78
79 base::string16 DevModeBubbleDelegate::GetOverflowText(
80     const base::string16& overflow_count) const {
81   return l10n_util::GetStringFUTF16(
82             IDS_EXTENSIONS_DISABLED_AND_N_MORE,
83             overflow_count);
84 }
85
86 base::string16 DevModeBubbleDelegate::GetLearnMoreLabel() const {
87   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
88 }
89
90 GURL DevModeBubbleDelegate::GetLearnMoreUrl() const {
91   return GURL(chrome::kChromeUIExtensionsURL);
92 }
93
94 base::string16 DevModeBubbleDelegate::GetActionButtonLabel() const {
95   return l10n_util::GetStringUTF16(IDS_DISABLE);
96 }
97
98 base::string16 DevModeBubbleDelegate::GetDismissButtonLabel() const {
99   return l10n_util::GetStringUTF16(IDS_CANCEL);
100 }
101
102 bool DevModeBubbleDelegate::ShouldShowExtensionList() const {
103   return false;
104 }
105
106 void DevModeBubbleDelegate::LogExtensionCount(size_t count) {
107   UMA_HISTOGRAM_COUNTS_100(
108       "DevModeExtensionBubble.ExtensionsInDevModeCount", count);
109 }
110
111 void DevModeBubbleDelegate::LogAction(
112     ExtensionMessageBubbleController::BubbleAction action) {
113   UMA_HISTOGRAM_ENUMERATION(
114       "DevModeExtensionBubble.UserSelection",
115       action, ExtensionMessageBubbleController::ACTION_BOUNDARY);
116 }
117
118 }  // namespace
119
120 ////////////////////////////////////////////////////////////////////////////////
121 // DevModeBubbleController
122
123 // static
124 void DevModeBubbleController::ClearProfileListForTesting() {
125   g_shown_for_profiles.Get().clear();
126 }
127
128 // static
129 bool DevModeBubbleController::IsDevModeExtension(
130     const Extension* extension) {
131   if (!FeatureSwitch::force_dev_mode_highlighting()->IsEnabled()) {
132     if (chrome::VersionInfo::GetChannel() <
133             chrome::VersionInfo::CHANNEL_BETA)
134       return false;
135   }
136   return extension->location() == Manifest::UNPACKED ||
137          extension->location() == Manifest::COMMAND_LINE;
138 }
139
140 DevModeBubbleController::DevModeBubbleController(Profile* profile)
141     : ExtensionMessageBubbleController(new DevModeBubbleDelegate(profile),
142                                        profile),
143       profile_(profile) {}
144
145 DevModeBubbleController::~DevModeBubbleController() {
146 }
147
148 bool DevModeBubbleController::ShouldShow() {
149   return !g_shown_for_profiles.Get().count(profile_->GetOriginalProfile()) &&
150       !GetExtensionList().empty();
151 }
152
153 void DevModeBubbleController::Show(ExtensionMessageBubble* bubble) {
154   g_shown_for_profiles.Get().insert(profile_->GetOriginalProfile());
155   ExtensionMessageBubbleController::Show(bubble);
156 }
157
158 }  // namespace extensions