wallpaper_manager_util.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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/chromeos/extensions/wallpaper_manager_util.h"
6
7#include "ash/shell.h"
8#include "base/command_line.h"
9#include "chrome/browser/extensions/extension_service.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/browser/profiles/profile_manager.h"
12#include "chrome/browser/ui/browser.h"
13#include "chrome/browser/ui/browser_finder.h"
14#include "chrome/browser/ui/browser_list.h"
15#include "chrome/browser/ui/browser_tabstrip.h"
16#include "chrome/browser/ui/browser_window.h"
17#include "chrome/browser/ui/chrome_pages.h"
18#include "chrome/browser/ui/host_desktop.h"
19#include "chrome/browser/ui/tab_contents/tab_contents.h"
20#include "chrome/browser/ui/tabs/tab_strip_model.h"
21#include "chrome/common/chrome_switches.h"
22#include "chrome/common/extensions/extension_constants.h"
23#include "chrome/common/url_constants.h"
24#include "content/public/browser/web_contents.h"
25#include "ui/gfx/screen.h"
26
27namespace wallpaper_manager_util {
28namespace {
29
30Browser* GetBrowserForUrl(GURL target_url) {
31  for (BrowserList::const_iterator browser_iterator = BrowserList::begin();
32       browser_iterator != BrowserList::end(); ++browser_iterator) {
33    Browser* browser = *browser_iterator;
34    TabStripModel* tab_strip = browser->tab_strip_model();
35    for (int idx = 0; idx < tab_strip->count(); idx++) {
36      content::WebContents* web_contents =
37          tab_strip->GetTabContentsAt(idx)->web_contents();
38      const GURL& url = web_contents->GetURL();
39      if (url == target_url)
40        return browser;
41    }
42  }
43  return NULL;
44}
45
46}  // namespace
47
48void OpenWallpaperManager() {
49  Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
50  std::string url = chrome::kChromeUIWallpaperURL;
51  ExtensionService* service = profile->GetExtensionService();
52  if (!service)
53    return;
54
55  const extensions::Extension* extension =
56      service->GetExtensionById(extension_misc::kWallpaperManagerId, false);
57  if (!extension)
58    return;
59
60  GURL wallpaper_picker_url(url);
61  int width = extension->launch_width();
62  int height = extension->launch_height();
63  // TODO(oshima|bshe): Open WallpaperManager in the display is is requested.
64  const gfx::Size screen =
65      ash::Shell::GetScreen()->GetPrimaryDisplay().size();
66  const gfx::Rect bounds((screen.width() - width) / 2,
67                         (screen.height() - height) / 2,
68                         width,
69                         height);
70
71  Browser* browser = GetBrowserForUrl(wallpaper_picker_url);
72
73  if (!browser) {
74    browser = new Browser(
75        Browser::CreateParams::CreateForApp(Browser::TYPE_POPUP,
76                                            extension->name(),
77                                            bounds,
78                                            profile));
79
80    chrome::AddSelectedTabWithURL(browser, wallpaper_picker_url,
81                                  content::PAGE_TRANSITION_LINK);
82  }
83  browser->window()->Show();
84}
85
86}  // namespace wallpaper_manager_util
87