- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / file_manager / action_choice_dialog.cc
1 // Copyright 2013 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/file_manager/action_choice_dialog.h"
6
7 #include "ash/shell.h"
8 #include "base/files/file_path.h"
9 #include "chrome/browser/chromeos/file_manager/app_id.h"
10 #include "chrome/browser/chromeos/file_manager/fileapi_util.h"
11 #include "chrome/browser/chromeos/file_manager/url_util.h"
12 #include "chrome/browser/extensions/extension_service.h"
13 #include "chrome/browser/extensions/extension_system.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/profiles/profile_manager.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_iterator.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/extensions/application_launch.h"
20 #include "chrome/browser/ui/tabs/tab_strip_model.h"
21 #include "content/public/browser/web_contents.h"
22 #include "ui/gfx/screen.h"
23
24 namespace file_manager {
25 namespace util {
26 namespace {
27
28 // Finds a browser instance showing the target URL. Returns NULL if not
29 // found.
30 Browser* FindBrowserForUrl(GURL target_url) {
31   for (chrome::BrowserIterator it; !it.done(); it.Next()) {
32     Browser* browser = *it;
33     TabStripModel* tab_strip = browser->tab_strip_model();
34     for (int idx = 0; idx < tab_strip->count(); idx++) {
35       content::WebContents* web_contents = tab_strip->GetWebContentsAt(idx);
36       const GURL& url = web_contents->GetLastCommittedURL();
37       if (url == target_url)
38         return browser;
39     }
40   }
41   return NULL;
42 }
43
44 }  // namespace
45
46 void OpenActionChoiceDialog(const base::FilePath& file_path,
47                             bool advanced_mode) {
48   const int kDialogWidth = 394;
49   // TODO(dgozman): remove 50, which is a title height once popup window
50   // will have no title.
51   const int kDialogHeight = 316 + 50;
52
53   Profile* profile = ProfileManager::GetDefaultProfileOrOffTheRecord();
54
55   base::FilePath virtual_path;
56   if (!ConvertAbsoluteFilePathToRelativeFileSystemPath(
57           profile, kFileManagerAppId, file_path, &virtual_path))
58     return;
59   GURL dialog_url = GetActionChoiceUrl(virtual_path, advanced_mode);
60
61   const gfx::Size screen = ash::Shell::GetScreen()->GetPrimaryDisplay().size();
62   const gfx::Rect bounds((screen.width() - kDialogWidth) / 2,
63                          (screen.height() - kDialogHeight) / 2,
64                          kDialogWidth,
65                          kDialogHeight);
66
67   Browser* browser = FindBrowserForUrl(dialog_url);
68
69   if (browser) {
70     browser->window()->Show();
71     return;
72   }
73
74   ExtensionService* service = extensions::ExtensionSystem::Get(profile)->
75       extension_service();
76   if (!service)
77     return;
78
79   const extensions::Extension* extension =
80       service->GetExtensionById(kFileManagerAppId, false);
81   if (!extension)
82     return;
83
84   AppLaunchParams params(profile, extension,
85                          extension_misc::LAUNCH_WINDOW,
86                          NEW_FOREGROUND_TAB);
87   params.override_url = dialog_url;
88   params.override_bounds = bounds;
89   OpenApplication(params);
90 }
91
92 }  // namespace util
93 }  // namespace file_manager