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 #ifndef CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_ 6 #define CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_ 7 8 #include <map> 9 #include <set> 10 #include <string> 11 #include <vector> 12 13 #include "base/compiler_specific.h" 14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" 15 #include "chrome/common/content_settings.h" 16 #include "chrome/common/custom_handlers/protocol_handler.h" 17 #include "content/public/browser/notification_observer.h" 18 #include "content/public/browser/notification_registrar.h" 19 #include "content/public/common/media_stream_request.h" 20 #include "ui/gfx/image/image.h" 21 #include "url/gurl.h" 22 23 class ContentSettingBubbleModelDelegate; 24 class Profile; 25 class ProtocolHandlerRegistry; 26 27 namespace content { 28 class WebContents; 29 } 30 31 // This model provides data for ContentSettingBubble, and also controls 32 // the action triggered when the allow / block radio buttons are triggered. 33 class ContentSettingBubbleModel : public content::NotificationObserver { 34 public: 35 typedef ContentSettingBubbleModelDelegate Delegate; 36 37 struct PopupItem { 38 PopupItem(const gfx::Image& image, const std::string& title, int32 popup_id) 39 : image(image), title(title), popup_id(popup_id) {} 40 41 gfx::Image image; 42 std::string title; 43 int32 popup_id; 44 }; 45 typedef std::vector<PopupItem> PopupItems; 46 47 typedef std::vector<std::string> RadioItems; 48 struct RadioGroup { 49 RadioGroup(); 50 ~RadioGroup(); 51 52 GURL url; 53 std::string title; 54 RadioItems radio_items; 55 int default_item; 56 }; 57 58 struct DomainList { 59 DomainList(); 60 ~DomainList(); 61 62 std::string title; 63 std::set<std::string> hosts; 64 }; 65 66 struct MediaMenu { 67 MediaMenu(); 68 ~MediaMenu(); 69 70 std::string label; 71 content::MediaStreamDevice default_device; 72 content::MediaStreamDevice selected_device; 73 bool disabled; 74 }; 75 typedef std::map<content::MediaStreamType, MediaMenu> MediaMenuMap; 76 77 struct BubbleContent { 78 BubbleContent(); 79 ~BubbleContent(); 80 81 std::string title; 82 PopupItems popup_items; 83 RadioGroup radio_group; 84 bool radio_group_enabled; 85 std::vector<DomainList> domain_lists; 86 std::string custom_link; 87 bool custom_link_enabled; 88 std::string manage_link; 89 MediaMenuMap media_menus; 90 std::string learn_more_link; 91 92 private: 93 DISALLOW_COPY_AND_ASSIGN(BubbleContent); 94 }; 95 96 static ContentSettingBubbleModel* CreateContentSettingBubbleModel( 97 Delegate* delegate, 98 content::WebContents* web_contents, 99 Profile* profile, 100 ContentSettingsType content_type); 101 102 virtual ~ContentSettingBubbleModel(); 103 104 ContentSettingsType content_type() const { return content_type_; } 105 106 const BubbleContent& bubble_content() const { return bubble_content_; } 107 108 // content::NotificationObserver: 109 virtual void Observe(int type, 110 const content::NotificationSource& source, 111 const content::NotificationDetails& details) OVERRIDE; 112 113 virtual void OnRadioClicked(int radio_index) {} 114 virtual void OnPopupClicked(int index) {} 115 virtual void OnCustomLinkClicked() {} 116 virtual void OnManageLinkClicked() {} 117 virtual void OnLearnMoreLinkClicked() {} 118 virtual void OnMediaMenuClicked(content::MediaStreamType type, 119 const std::string& selected_device_id) {} 120 121 // Called by the view code when the bubble is closed by the user using the 122 // Done button. 123 virtual void OnDoneClicked() {} 124 125 protected: 126 ContentSettingBubbleModel( 127 content::WebContents* web_contents, 128 Profile* profile, 129 ContentSettingsType content_type); 130 131 content::WebContents* web_contents() const { return web_contents_; } 132 Profile* profile() const { return profile_; } 133 134 void set_title(const std::string& title) { bubble_content_.title = title; } 135 void add_popup(const PopupItem& popup) { 136 bubble_content_.popup_items.push_back(popup); 137 } 138 void set_radio_group(const RadioGroup& radio_group) { 139 bubble_content_.radio_group = radio_group; 140 } 141 void set_radio_group_enabled(bool enabled) { 142 bubble_content_.radio_group_enabled = enabled; 143 } 144 void add_domain_list(const DomainList& domain_list) { 145 bubble_content_.domain_lists.push_back(domain_list); 146 } 147 void set_custom_link(const std::string& link) { 148 bubble_content_.custom_link = link; 149 } 150 void set_custom_link_enabled(bool enabled) { 151 bubble_content_.custom_link_enabled = enabled; 152 } 153 void set_manage_link(const std::string& link) { 154 bubble_content_.manage_link = link; 155 } 156 void set_learn_more_link(const std::string& link) { 157 bubble_content_.learn_more_link = link; 158 } 159 void add_media_menu(content::MediaStreamType type, const MediaMenu& menu) { 160 bubble_content_.media_menus[type] = menu; 161 } 162 void set_selected_device(const content::MediaStreamDevice& device) { 163 bubble_content_.media_menus[device.type].selected_device = device; 164 } 165 bool setting_is_managed() { 166 return setting_is_managed_; 167 } 168 void set_setting_is_managed(bool managed) { 169 setting_is_managed_ = managed; 170 } 171 172 private: 173 content::WebContents* web_contents_; 174 Profile* profile_; 175 ContentSettingsType content_type_; 176 BubbleContent bubble_content_; 177 // A registrar for listening for WEB_CONTENTS_DESTROYED notifications. 178 content::NotificationRegistrar registrar_; 179 // A flag that indicates if the content setting managed i.e. can't be 180 // controlled by the user. 181 bool setting_is_managed_; 182 183 DISALLOW_COPY_AND_ASSIGN(ContentSettingBubbleModel); 184 }; 185 186 class ContentSettingTitleAndLinkModel : public ContentSettingBubbleModel { 187 public: 188 ContentSettingTitleAndLinkModel(Delegate* delegate, 189 content::WebContents* web_contents, 190 Profile* profile, 191 ContentSettingsType content_type); 192 virtual ~ContentSettingTitleAndLinkModel() {} 193 Delegate* delegate() const { return delegate_; } 194 195 private: 196 void SetTitle(); 197 void SetManageLink(); 198 void SetLearnMoreLink(); 199 200 // content::ContentSettingBubbleModel: 201 virtual void OnManageLinkClicked() OVERRIDE; 202 virtual void OnLearnMoreLinkClicked() OVERRIDE; 203 Delegate* delegate_; 204 }; 205 206 class ContentSettingRPHBubbleModel : public ContentSettingTitleAndLinkModel { 207 public: 208 ContentSettingRPHBubbleModel(Delegate* delegate, 209 content::WebContents* web_contents, 210 Profile* profile, 211 ProtocolHandlerRegistry* registry, 212 ContentSettingsType content_type); 213 214 virtual void OnRadioClicked(int radio_index) OVERRIDE; 215 virtual void OnDoneClicked() OVERRIDE; 216 217 private: 218 // These states must match the order of appearance of the radio buttons 219 // in the XIB file for the Mac port. 220 enum RPHState { 221 RPH_ALLOW = 0, 222 RPH_BLOCK, 223 RPH_IGNORE, 224 }; 225 226 void RegisterProtocolHandler(); 227 void UnregisterProtocolHandler(); 228 void IgnoreProtocolHandler(); 229 void ClearOrSetPreviousHandler(); 230 231 int selected_item_; 232 ProtocolHandlerRegistry* registry_; 233 ProtocolHandler pending_handler_; 234 ProtocolHandler previous_handler_; 235 }; 236 237 #endif // CHROME_BROWSER_UI_CONTENT_SETTINGS_CONTENT_SETTING_BUBBLE_MODEL_H_ 238