tab_util.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/extensions/extension_service.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/webui/chrome_web_ui_controller_factory.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/chrome_switches.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/render_view_host.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/site_instance.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "googleurl/src/gurl.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::RenderViewHost;
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::SiteInstance;
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)using content::WebContents;
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace tab_util {
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)content::WebContents* GetWebContentsByID(int render_process_id,
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         int render_view_id) {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RenderViewHost* render_view_host =
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      RenderViewHost::FromID(render_process_id, render_view_id);
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (!render_view_host)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return NULL;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return WebContents::FromRenderViewHost(render_view_host);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)SiteInstance* GetSiteInstanceForNewTab(Profile* profile,
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       const GURL& url) {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If url is a WebUI or extension, we need to be sure to use the right type
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of renderer process up front.  Otherwise, we create a normal SiteInstance
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as part of creating the tab.
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExtensionService* service = profile->GetExtensionService();
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (ChromeWebUIControllerFactory::GetInstance()->UseWebUIForURL(
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          profile, url) ||
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      (service &&
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)       service->extensions()->GetHostedAppByURL(ExtensionURLInfo(url)))) {
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return SiteInstance::CreateForURL(profile, url);
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // We used to share the SiteInstance for same-site links opened in new tabs,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to leverage the in-memory cache and reduce process creation.  It now
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // appears that it is more useful to have such links open in a new process.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return NULL;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace tab_util
52