Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / browsing_instance.cc
blob1d5f8953769f92efbc770b3ca88331235d4d8002
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),
19 active_contents_count_(0u) {
22 bool BrowsingInstance::HasSiteInstance(const GURL& url) {
23 std::string site =
24 SiteInstanceImpl::GetSiteForURL(browser_context_, url)
25 .possibly_invalid_spec();
27 return site_instance_map_.find(site) != site_instance_map_.end();
30 SiteInstance* BrowsingInstance::GetSiteInstanceForURL(const GURL& url) {
31 std::string site =
32 SiteInstanceImpl::GetSiteForURL(browser_context_, url)
33 .possibly_invalid_spec();
35 SiteInstanceMap::iterator i = site_instance_map_.find(site);
36 if (i != site_instance_map_.end())
37 return i->second;
40 // No current SiteInstance for this site, so let's create one.
41 SiteInstanceImpl* instance = new SiteInstanceImpl(this);
43 // Set the site of this new SiteInstance, which will register it with us.
44 instance->SetSite(url);
45 return instance;
48 void BrowsingInstance::RegisterSiteInstance(SiteInstance* site_instance) {
49 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)
50 ->browsing_instance_.get() ==
51 this);
52 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite());
53 std::string site = site_instance->GetSiteURL().possibly_invalid_spec();
55 // Only register if we don't have a SiteInstance for this site already.
56 // It's possible to have two SiteInstances point to the same site if two
57 // tabs are navigated there at the same time. (We don't call SetSite or
58 // register them until DidNavigate.) If there is a previously existing
59 // SiteInstance for this site, we just won't register the new one.
60 SiteInstanceMap::iterator i = site_instance_map_.find(site);
61 if (i == site_instance_map_.end()) {
62 // Not previously registered, so register it.
63 site_instance_map_[site] = site_instance;
67 void BrowsingInstance::UnregisterSiteInstance(SiteInstance* site_instance) {
68 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)
69 ->browsing_instance_.get() ==
70 this);
71 DCHECK(static_cast<SiteInstanceImpl*>(site_instance)->HasSite());
72 std::string site = site_instance->GetSiteURL().possibly_invalid_spec();
74 // Only unregister the SiteInstance if it is the same one that is registered
75 // for the site. (It might have been an unregistered SiteInstance. See the
76 // comments in RegisterSiteInstance.)
77 SiteInstanceMap::iterator i = site_instance_map_.find(site);
78 if (i != site_instance_map_.end() && i->second == site_instance) {
79 // Matches, so erase it.
80 site_instance_map_.erase(i);
84 BrowsingInstance::~BrowsingInstance() {
85 // We should only be deleted when all of the SiteInstances that refer to
86 // us are gone.
87 DCHECK(site_instance_map_.empty());
88 DCHECK_EQ(0u, active_contents_count_);
91 } // namespace content