Add an extension override bubble and warning box for proxy extensions. (2nd attempt...
[chromium-blink-merge.git] / chrome / browser / ui / views / settings_api_bubble_helper_views.cc
blobc3dc7f48b1018ee279e4324e4236677e53e59690
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 extensions {
24 namespace {
26 void ShowSettingsApiBubble(SettingsApiOverrideType type,
27 const std::string& extension_id,
28 Profile* profile,
29 views::View* anchor_view,
30 views::BubbleBorder::Arrow arrow) {
31 scoped_ptr<SettingsApiBubbleController> settings_api_bubble(
32 new SettingsApiBubbleController(profile, type));
33 if (!settings_api_bubble->ShouldShow(extension_id))
34 return;
36 SettingsApiBubbleController* controller = settings_api_bubble.get();
37 ExtensionMessageBubbleView* bubble_delegate =
38 new ExtensionMessageBubbleView(
39 anchor_view,
40 arrow,
41 settings_api_bubble.PassAs<
42 ExtensionMessageBubbleController>());
43 views::BubbleDelegateView::CreateBubble(bubble_delegate);
44 controller->Show(bubble_delegate);
47 } // namespace
49 void MaybeShowExtensionControlledHomeNotification(Browser* browser) {
50 #if !defined(OS_WIN)
51 return;
52 #endif
54 const Extension* extension =
55 GetExtensionOverridingHomepage(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 GetExtensionOverridingSearchEngine(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