15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/tab_contents/tab_util.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/site_instance.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "url/gurl.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "extensions/browser/extension_registry.h"
176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::RenderViewHost;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::SiteInstance;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace tab_util {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)content::WebContents* GetWebContentsByID(int render_process_id,
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         int render_view_id) {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* render_view_host =
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      RenderViewHost::FromID(render_process_id, render_view_id);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!render_view_host)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WebContents::FromRenderViewHost(render_view_host);
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SiteInstance* GetSiteInstanceForNewTab(Profile* profile,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       const GURL& url) {
36ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // If |url| is a WebUI or extension, we set the SiteInstance up front so that
37ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // we don't end up with an extra process swap on the first navigation.
386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL(profile, url))
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return SiteInstance::CreateForURL(profile, url);
406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#if defined(ENABLE_EXTENSIONS)
426e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)   if (extensions::ExtensionRegistry::Get(
436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)       profile)->enabled_extensions().GetHostedAppByURL(url))
446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)     return SiteInstance::CreateForURL(profile, url);
456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#endif
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We used to share the SiteInstance for same-site links opened in new tabs,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to leverage the in-memory cache and reduce process creation.  It now
49ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // appears that it is more useful to have such links open in a new process,
50ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  // so we create new tabs in a new BrowsingInstance.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace tab_util
55