| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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/three_d_api_observer.h" |
| 6 |
| 7 #include "chrome/browser/api/infobars/confirm_infobar_delegate.h" |
| 8 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 9 #include "content/public/browser/gpu_data_manager.h" |
| 10 #include "content/public/common/three_d_api_types.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 |
| 14 DEFINE_WEB_CONTENTS_USER_DATA_KEY(ThreeDAPIObserver) |
| 15 |
| 16 // ---------------------------------------------------------------------- |
| 17 // ThreeDAPIInfoBarDelegate |
| 18 |
| 19 // TODO(kbr): write a "learn more" article about the issues associated |
| 20 // with client 3D APIs and GPU resets. |
| 21 |
| 22 class ThreeDAPIInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 23 public: |
| 24 static void Show( |
| 25 content::WebContents* web_contents, |
| 26 const GURL& url); |
| 27 |
| 28 ThreeDAPIInfoBarDelegate( |
| 29 InfoBarTabHelper* helper, |
| 30 const GURL& url, |
| 31 content::ThreeDAPIType requester); |
| 32 virtual ~ThreeDAPIInfoBarDelegate(); |
| 33 |
| 34 // InfoBarDelegate: |
| 35 virtual bool EqualsDelegate(InfoBarDelegate* delegate) const OVERRIDE; |
| 36 virtual ThreeDAPIInfoBarDelegate* AsThreeDAPIInfoBarDelegate() OVERRIDE; |
| 37 |
| 38 // ConfirmInfoBarDelegate: |
| 39 virtual string16 GetMessageText() const OVERRIDE; |
| 40 virtual int GetButtons() const OVERRIDE; |
| 41 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; |
| 42 virtual bool Accept() OVERRIDE; |
| 43 virtual bool Cancel() OVERRIDE; |
| 44 virtual string16 GetLinkText() const OVERRIDE; |
| 45 virtual bool LinkClicked(WindowOpenDisposition disposition) OVERRIDE; |
| 46 virtual gfx::Image* GetIcon() const OVERRIDE; |
| 47 |
| 48 private: |
| 49 GURL url_; |
| 50 content::ThreeDAPIType requester_; |
| 51 |
| 52 DISALLOW_COPY_AND_ASSIGN(ThreeDAPIInfoBarDelegate); |
| 53 }; |
| 54 |
| 55 ThreeDAPIInfoBarDelegate::ThreeDAPIInfoBarDelegate( |
| 56 InfoBarTabHelper* helper, |
| 57 const GURL& url, |
| 58 content::ThreeDAPIType requester) |
| 59 : ConfirmInfoBarDelegate(helper), |
| 60 url_(url), |
| 61 requester_(requester) { |
| 62 } |
| 63 |
| 64 ThreeDAPIInfoBarDelegate::~ThreeDAPIInfoBarDelegate() { |
| 65 } |
| 66 |
| 67 bool ThreeDAPIInfoBarDelegate::EqualsDelegate(InfoBarDelegate* delegate) const { |
| 68 ThreeDAPIInfoBarDelegate* three_d_delegate = |
| 69 delegate->AsThreeDAPIInfoBarDelegate(); |
| 70 // For the time being, if a given web page is actually using both |
| 71 // WebGL and Pepper 3D and both APIs are blocked, just leave the |
| 72 // first infobar up. If the user selects "try again", both APIs will |
| 73 // be unblocked and the web page reload will succeed. |
| 74 return (three_d_delegate != NULL); |
| 75 } |
| 76 |
| 77 ThreeDAPIInfoBarDelegate* |
| 78 ThreeDAPIInfoBarDelegate::AsThreeDAPIInfoBarDelegate() { |
| 79 return this; |
| 80 } |
| 81 |
| 82 string16 ThreeDAPIInfoBarDelegate::GetMessageText() const { |
| 83 string16 api_name; |
| 84 switch (requester_) { |
| 85 case content::THREE_D_API_TYPE_WEBGL: |
| 86 api_name = l10n_util::GetStringUTF16(IDS_3D_APIS_WEBGL_NAME); |
| 87 break; |
| 88 case content::THREE_D_API_TYPE_PEPPER_3D: |
| 89 api_name = l10n_util::GetStringUTF16(IDS_3D_APIS_PEPPER_3D_NAME); |
| 90 break; |
| 91 } |
| 92 |
| 93 return l10n_util::GetStringFUTF16(IDS_3D_APIS_BLOCKED_TEXT, |
| 94 api_name); |
| 95 } |
| 96 |
| 97 int ThreeDAPIInfoBarDelegate::GetButtons() const { |
| 98 return BUTTON_OK | BUTTON_CANCEL; |
| 99 } |
| 100 |
| 101 string16 ThreeDAPIInfoBarDelegate::GetButtonLabel( |
| 102 InfoBarButton button) const { |
| 103 switch (button) { |
| 104 case BUTTON_OK: |
| 105 return l10n_util::GetStringUTF16(IDS_3D_APIS_BLOCKED_OK_BUTTON_LABEL); |
| 106 case BUTTON_CANCEL: |
| 107 return l10n_util::GetStringUTF16( |
| 108 IDS_3D_APIS_BLOCKED_TRY_AGAIN_BUTTON_LABEL); |
| 109 default: |
| 110 NOTREACHED(); |
| 111 return string16(); |
| 112 } |
| 113 } |
| 114 |
| 115 bool ThreeDAPIInfoBarDelegate::Accept() { |
| 116 // TODO(kbr): add UMA stats. |
| 117 return true; |
| 118 } |
| 119 |
| 120 bool ThreeDAPIInfoBarDelegate::Cancel() { |
| 121 // TODO(kbr): add UMA stats. |
| 122 content::GpuDataManager::GetInstance()->UnblockDomainFrom3DAPIs(url_); |
| 123 owner()->GetWebContents()->GetController().Reload(true); |
| 124 return true; |
| 125 } |
| 126 |
| 127 string16 ThreeDAPIInfoBarDelegate::GetLinkText() const { |
| 128 return string16(); |
| 129 } |
| 130 |
| 131 bool ThreeDAPIInfoBarDelegate::LinkClicked( |
| 132 WindowOpenDisposition disposition) { |
| 133 return false; |
| 134 } |
| 135 |
| 136 gfx::Image* ThreeDAPIInfoBarDelegate::GetIcon() const { |
| 137 return NULL; |
| 138 } |
| 139 |
| 140 |
| 141 // ---------------------------------------------------------------------- |
| 142 // ThreeDAPIObserver |
| 143 |
| 144 ThreeDAPIObserver::ThreeDAPIObserver(content::WebContents* web_contents) |
| 145 : WebContentsObserver(web_contents) {} |
| 146 |
| 147 ThreeDAPIObserver::~ThreeDAPIObserver() {} |
| 148 |
| 149 void ThreeDAPIObserver::DidBlock3DAPIs(const GURL& url, |
| 150 content::ThreeDAPIType requester) { |
| 151 InfoBarTabHelper* infobar_helper = |
| 152 InfoBarTabHelper::FromWebContents(web_contents()); |
| 153 infobar_helper->AddInfoBar( |
| 154 new ThreeDAPIInfoBarDelegate(infobar_helper, url, requester)); |
| 155 } |
| OLD | NEW |