[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / ui / views / settings_api_bubble_helper_views.cc
blob67199cdb782e7a079c5d2c7e0a4ddb2457f45f5f
1 // Copyright (c) 2014 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.
5 #include "chrome/browser/ui/views/settings_api_bubble_helper_views.h"
7 #include "chrome/browser/extensions/ntp_overridden_bubble_controller.h"
8 #include "chrome/browser/extensions/settings_api_bubble_controller.h"
9 #include "chrome/browser/extensions/settings_api_helpers.h"
10 #include "chrome/browser/ui/browser_finder.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/browser/ui/views/extensions/extension_message_bubble_view.h"
13 #include "chrome/browser/ui/views/frame/browser_view.h"
14 #include "chrome/browser/ui/views/settings_api_bubble_helper_views.h"
15 #include "chrome/browser/ui/views/toolbar/home_button.h"
16 #include "chrome/browser/ui/views/toolbar/toolbar_view.h"
17 #include "chrome/common/extensions/manifest_handlers/settings_overrides_handler.h"
18 #include "chrome/common/url_constants.h"
19 #include "content/public/browser/browser_url_handler.h"
20 #include "content/public/browser/navigation_entry.h"
22 namespace {
24 void ShowSettingsApiBubble(extensions::SettingsApiOverrideType type,
25 const std::string& extension_id,
26 Profile* profile,
27 views::View* anchor_view,
28 views::BubbleBorder::Arrow arrow) {
29 scoped_ptr<extensions::SettingsApiBubbleController> settings_api_bubble(
30 new extensions::SettingsApiBubbleController(profile, type));
31 if (!settings_api_bubble->ShouldShow(extension_id))
32 return;
34 extensions::SettingsApiBubbleController* controller =
35 settings_api_bubble.get();
36 extensions::ExtensionMessageBubbleView* bubble_delegate =
37 new extensions::ExtensionMessageBubbleView(
38 anchor_view,
39 arrow,
40 settings_api_bubble.PassAs<
41 extensions::ExtensionMessageBubbleController>());
42 views::BubbleDelegateView::CreateBubble(bubble_delegate);
43 controller->Show(bubble_delegate);
46 } // namespace
48 namespace extensions {
50 void MaybeShowExtensionControlledHomeNotification(Browser* browser) {
51 #if !defined(OS_WIN)
52 return;
53 #endif
55 const Extension* extension = OverridesHomepage(browser->profile(), NULL);
56 if (extension) {
57 // The bubble will try to anchor itself against the home button
58 views::View* anchor_view = BrowserView::GetBrowserViewForBrowser(browser)->
59 toolbar()->home_button();
60 ShowSettingsApiBubble(BUBBLE_TYPE_HOME_PAGE,
61 extension->id(),
62 browser->profile(),
63 anchor_view,
64 views::BubbleBorder::TOP_LEFT);
68 void MaybeShowExtensionControlledSearchNotification(
69 Profile* profile,
70 content::WebContents* web_contents,
71 const AutocompleteMatch& match) {
72 #if !defined(OS_WIN)
73 return;
74 #endif
76 if (AutocompleteMatch::IsSearchType(match.type) &&
77 match.type != AutocompleteMatchType::SEARCH_OTHER_ENGINE) {
78 const Extension* extension =
79 OverridesSearchEngine(profile, NULL);
80 if (extension) {
81 ToolbarView* toolbar =
82 BrowserView::GetBrowserViewForBrowser(
83 chrome::FindBrowserWithWebContents(web_contents))->toolbar();
84 ShowSettingsApiBubble(BUBBLE_TYPE_SEARCH_ENGINE,
85 extension->id(),
86 profile,
87 toolbar->app_menu(),
88 views::BubbleBorder::TOP_RIGHT);
93 void MaybeShowExtensionControlledNewTabPage(
94 Browser* browser, content::WebContents* web_contents) {
95 #if !defined(OS_WIN)
96 return;
97 #endif
99 content::NavigationEntry* entry =
100 web_contents->GetController().GetActiveEntry();
101 if (!entry)
102 return;
103 GURL active_url = entry->GetURL();
104 if (!active_url.SchemeIs("chrome-extension"))
105 return; // Not a URL that we care about.
107 // See if the current active URL matches a transformed NewTab URL.
108 GURL ntp_url(chrome::kChromeUINewTabURL);
109 bool ignored_param;
110 content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary(
111 &ntp_url,
112 web_contents->GetBrowserContext(),
113 &ignored_param);
114 if (ntp_url != active_url)
115 return; // Not being overridden by an extension.
117 scoped_ptr<NtpOverriddenBubbleController> ntp_overridden_bubble(
118 new NtpOverriddenBubbleController(browser->profile()));
119 if (!ntp_overridden_bubble->ShouldShow(ntp_url.host()))
120 return;
122 NtpOverriddenBubbleController* controller = ntp_overridden_bubble.get();
123 ExtensionMessageBubbleView* bubble_delegate =
124 new ExtensionMessageBubbleView(
125 BrowserView::GetBrowserViewForBrowser(browser)->toolbar()->app_menu(),
126 views::BubbleBorder::TOP_RIGHT,
127 ntp_overridden_bubble.PassAs<
128 ExtensionMessageBubbleController>());
129 views::BubbleDelegateView::CreateBubble(bubble_delegate);
130 controller->Show(bubble_delegate);
133 } // namespace extensions