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_tab_helper.h" |
| 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "chrome/common/extensions/extension.h" |
| 12 #include "chrome/common/extensions/extension_messages.h" |
| 13 #include "chrome/common/extensions/extension_permission_set.h" |
| 14 #include "content/public/browser/navigation_entry.h" |
| 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_service.h" |
| 17 #include "content/public/browser/notification_source.h" |
| 18 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/web_contents.h" |
| 20 |
| 21 using content::RenderProcessHost; |
| 22 using content::WebContentsObserver; |
| 23 |
| 24 namespace extensions { |
| 25 |
| 26 ActiveTabPermissionManager::ActiveTabPermissionManager( |
| 27 TabContents* tab_contents) |
| 28 : WebContentsObserver(tab_contents->web_contents()), |
| 29 tab_contents_(tab_contents) { |
| 30 InsertActiveURL(web_contents()->GetURL()); |
| 31 registrar_.Add(this, |
| 32 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 33 content::Source<Profile>(tab_contents->profile())); |
| 34 } |
| 35 |
| 36 ActiveTabPermissionManager::~ActiveTabPermissionManager() {} |
| 37 |
| 38 void ActiveTabPermissionManager::GrantIfRequested(const Extension* extension) { |
| 39 if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab)) |
| 40 return; |
| 41 |
| 42 if (active_urls_.is_empty()) |
| 43 return; |
| 44 |
| 45 // Only need to check the number of permissions here rather than the URLs |
| 46 // themselves, because the set can only ever grow. |
| 47 const URLPatternSet* old_permissions = |
| 48 extension->GetTabSpecificHostPermissions(tab_id()); |
| 49 if (old_permissions && (old_permissions->size() == active_urls_.size())) |
| 50 return; |
| 51 |
| 52 granted_.Insert(extension); |
| 53 extension->SetTabSpecificHostPermissions(tab_id(), active_urls_); |
| 54 Send(new ExtensionMsg_UpdateTabSpecificPermissions(GetPageID(), |
| 55 tab_id(), |
| 56 extension->id(), |
| 57 active_urls_)); |
| 58 } |
| 59 |
| 60 void ActiveTabPermissionManager::DidCommitProvisionalLoadForFrame( |
| 61 int64 frame_id, |
| 62 bool is_main_frame, |
| 63 const GURL& url, |
| 64 content::PageTransition transition_type, |
| 65 content::RenderViewHost* render_view_host) { |
| 66 if (is_main_frame) |
| 67 ClearActiveURLsAndNotify(); |
| 68 InsertActiveURL(url); |
| 69 } |
| 70 |
| 71 void ActiveTabPermissionManager::WebContentsDestroyed( |
| 72 content::WebContents* web_contents) { |
| 73 ClearActiveURLsAndNotify(); |
| 74 } |
| 75 |
| 76 void ActiveTabPermissionManager::Observe( |
| 77 int type, |
| 78 const content::NotificationSource& source, |
| 79 const content::NotificationDetails& details) { |
| 80 DCHECK_EQ(type, chrome::NOTIFICATION_EXTENSION_UNLOADED); |
| 81 const Extension* extension = |
| 82 content::Details<UnloadedExtensionInfo>(details)->extension; |
| 83 extension->ClearTabSpecificHostPermissions(tab_id()); |
| 84 std::vector<std::string> single_id(1, extension->id()); |
| 85 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id(), single_id)); |
| 86 granted_.Remove(extension->id()); |
| 87 } |
| 88 |
| 89 void ActiveTabPermissionManager::ClearActiveURLsAndNotify() { |
| 90 active_urls_.ClearPatterns(); |
| 91 |
| 92 if (granted_.is_empty()) |
| 93 return; |
| 94 |
| 95 std::vector<std::string> extension_ids; |
| 96 |
| 97 for (ExtensionSet::const_iterator it = granted_.begin(); |
| 98 it != granted_.end(); ++it) { |
| 99 (*it)->ClearTabSpecificHostPermissions(tab_id()); |
| 100 extension_ids.push_back((*it)->id()); |
| 101 } |
| 102 |
| 103 Send(new ExtensionMsg_ClearTabSpecificPermissions(tab_id(), extension_ids)); |
| 104 granted_.Clear(); |
| 105 } |
| 106 |
| 107 void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) { |
| 108 URLPattern pattern(UserScript::kValidUserScriptSchemes); |
| 109 if (pattern.Parse(url.spec()) == URLPattern::PARSE_SUCCESS) |
| 110 active_urls_.AddPattern(pattern); |
| 111 } |
| 112 |
| 113 int32 ActiveTabPermissionManager::tab_id() { |
| 114 return tab_contents_->extension_tab_helper()->tab_id(); |
| 115 } |
| 116 |
| 117 int32 ActiveTabPermissionManager::GetPageID() { |
| 118 return tab_contents_->web_contents()->GetController().GetActiveEntry()-> |
| 119 GetPageID(); |
| 120 } |
| 121 |
| 122 } // namespace extensions |
OLD | NEW |