Chromium Code Reviews| 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 "base/memory/ref_counted.h" | |
| 8 #include "chrome/browser/extensions/extension_tab_helper.h" | |
| 9 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.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/extension_permission_set.h" | |
| 16 #include "content/public/browser/navigation_entry.h" | |
| 17 #include "content/public/browser/notification_details.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "content/public/browser/render_process_host.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 | |
| 22 using content::RenderProcessHost; | |
| 23 using content::WebContentsObserver; | |
| 24 | |
| 25 namespace extensions { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // Sends a copy of |message| to every RenderProcessHost in |profile|. | |
| 30 template <class E> | |
| 31 void SendToProfile(Profile* profile, const E& message) { | |
|
Aaron Boodman
2012/06/06 01:03:13
Nit: this would make more sense me if it were name
not at google - send to devlin
2012/06/06 07:38:40
Done.
| |
| 32 for (RenderProcessHost::iterator i(RenderProcessHost::AllHostsIterator()); | |
| 33 !i.IsAtEnd(); i.Advance()) { | |
| 34 RenderProcessHost* host = i.GetCurrentValue(); | |
| 35 Profile* host_profile = | |
| 36 Profile::FromBrowserContext(host->GetBrowserContext()); | |
| 37 if (host_profile->IsSameProfile(profile)) | |
| 38 host->Send(new E(message)); | |
|
Aaron Boodman
2012/06/06 01:03:13
Is it possible to pass a heap-allocated message to
not at google - send to devlin
2012/06/06 07:38:40
Everybody always does "Send(new BlahMessage)" so I
| |
| 39 } | |
| 40 } | |
| 41 | |
| 42 } | |
| 43 | |
| 44 ActiveTabPermissionManager::ActiveTabPermissionManager( | |
| 45 TabContentsWrapper* tab_contents) | |
| 46 : WebContentsObserver(tab_contents->web_contents()), | |
| 47 tab_contents_(tab_contents) { | |
| 48 InsertActiveURL(web_contents()->GetURL()); | |
| 49 } | |
| 50 | |
| 51 ActiveTabPermissionManager::~ActiveTabPermissionManager() {} | |
| 52 | |
| 53 void ActiveTabPermissionManager::Grant(const Extension* extension) { | |
| 54 if (!extension->HasAPIPermission(ExtensionAPIPermission::kActiveTab)) | |
| 55 return; | |
| 56 | |
| 57 int tab_id = tab_contents_->extension_tab_helper()->GetTabId(); | |
| 58 | |
| 59 // Only need to check the number of permissions here rather than the URLs | |
| 60 // themselves, because the set can only ever grow. | |
| 61 const URLPatternSet* old_permissions = | |
| 62 extension->GetTabSpecificHostPermissions(tab_id); | |
| 63 if (old_permissions && old_permissions->size() == active_urls_.size()) | |
| 64 return; | |
| 65 | |
| 66 granted_.Insert(extension); | |
|
Aaron Boodman
2012/06/06 01:03:13
What happens if the extension is unloaded?
not at google - send to devlin
2012/06/06 07:38:40
The extension stays around cos it's refcounted, an
| |
| 67 extension->SetTabSpecificHostPermissions(tab_id, active_urls_); | |
| 68 SendToProfile( | |
| 69 tab_contents_->profile(), | |
| 70 ExtensionMsg_SetTabSpecificPermissions(GetPageID() + 1, // mauahaha | |
|
Aaron Boodman
2012/06/06 01:03:13
also, i believe the correct spelling is 'muah'. It
Aaron Boodman
2012/06/06 01:03:13
I don't follow... why +1?
not at google - send to devlin
2012/06/06 07:38:40
Oh, I was just testing that the page ID blocking l
| |
| 71 tab_id, | |
| 72 extension->id(), | |
| 73 active_urls_)); | |
| 74 } | |
| 75 | |
| 76 void ActiveTabPermissionManager::DidCommitProvisionalLoadForFrame( | |
| 77 int64 frame_id, | |
| 78 bool is_main_frame, | |
| 79 const GURL& url, | |
| 80 content::PageTransition transition_type, | |
| 81 content::RenderViewHost* render_view_host) { | |
| 82 if (is_main_frame) | |
| 83 ClearActiveURLsAndNotify(); | |
| 84 InsertActiveURL(url); | |
| 85 } | |
| 86 | |
| 87 void ActiveTabPermissionManager::WebContentsDestroyed( | |
| 88 content::WebContents* web_contents) { | |
| 89 ClearActiveURLsAndNotify(); | |
| 90 } | |
| 91 | |
| 92 void ActiveTabPermissionManager::ClearActiveURLsAndNotify() { | |
| 93 active_urls_.ClearPatterns(); | |
| 94 | |
| 95 // The rest of this method deals with informing the extension(s) that the | |
| 96 // permissions for this tab need to be cleared. No need if there aren't any. | |
| 97 if (granted_.is_empty()) | |
| 98 return; | |
| 99 | |
| 100 int tab_id = tab_contents_->extension_tab_helper()->GetTabId(); | |
| 101 std::vector<std::string> extension_ids; | |
| 102 | |
| 103 for (ExtensionSet::const_iterator it = granted_.begin(); | |
| 104 it != granted_.end(); ++it) { | |
| 105 (*it)->ClearTabSpecificHostPermissions(tab_id); | |
| 106 extension_ids.push_back((*it)->id()); | |
| 107 } | |
| 108 granted_.Clear(); | |
| 109 | |
| 110 SendToProfile( | |
| 111 tab_contents_->profile(), | |
| 112 ExtensionMsg_ClearTabSpecificPermissions(tab_id, extension_ids)); | |
| 113 } | |
| 114 | |
| 115 void ActiveTabPermissionManager::InsertActiveURL(const GURL& url) { | |
| 116 URLPattern pattern(URLPattern::SCHEME_FILE | | |
|
Aaron Boodman
2012/06/06 01:03:13
UserScript::kValidUserScriptSchemes? (ftp is a bit
not at google - send to devlin
2012/06/06 07:38:40
Didn't know about that constant. Done.
| |
| 117 URLPattern::SCHEME_HTTP | | |
| 118 URLPattern::SCHEME_HTTPS); | |
| 119 if (pattern.Parse(url.spec()) == URLPattern::PARSE_SUCCESS) | |
| 120 active_urls_.AddPattern(pattern); | |
| 121 } | |
| 122 | |
| 123 int32 ActiveTabPermissionManager::GetPageID() { | |
| 124 return tab_contents_->web_contents()->GetController().GetActiveEntry()-> | |
| 125 GetPageID(); | |
| 126 } | |
| 127 | |
| 128 } // namespace extensions | |
| OLD | NEW |