Check USB device path access when prompting users to select a device.
[chromium-blink-merge.git] / chrome / browser / gpu / three_d_api_observer.cc
blob785e706f9556e463d86ab63bf75d0d787e67854e
1 // Copyright 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/gpu/three_d_api_observer.h"
7 #include "base/metrics/histogram.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/tab_contents/tab_util.h"
10 #include "chrome/grit/generated_resources.h"
11 #include "components/infobars/core/confirm_infobar_delegate.h"
12 #include "components/infobars/core/infobar.h"
13 #include "content/public/browser/gpu_data_manager.h"
14 #include "grit/components_strings.h"
15 #include "grit/theme_resources.h"
16 #include "ui/base/l10n/l10n_util.h"
19 // ThreeDAPIInfoBarDelegate ---------------------------------------------------
21 class ThreeDAPIInfoBarDelegate : public ConfirmInfoBarDelegate {
22 public:
23 // Creates a 3D API infobar and delegate and adds the infobar to
24 // |infobar_service|.
25 static void Create(InfoBarService* infobar_service,
26 const GURL& url,
27 content::ThreeDAPIType requester);
29 private:
30 enum DismissalHistogram {
31 IGNORED,
32 RELOADED,
33 CLOSED_WITHOUT_ACTION,
34 DISMISSAL_MAX
37 ThreeDAPIInfoBarDelegate(const GURL& url, content::ThreeDAPIType requester);
38 ~ThreeDAPIInfoBarDelegate() override;
40 // ConfirmInfoBarDelegate:
41 int GetIconID() const override;
42 bool EqualsDelegate(infobars::InfoBarDelegate* delegate) const override;
43 ThreeDAPIInfoBarDelegate* AsThreeDAPIInfoBarDelegate() override;
44 base::string16 GetMessageText() const override;
45 base::string16 GetButtonLabel(InfoBarButton button) const override;
46 bool Accept() override;
47 bool Cancel() override;
48 base::string16 GetLinkText() const override;
49 bool LinkClicked(WindowOpenDisposition disposition) override;
51 GURL url_;
52 content::ThreeDAPIType requester_;
53 // Basically indicates whether the infobar was displayed at all, or
54 // was a temporary instance thrown away by the InfobarService.
55 mutable bool message_text_queried_;
56 bool action_taken_;
58 DISALLOW_COPY_AND_ASSIGN(ThreeDAPIInfoBarDelegate);
61 // static
62 void ThreeDAPIInfoBarDelegate::Create(InfoBarService* infobar_service,
63 const GURL& url,
64 content::ThreeDAPIType requester) {
65 if (!infobar_service)
66 return; // NULL for apps.
67 infobar_service->AddInfoBar(
68 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
69 new ThreeDAPIInfoBarDelegate(url, requester))));
72 ThreeDAPIInfoBarDelegate::ThreeDAPIInfoBarDelegate(
73 const GURL& url,
74 content::ThreeDAPIType requester)
75 : ConfirmInfoBarDelegate(),
76 url_(url),
77 requester_(requester),
78 message_text_queried_(false),
79 action_taken_(false) {
82 ThreeDAPIInfoBarDelegate::~ThreeDAPIInfoBarDelegate() {
83 if (message_text_queried_ && !action_taken_) {
84 UMA_HISTOGRAM_ENUMERATION("GPU.ThreeDAPIInfoBarDismissal",
85 CLOSED_WITHOUT_ACTION, DISMISSAL_MAX);
89 int ThreeDAPIInfoBarDelegate::GetIconID() const {
90 return IDR_INFOBAR_3D_BLOCKED;
93 bool ThreeDAPIInfoBarDelegate::EqualsDelegate(
94 infobars::InfoBarDelegate* delegate) const {
95 // For the time being, if a given web page is actually using both
96 // WebGL and Pepper 3D and both APIs are blocked, just leave the
97 // first infobar up. If the user selects "try again", both APIs will
98 // be unblocked and the web page reload will succeed.
99 return !!delegate->AsThreeDAPIInfoBarDelegate();
102 ThreeDAPIInfoBarDelegate*
103 ThreeDAPIInfoBarDelegate::AsThreeDAPIInfoBarDelegate() {
104 return this;
107 base::string16 ThreeDAPIInfoBarDelegate::GetMessageText() const {
108 message_text_queried_ = true;
110 base::string16 api_name;
111 switch (requester_) {
112 case content::THREE_D_API_TYPE_WEBGL:
113 api_name = l10n_util::GetStringUTF16(IDS_3D_APIS_WEBGL_NAME);
114 break;
115 case content::THREE_D_API_TYPE_PEPPER_3D:
116 api_name = l10n_util::GetStringUTF16(IDS_3D_APIS_PEPPER_3D_NAME);
117 break;
120 return l10n_util::GetStringFUTF16(IDS_3D_APIS_BLOCKED_TEXT,
121 api_name);
124 base::string16 ThreeDAPIInfoBarDelegate::GetButtonLabel(
125 InfoBarButton button) const {
126 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
127 IDS_3D_APIS_BLOCKED_OK_BUTTON_LABEL :
128 IDS_3D_APIS_BLOCKED_TRY_AGAIN_BUTTON_LABEL);
131 bool ThreeDAPIInfoBarDelegate::Accept() {
132 action_taken_ = true;
133 UMA_HISTOGRAM_ENUMERATION("GPU.ThreeDAPIInfoBarDismissal", IGNORED,
134 DISMISSAL_MAX);
135 return true;
138 bool ThreeDAPIInfoBarDelegate::Cancel() {
139 action_taken_ = true;
140 UMA_HISTOGRAM_ENUMERATION("GPU.ThreeDAPIInfoBarDismissal", RELOADED,
141 DISMISSAL_MAX);
142 content::GpuDataManager::GetInstance()->UnblockDomainFrom3DAPIs(url_);
143 InfoBarService::WebContentsFromInfoBar(infobar())->GetController().Reload(
144 true);
145 return true;
148 base::string16 ThreeDAPIInfoBarDelegate::GetLinkText() const {
149 return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
152 bool ThreeDAPIInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) {
153 InfoBarService::WebContentsFromInfoBar(infobar())->OpenURL(
154 content::OpenURLParams(
155 GURL("https://support.google.com/chrome/?p=ib_webgl"),
156 content::Referrer(),
157 (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
158 ui::PAGE_TRANSITION_LINK, false));
159 return false;
163 // ThreeDAPIObserver ----------------------------------------------------------
165 ThreeDAPIObserver::ThreeDAPIObserver() {
166 content::GpuDataManager::GetInstance()->AddObserver(this);
169 ThreeDAPIObserver::~ThreeDAPIObserver() {
170 content::GpuDataManager::GetInstance()->RemoveObserver(this);
173 void ThreeDAPIObserver::DidBlock3DAPIs(const GURL& url,
174 int render_process_id,
175 int render_view_id,
176 content::ThreeDAPIType requester) {
177 content::WebContents* web_contents = tab_util::GetWebContentsByID(
178 render_process_id, render_view_id);
179 if (!web_contents)
180 return;
181 ThreeDAPIInfoBarDelegate::Create(
182 InfoBarService::FromWebContents(web_contents), url, requester);