| 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/extensions/active_tab_permission_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/extensions/extension_service.h" | |
| 8 #include "chrome/browser/extensions/extension_system.h" | |
| 9 #include "chrome/browser/profiles/profile.h" | |
| 10 #include "chrome/browser/sessions/session_id.h" | |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents.h" | |
| 12 #include "chrome/common/chrome_notification_types.h" | |
| 13 #include "chrome/common/extensions/extension.h" | |
| 14 #include "chrome/common/extensions/extension_messages.h" | |
| 15 #include "chrome/common/extensions/permissions/permission_set.h" | |
| 16 #include "content/public/browser/navigation_entry.h" | |
| 17 #include "content/public/browser/navigation_details.h" | |
| 18 #include "content/public/browser/notification_service.h" | |
| 19 #include "content/public/browser/notification_source.h" | |
| 20 #include "content/public/browser/render_process_host.h" | |
| 21 #include "content/public/browser/web_contents.h" | |
| 22 | |
| 23 using content::RenderProcessHost; | |
| 24 using content::WebContentsObserver; | |
| 25 | |
| 26 namespace extensions { | |
| 27 | |
| 28 ActiveTabPermissionManager::ActiveTabPermissionManager( | |
| 29 content::WebContents* web_contents, int tab_id, Profile* profile) | |
| 30 : WebContentsObserver(web_contents), tab_id_(tab_id) { | |
| 31 registrar_.Add(this, | |
| 32 chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 33 content::Source<Profile>(profile)); | |
| 34 } | |
| 35 | |
| 36 ActiveTabPermissionManager::~ActiveTabPermissionManager() {} | |
| 37 | |
| 38 void ActiveTabPermissionManager::GrantIfRequested(const Extension* extension) { | |
| 39 if (!extension->HasAPIPermission(extensions::APIPermission::kActiveTab)) | |
| 40 return; | |
| 41 | |
| 42 if (IsGranted(extension)) | |
| 43 return; | |
| 44 | |
| 45 URLPattern pattern(UserScript::kValidUserScriptSchemes); | |
| 46 if (pattern.Parse(web_contents()->GetURL().spec()) != | |
| 47 URLPattern::PARSE_SUCCESS) { | |
| 48 // Pattern parsing could fail if this is an unsupported URL e.g. chrome://. | |
| 49 return; | |
| 50 } | |
| 51 | |
| 52 APIPermissionSet new_apis; | |
| 53 new_apis.insert(APIPermission::kTab); | |
| 54 URLPatternSet new_hosts; | |
| 55 new_hosts.AddPattern(pattern); | |
| 56 scoped_refptr<const PermissionSet> new_permissions = | |
| 57 new PermissionSet(new_apis, new_hosts, URLPatternSet()); | |
| 58 | |
| 59 extension->UpdateTabSpecificPermissions(tab_id_, new_permissions); | |
| 60 granted_extensions_.Insert(extension); | |
| 61 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(), | |
| 62 tab_id_, | |
| 63 extension->id(), | |
| 64 new_hosts)); | |
| 65 } | |
| 66 | |
| 67 bool ActiveTabPermissionManager::IsGranted(const Extension* extension) { | |
| 68 return granted_extensions_.Contains(extension->id()); | |
| 69 } | |
| 70 | |
| 71 void ActiveTabPermissionManager::DidNavigateMainFrame( | |
| 72 const content::LoadCommittedDetails& details, | |
| 73 const content::FrameNavigateParams& params) { | |
| 74 if (details.is_in_page) | |
| 75 return; | |
| 76 DCHECK(details.is_main_frame); // important: sub-frames don't get granted! | |
| 77 ClearActiveExtensionsAndNotify(); | |
| 78 } | |
| 79 | |
| 80 void ActiveTabPermissionManager::WebContentsDestroyed( | |
| 81 content::WebContents* web_contents) { | |
| 82 ClearActiveExtensionsAndNotify(); | |
| 83 } | |
| 84 | |
| 85 void ActiveTabPermissionManager::Observe( | |
| 86 int type, | |
| 87 const content::NotificationSource& source, | |
| 88 const content::NotificationDetails& details) { | |
| 89 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); | |
| 90 const Extension* extension = | |
| 91 content::Details<UnloadedExtensionInfo>(details)->extension; | |
| 92 // Note: don't need to clear the permissions (nor tell the renderer about it) | |
| 93 // because it's being unloaded anyway. | |
| 94 granted_extensions_.Remove(extension->id()); | |
| 95 } | |
| 96 | |
| 97 void ActiveTabPermissionManager::ClearActiveExtensionsAndNotify() { | |
| 98 if (granted_extensions_.is_empty()) | |
| 99 return; | |
| 100 | |
| 101 std::vector<std::string> extension_ids; | |
| 102 | |
| 103 for (ExtensionSet::const_iterator it = granted_extensions_.begin(); | |
| 104 it != granted_extensions_.end(); ++it) { | |
| 105 (*it)->ClearTabSpecificPermissions(tab_id_); | |
| 106 extension_ids.push_back((*it)->id()); | |
| 107 } | |
| 108 | |
| 109 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id_, extension_ids)); | |
| 110 granted_extensions_.Clear(); | |
| 111 } | |
| 112 | |
| 113 int32 ActiveTabPermissionManager::GetPageID() { | |
| 114 return web_contents()->GetController().GetActiveEntry()->GetPageID(); | |
| 115 } | |
| 116 | |
| 117 } // namespace extensions | |
| OLD | NEW |