Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/permission_context_base.h" | 5 #include "chrome/browser/content_settings/permission_context_base.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/metrics/histogram.h" | |
| 8 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
| 9 #include "chrome/browser/content_settings/host_content_settings_map.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 10 #include "chrome/browser/content_settings/permission_bubble_request_impl.h" | 11 #include "chrome/browser/content_settings/permission_bubble_request_impl.h" |
| 11 #include "chrome/browser/content_settings/permission_queue_controller.h" | 12 #include "chrome/browser/content_settings/permission_queue_controller.h" |
| 12 #include "chrome/browser/content_settings/permission_request_id.h" | 13 #include "chrome/browser/content_settings/permission_request_id.h" |
| 13 #include "chrome/browser/content_settings/tab_specific_content_settings.h" | 14 #include "chrome/browser/content_settings/tab_specific_content_settings.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" | 16 #include "chrome/browser/ui/website_settings/permission_bubble_manager.h" |
| 16 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 33 PermissionContextBase::~PermissionContextBase() { | 34 PermissionContextBase::~PermissionContextBase() { |
| 34 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 35 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 35 } | 36 } |
| 36 | 37 |
| 37 void PermissionContextBase::RequestPermission( | 38 void PermissionContextBase::RequestPermission( |
| 38 content::WebContents* web_contents, | 39 content::WebContents* web_contents, |
| 39 const PermissionRequestID& id, | 40 const PermissionRequestID& id, |
| 40 const GURL& requesting_frame, | 41 const GURL& requesting_frame, |
| 41 bool user_gesture, | 42 bool user_gesture, |
| 42 const BrowserPermissionCallback& callback) { | 43 const BrowserPermissionCallback& callback) { |
| 43 // TODO(miguelg): Add UMA instrumentation. | |
| 44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 44 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 45 | 45 |
| 46 DecidePermission(web_contents, | 46 DecidePermission(web_contents, |
| 47 id, | 47 id, |
| 48 requesting_frame.GetOrigin(), | 48 requesting_frame.GetOrigin(), |
| 49 web_contents->GetLastCommittedURL().GetOrigin(), | 49 web_contents->GetLastCommittedURL().GetOrigin(), |
| 50 user_gesture, | 50 user_gesture, |
| 51 callback); | 51 callback); |
| 52 } | 52 } |
| 53 | 53 |
| 54 void PermissionContextBase::DecidePermission( | 54 void PermissionContextBase::DecidePermission( |
| 55 content::WebContents* web_contents, | 55 content::WebContents* web_contents, |
| 56 const PermissionRequestID& id, | 56 const PermissionRequestID& id, |
| 57 const GURL& requesting_origin, | 57 const GURL& requesting_origin, |
| 58 const GURL& embedder_origin, | 58 const GURL& embedder_origin, |
| 59 bool user_gesture, | 59 bool user_gesture, |
| 60 const BrowserPermissionCallback& callback) { | 60 const BrowserPermissionCallback& callback) { |
| 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 61 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 62 | 62 |
| 63 ContentSetting content_setting = | 63 ContentSetting content_setting = |
| 64 profile_->GetHostContentSettingsMap()->GetContentSetting( | 64 profile_->GetHostContentSettingsMap()->GetContentSetting( |
| 65 requesting_origin, embedder_origin, permission_type_, std::string()); | 65 requesting_origin, embedder_origin, permission_type_, std::string()); |
| 66 switch (content_setting) { | 66 switch (content_setting) { |
| 67 case CONTENT_SETTING_BLOCK: | 67 case CONTENT_SETTING_BLOCK: |
| 68 PermissionDecided( | 68 NotifyPermissionSet(id, requesting_origin, embedder_origin, |
| 69 id, requesting_origin, embedder_origin, callback, false); | 69 callback, false /* persist */, false /* granted */); |
| 70 return; | 70 return; |
| 71 case CONTENT_SETTING_ALLOW: | 71 case CONTENT_SETTING_ALLOW: |
| 72 PermissionDecided(id, requesting_origin, embedder_origin, callback, true); | 72 NotifyPermissionSet(id, requesting_origin, embedder_origin, |
| 73 callback, false /* persist */, true /* granted */); | |
| 73 return; | 74 return; |
| 74 default: | 75 default: |
| 75 break; | 76 break; |
| 76 } | 77 } |
| 77 | 78 |
| 79 UMA_HISTOGRAM_ENUMERATION("ContentSettings.PermissionRequested", | |
| 80 SettingToPermission(permission_type_), | |
| 81 PERMISSION_NUM); | |
| 82 | |
| 78 if (PermissionBubbleManager::Enabled()) { | 83 if (PermissionBubbleManager::Enabled()) { |
| 79 PermissionBubbleManager* bubble_manager = | 84 PermissionBubbleManager* bubble_manager = |
| 80 PermissionBubbleManager::FromWebContents(web_contents); | 85 PermissionBubbleManager::FromWebContents(web_contents); |
| 81 DCHECK(bubble_manager); | 86 DCHECK(bubble_manager); |
| 82 scoped_ptr<PermissionBubbleRequest> request_ptr( | 87 scoped_ptr<PermissionBubbleRequest> request_ptr( |
| 83 new PermissionBubbleRequestImpl( | 88 new PermissionBubbleRequestImpl( |
| 84 requesting_origin, | 89 requesting_origin, |
| 85 user_gesture, | 90 user_gesture, |
| 86 permission_type_, | 91 permission_type_, |
| 87 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), | 92 profile_->GetPrefs()->GetString(prefs::kAcceptLanguages), |
| 88 base::Bind(&PermissionContextBase::NotifyPermissionSet, | 93 base::Bind(&PermissionContextBase::PermissionDecided, |
| 89 weak_factory_.GetWeakPtr(), | 94 weak_factory_.GetWeakPtr(), |
| 90 id, | 95 id, |
| 91 requesting_origin, | 96 requesting_origin, |
| 92 embedder_origin, | 97 embedder_origin, |
| 93 callback), | 98 callback), |
| 94 base::Bind(&PermissionContextBase::CleanUpBubble, | 99 base::Bind(&PermissionContextBase::CleanUpBubble, |
| 95 weak_factory_.GetWeakPtr(), id))); | 100 weak_factory_.GetWeakPtr(), id))); |
| 96 PermissionBubbleRequest* request = request_ptr.get(); | 101 PermissionBubbleRequest* request = request_ptr.get(); |
| 97 | 102 |
| 98 bool inserted = pending_bubbles_.add( | 103 bool inserted = pending_bubbles_.add( |
| 99 id.ToString(), request_ptr.Pass()).second; | 104 id.ToString(), request_ptr.Pass()).second; |
| 100 DCHECK(inserted) << "Duplicate id " << id.ToString(); | 105 DCHECK(inserted) << "Duplicate id " << id.ToString(); |
| 101 bubble_manager->AddRequest(request); | 106 bubble_manager->AddRequest(request); |
| 102 return; | 107 return; |
| 103 } | 108 } |
| 104 | 109 |
| 105 // TODO(gbillock): Delete this and the infobar delegate when | 110 // TODO(gbillock): Delete this and the infobar delegate when |
| 106 // we're using only bubbles. crbug.com/337458 | 111 // we're using only bubbles. crbug.com/337458 |
| 107 GetQueueController()->CreateInfoBarRequest( | 112 GetQueueController()->CreateInfoBarRequest( |
| 108 id, | 113 id, |
| 109 requesting_origin, | 114 requesting_origin, |
| 110 embedder_origin, | 115 embedder_origin, |
| 111 std::string(), | 116 std::string(), |
| 112 base::Bind(&PermissionContextBase::NotifyPermissionSet, | 117 base::Bind(&PermissionContextBase::PermissionDecided, |
| 113 weak_factory_.GetWeakPtr(), | 118 weak_factory_.GetWeakPtr(), |
| 114 id, | 119 id, |
| 115 requesting_origin, | 120 requesting_origin, |
| 116 embedder_origin, | 121 embedder_origin, |
| 117 callback, | 122 callback, |
| 118 // the queue controller takes care of persisting the | 123 // the queue controller takes care of persisting the |
| 119 // permission | 124 // permission |
| 120 false)); | 125 false)); |
| 121 } | 126 } |
| 122 | 127 |
| 123 void PermissionContextBase::PermissionDecided( | 128 void PermissionContextBase::PermissionDecided( |
| 124 const PermissionRequestID& id, | 129 const PermissionRequestID& id, |
| 125 const GURL& requesting_origin, | 130 const GURL& requesting_origin, |
| 126 const GURL& embedder_origin, | 131 const GURL& embedder_origin, |
| 127 const BrowserPermissionCallback& callback, | 132 const BrowserPermissionCallback& callback, |
| 133 bool persist, | |
| 128 bool allowed) { | 134 bool allowed) { |
| 129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 135 UMA_HISTOGRAM_ENUMERATION(allowed |
|
Bernhard Bauer
2014/07/08 12:54:51
You were going to fix this?
Miguel Garcia
2014/07/08 14:14:20
Yes, I had not uploaded the change yet, you review
| |
| 136 ? "ContentSettings.PermissionGranted" | |
| 137 : "ContentSettings.PermissionDenied", | |
| 138 SettingToPermission(permission_type_), | |
| 139 PERMISSION_NUM); | |
|
Takashi Toyoshima
2014/07/08 10:46:17
This is really nice:)
Miguel Garcia
2014/07/08 14:14:20
Thanks!
| |
| 130 NotifyPermissionSet( | 140 NotifyPermissionSet( |
| 131 id, requesting_origin, embedder_origin, callback, false, allowed); | 141 id, requesting_origin, embedder_origin, callback, persist, allowed); |
| 132 } | 142 } |
| 133 | 143 |
| 134 PermissionQueueController* PermissionContextBase::GetQueueController() { | 144 PermissionQueueController* PermissionContextBase::GetQueueController() { |
| 135 return permission_queue_controller_.get(); | 145 return permission_queue_controller_.get(); |
| 136 } | 146 } |
| 137 | 147 |
| 138 void PermissionContextBase::NotifyPermissionSet( | 148 void PermissionContextBase::NotifyPermissionSet( |
| 139 const PermissionRequestID& id, | 149 const PermissionRequestID& id, |
| 140 const GURL& requesting_origin, | 150 const GURL& requesting_origin, |
| 141 const GURL& embedder_origin, | 151 const GURL& embedder_origin, |
| 142 const BrowserPermissionCallback& callback, | 152 const BrowserPermissionCallback& callback, |
| 143 bool persist, | 153 bool persist, |
| 144 bool allowed) { | 154 bool allowed) { |
| 145 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 155 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 146 if (persist) | 156 if (persist) |
| 147 UpdateContentSetting(requesting_origin, embedder_origin, allowed); | 157 UpdateContentSetting(requesting_origin, embedder_origin, allowed); |
| 148 | 158 |
| 149 UpdateTabContext(id, requesting_origin, allowed); | 159 UpdateTabContext(id, requesting_origin, allowed); |
| 150 callback.Run(allowed); | 160 callback.Run(allowed); |
| 151 } | 161 } |
| 152 | 162 |
| 153 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) { | 163 void PermissionContextBase::CleanUpBubble(const PermissionRequestID& id) { |
| 154 bool success = pending_bubbles_.erase(id.ToString()); | 164 bool success = pending_bubbles_.erase(id.ToString()); |
| 155 DCHECK(success) << "Missing request " << id.ToString(); | 165 DCHECK(success) << "Missing request " << id.ToString(); |
| 156 } | 166 } |
| 157 | 167 |
| 168 // static | |
| 169 PermissionType PermissionContextBase::SettingToPermission( | |
| 170 ContentSettingsType permission) { | |
| 171 switch (permission) { | |
| 172 case CONTENT_SETTINGS_TYPE_MIDI_SYSEX: | |
| 173 return PERMISSION_MIDI_SYSEX; | |
| 174 case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING: | |
| 175 return PERMISSION_PUSH_MESSAGING; | |
| 176 default: | |
| 177 NOTREACHED() << "PERMISSION " << permission << " not accounted for"; | |
| 178 } | |
| 179 return PERMISSION_UNKNOWN; | |
| 180 } | |
| 181 | |
| 158 void PermissionContextBase::UpdateContentSetting( | 182 void PermissionContextBase::UpdateContentSetting( |
| 159 const GURL& requesting_origin, | 183 const GURL& requesting_origin, |
| 160 const GURL& embedder_origin, | 184 const GURL& embedder_origin, |
| 161 bool allowed) { | 185 bool allowed) { |
| 162 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin()); | 186 DCHECK_EQ(requesting_origin, requesting_origin.GetOrigin()); |
| 163 DCHECK_EQ(embedder_origin, embedder_origin.GetOrigin()); | 187 DCHECK_EQ(embedder_origin, embedder_origin.GetOrigin()); |
| 164 ContentSetting content_setting = | 188 ContentSetting content_setting = |
| 165 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; | 189 allowed ? CONTENT_SETTING_ALLOW : CONTENT_SETTING_BLOCK; |
| 166 profile_->GetHostContentSettingsMap()->SetContentSetting( | 190 profile_->GetHostContentSettingsMap()->SetContentSetting( |
| 167 ContentSettingsPattern::FromURLNoWildcard(requesting_origin), | 191 ContentSettingsPattern::FromURLNoWildcard(requesting_origin), |
| 168 ContentSettingsPattern::FromURLNoWildcard(embedder_origin), | 192 ContentSettingsPattern::FromURLNoWildcard(embedder_origin), |
| 169 permission_type_, | 193 permission_type_, |
| 170 std::string(), | 194 std::string(), |
| 171 content_setting); | 195 content_setting); |
| 172 } | 196 } |
| OLD | NEW |