Broke ContentSettingBubbleModelTest.Plugins on Android.
[chromium-blink-merge.git] / content / browser / browsing_instance.cc
blobd10ea209b2f0bd44bc9be0959c3524a9155db5e3
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.
5 #include "content/browser/browsing_instance.h"
7 #include "base/command_line.h"
8 #include "base/logging.h"
9 #include "content/browser/site_instance_impl.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/content_browser_client.h"
12 #include "content/public/common/content_switches.h"
13 #include "content/public/common/url_constants.h"
15 namespace content {
17 BrowsingInstance::BrowsingInstance(BrowserContext* browser_context)
18 : browser_context_(browser_context) {
21 bool BrowsingInstance::HasSiteInstance(const GURL& url) {
22 std::string site =
23 SiteInstanceImpl::GetSiteForURL(browser_context_, url)
24 .possibly_invalid_spec();
26 return site_instance_map_.find(site) != site_instance_map_.end();
29 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) {
30 std::string site =
31 SiteInstanceImpl::GetSiteForURL(browser_context_, url)
32 .possibly_invalid_spec();
34 SiteInstanceMap::iterator i = site_instance_map_.find(site);
35 if (i != site_instance_map_.end())
36 return i->second;
39 // No current SiteInstance for this site, so let's create one.
40 SiteInstanceImpl* instance = new SiteInstanceImpl(this);
42 // Set the site of this new SiteInstance, which will register it with us.
43 instance->SetSite(url);
44 return instance;
47 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) {
48 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->
49 browsing_instance_ == this);
50 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite());
51 std::string site = site_instance->GetSiteURL().possibly_invalid_spec();
53 // Only register if we don't have a SiteInstance for this site already.
54 // It's possible to have two SiteInstances point to the same site if two
55 // tabs are navigated there at the same time. (We don't call SetSite or
56 // register them until DidNavigate.) If there is a previously existing
57 // SiteInstance for this site, we just won't register the new one.
58 SiteInstanceMap::iterator i = site_instance_map_.find(site);
59 if (i == site_instance_map_.end()) {
60 // Not previously registered, so register it.
61 site_instance_map_[site] = site_instance;
65 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) {
66 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->
67 browsing_instance_ == this);
68 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite());
69 std::string site = site_instance->GetSiteURL().possibly_invalid_spec();
71 // Only unregister the SiteInstance if it is the same one that is registered
72 // for the site. (It might have been an unregistered SiteInstance. See the
73 // comments in RegisterSiteInstance.)
74 SiteInstanceMap::iterator i = site_instance_map_.find(site);
75 if (i != site_instance_map_.end() && i->second == site_instance) {
76 // Matches, so erase it.
77 site_instance_map_.erase(i);
81 BrowsingInstance::~BrowsingInstance() {
82 // We should only be deleted when all of the SiteInstances that refer to
83 // us are gone.
84 DCHECK(site_instance_map_.empty());
87 } // namespace content