| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/shell_dialogs.h" | 5 #include "chrome/browser/shell_dialogs.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| 11 #include "base/scoped_ptr.h" | 11 #include "base/scoped_ptr.h" |
| 12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
| 13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/browser/browser.h" | 15 #include "chrome/browser/browser.h" |
| 16 #include "chrome/browser/browser_list.h" | 16 #include "chrome/browser/browser_list.h" |
| 17 #include "chrome/browser/chrome_thread.h" | 17 #include "chrome/browser/chrome_thread.h" |
| 18 #include "chrome/browser/dom_ui/html_dialog_ui.h" | 18 #include "chrome/browser/dom_ui/html_dialog_ui.h" |
| 19 #include "chrome/browser/shell_dialogs.h" | 19 #include "chrome/browser/shell_dialogs.h" |
| 20 #include "chrome/browser/tab_contents/tab_contents.h" | 20 #include "chrome/browser/tab_contents/tab_contents.h" |
| 21 #include "chrome/browser/views/browser_dialogs.h" |
| 21 #include "chrome/browser/views/html_dialog_view.h" | 22 #include "chrome/browser/views/html_dialog_view.h" |
| 22 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 23 #include "grit/generated_resources.h" | 24 #include "grit/generated_resources.h" |
| 24 #include "views/window/non_client_view.h" | 25 #include "views/window/non_client_view.h" |
| 25 #include "views/window/window.h" | 26 #include "views/window/window.h" |
| 26 | 27 |
| 28 #include "chrome/browser/profile_manager.h" |
| 29 |
| 27 namespace { | 30 namespace { |
| 28 | 31 |
| 29 static const wchar_t* kKeyNamePath = L"path"; | 32 static const wchar_t* kKeyNamePath = L"path"; |
| 30 static const int kSaveCompletePageIndex = 2; | 33 static const int kSaveCompletePageIndex = 2; |
| 31 | 34 |
| 32 }; // namespace | 35 }; // namespace |
| 33 | 36 |
| 34 // Implementation of SelectFileDialog that shows an UI for choosing a file | 37 // Implementation of SelectFileDialog that shows an UI for choosing a file |
| 35 // or folder using FileBrowseUI. | 38 // or folder using FileBrowseUI. |
| 36 class SelectFileDialogImpl : public SelectFileDialog { | 39 class SelectFileDialogImpl : public SelectFileDialog { |
| 37 public: | 40 public: |
| 38 explicit SelectFileDialogImpl(Listener* listener); | 41 explicit SelectFileDialogImpl(Listener* listener); |
| 39 | 42 |
| 40 // BaseShellDialog implementation. | 43 // BaseShellDialog implementation. |
| 41 virtual bool IsRunning(gfx::NativeWindow parent_window) const; | 44 virtual bool IsRunning(gfx::NativeWindow parent_window) const; |
| 42 virtual void ListenerDestroyed(); | 45 virtual void ListenerDestroyed(); |
| 43 | 46 |
| 44 // SelectFileDialog implementation. | 47 // SelectFileDialog implementation. |
| 45 // |params| is user data we pass back via the Listener interface. | 48 // |params| is user data we pass back via the Listener interface. |
| 46 virtual void SelectFile(Type type, | 49 virtual void SelectFile(Type type, |
| 47 const string16& title, | 50 const string16& title, |
| 48 const FilePath& default_path, | 51 const FilePath& default_path, |
| 49 const FileTypeInfo* file_types, | 52 const FileTypeInfo* file_types, |
| 50 int file_type_index, | 53 int file_type_index, |
| 51 const FilePath::StringType& default_extension, | 54 const FilePath::StringType& default_extension, |
| 52 gfx::NativeWindow owning_window, | 55 gfx::NativeWindow owning_window, |
| 53 void* params); | 56 void* params); |
| 54 | 57 |
| 58 virtual void set_browser_mode(bool value) { |
| 59 browser_mode_ = value; |
| 60 } |
| 61 |
| 55 private: | 62 private: |
| 56 virtual ~SelectFileDialogImpl(); | 63 virtual ~SelectFileDialogImpl(); |
| 57 | 64 |
| 58 class FileBrowseDelegate : public HtmlDialogUIDelegate { | 65 class FileBrowseDelegate : public HtmlDialogUIDelegate { |
| 59 public: | 66 public: |
| 60 FileBrowseDelegate(SelectFileDialogImpl* owner, | 67 FileBrowseDelegate(SelectFileDialogImpl* owner, |
| 61 Type type, | 68 Type type, |
| 62 const std::wstring& title, | 69 const std::wstring& title, |
| 63 const FilePath& default_path, | 70 const FilePath& default_path, |
| 64 const FileTypeInfo* file_types, | 71 const FileTypeInfo* file_types, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 123 | 130 |
| 124 private: | 131 private: |
| 125 FileBrowseDelegate* delegate_; | 132 FileBrowseDelegate* delegate_; |
| 126 | 133 |
| 127 DISALLOW_COPY_AND_ASSIGN(FileBrowseDelegateHandler); | 134 DISALLOW_COPY_AND_ASSIGN(FileBrowseDelegateHandler); |
| 128 }; | 135 }; |
| 129 | 136 |
| 130 // Notification from FileBrowseDelegate when file browse UI is dismissed. | 137 // Notification from FileBrowseDelegate when file browse UI is dismissed. |
| 131 void OnDialogClosed(FileBrowseDelegate* delegate, const std::string& json); | 138 void OnDialogClosed(FileBrowseDelegate* delegate, const std::string& json); |
| 132 | 139 |
| 140 // Callback method to open HTML |
| 141 void OpenHtmlDialog(gfx::NativeWindow owning_window, |
| 142 FileBrowseDelegate* file_browse_delegate); |
| 143 |
| 133 // The set of all parent windows for which we are currently running dialogs. | 144 // The set of all parent windows for which we are currently running dialogs. |
| 134 std::set<gfx::NativeWindow> parents_; | 145 std::set<gfx::NativeWindow> parents_; |
| 135 | 146 |
| 136 // The set of all FileBrowseDelegate that we are currently running. | 147 // The set of all FileBrowseDelegate that we are currently running. |
| 137 std::set<FileBrowseDelegate*> delegates_; | 148 std::set<FileBrowseDelegate*> delegates_; |
| 138 | 149 |
| 150 // True when opening in browser, otherwise in OOBE/login mode. |
| 151 bool browser_mode_; |
| 152 |
| 139 // The listener to be notified of selection completion. | 153 // The listener to be notified of selection completion. |
| 140 Listener* listener_; | 154 Listener* listener_; |
| 141 | 155 |
| 142 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); | 156 DISALLOW_COPY_AND_ASSIGN(SelectFileDialogImpl); |
| 143 }; | 157 }; |
| 144 | 158 |
| 145 // static | 159 // static |
| 146 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { | 160 SelectFileDialog* SelectFileDialog::Create(Listener* listener) { |
| 147 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::IO)); | 161 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 148 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::FILE)); | 162 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::FILE)); |
| 149 return new SelectFileDialogImpl(listener); | 163 return new SelectFileDialogImpl(listener); |
| 150 } | 164 } |
| 151 | 165 |
| 152 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener) | 166 SelectFileDialogImpl::SelectFileDialogImpl(Listener* listener) |
| 153 : listener_(listener) { | 167 : browser_mode_(true), |
| 168 listener_(listener) { |
| 154 } | 169 } |
| 155 | 170 |
| 156 SelectFileDialogImpl::~SelectFileDialogImpl() { | 171 SelectFileDialogImpl::~SelectFileDialogImpl() { |
| 157 // All dialogs should be dismissed by now. | 172 // All dialogs should be dismissed by now. |
| 158 DCHECK(parents_.empty() && delegates_.empty()); | 173 DCHECK(parents_.empty() && delegates_.empty()); |
| 159 } | 174 } |
| 160 | 175 |
| 161 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow parent_window) const { | 176 bool SelectFileDialogImpl::IsRunning(gfx::NativeWindow parent_window) const { |
| 162 return parent_window && parents_.find(parent_window) != parents_.end(); | 177 return parent_window && parents_.find(parent_window) != parents_.end(); |
| 163 } | 178 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 } | 214 } |
| 200 | 215 |
| 201 if (owning_window) | 216 if (owning_window) |
| 202 parents_.insert(owning_window); | 217 parents_.insert(owning_window); |
| 203 | 218 |
| 204 FileBrowseDelegate* file_browse_delegate = new FileBrowseDelegate(this, | 219 FileBrowseDelegate* file_browse_delegate = new FileBrowseDelegate(this, |
| 205 type, title_string, default_path, file_types, file_type_index, | 220 type, title_string, default_path, file_types, file_type_index, |
| 206 default_extension, owning_window, params); | 221 default_extension, owning_window, params); |
| 207 delegates_.insert(file_browse_delegate); | 222 delegates_.insert(file_browse_delegate); |
| 208 | 223 |
| 209 Browser* browser = BrowserList::GetLastActive(); | 224 if (browser_mode_) { |
| 210 DCHECK(browser); | 225 Browser* browser = BrowserList::GetLastActive(); |
| 211 browser->BrowserShowHtmlDialog(file_browse_delegate, owning_window); | 226 DCHECK(browser); |
| 227 browser->BrowserShowHtmlDialog(file_browse_delegate, owning_window); |
| 228 } else { |
| 229 ChromeThread::PostTask( |
| 230 ChromeThread::UI, |
| 231 FROM_HERE, |
| 232 NewRunnableMethod(this, |
| 233 &SelectFileDialogImpl::OpenHtmlDialog, |
| 234 owning_window, |
| 235 file_browse_delegate)); |
| 236 } |
| 212 } | 237 } |
| 213 | 238 |
| 214 void SelectFileDialogImpl::OnDialogClosed(FileBrowseDelegate* delegate, | 239 void SelectFileDialogImpl::OnDialogClosed(FileBrowseDelegate* delegate, |
| 215 const std::string& json) { | 240 const std::string& json) { |
| 216 // Nothing to do if listener_ is gone. | 241 // Nothing to do if listener_ is gone. |
| 217 | 242 |
| 218 if (!listener_) | 243 if (!listener_) |
| 219 return; | 244 return; |
| 220 | 245 |
| 221 bool notification_fired = false; | 246 bool notification_fired = false; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 264 } | 289 } |
| 265 | 290 |
| 266 // Always notify listener when dialog is dismissed. | 291 // Always notify listener when dialog is dismissed. |
| 267 if (!notification_fired) | 292 if (!notification_fired) |
| 268 listener_->FileSelectionCanceled(delegate->params_); | 293 listener_->FileSelectionCanceled(delegate->params_); |
| 269 | 294 |
| 270 parents_.erase(delegate->parent_); | 295 parents_.erase(delegate->parent_); |
| 271 delegates_.erase(delegate); | 296 delegates_.erase(delegate); |
| 272 } | 297 } |
| 273 | 298 |
| 299 void SelectFileDialogImpl::OpenHtmlDialog( |
| 300 gfx::NativeWindow owning_window, |
| 301 FileBrowseDelegate* file_browse_delegate) { |
| 302 browser::ShowHtmlDialogView(owning_window, |
| 303 ProfileManager::GetDefaultProfile(), |
| 304 file_browse_delegate); |
| 305 } |
| 306 |
| 274 SelectFileDialogImpl::FileBrowseDelegate::FileBrowseDelegate( | 307 SelectFileDialogImpl::FileBrowseDelegate::FileBrowseDelegate( |
| 275 SelectFileDialogImpl* owner, | 308 SelectFileDialogImpl* owner, |
| 276 Type type, | 309 Type type, |
| 277 const std::wstring& title, | 310 const std::wstring& title, |
| 278 const FilePath& default_path, | 311 const FilePath& default_path, |
| 279 const FileTypeInfo* file_types, | 312 const FileTypeInfo* file_types, |
| 280 int file_type_index, | 313 int file_type_index, |
| 281 const FilePath::StringType& default_extension, | 314 const FilePath::StringType& default_extension, |
| 282 gfx::NativeWindow parent, | 315 gfx::NativeWindow parent, |
| 283 void* params) | 316 void* params) |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 438 // TODO(xiyuan): Change this when the infrastructure is improved. | 471 // TODO(xiyuan): Change this when the infrastructure is improved. |
| 439 HtmlDialogUIDelegate** delegate = HtmlDialogUI::GetPropertyAccessor(). | 472 HtmlDialogUIDelegate** delegate = HtmlDialogUI::GetPropertyAccessor(). |
| 440 GetProperty(dom_ui_->tab_contents()->property_bag()); | 473 GetProperty(dom_ui_->tab_contents()->property_bag()); |
| 441 HtmlDialogView* containing_view = static_cast<HtmlDialogView*>(*delegate); | 474 HtmlDialogView* containing_view = static_cast<HtmlDialogView*>(*delegate); |
| 442 DCHECK(containing_view); | 475 DCHECK(containing_view); |
| 443 | 476 |
| 444 containing_view->GetWindow()->UpdateWindowTitle(); | 477 containing_view->GetWindow()->UpdateWindowTitle(); |
| 445 containing_view->GetWindow()->GetNonClientView()->SchedulePaint(); | 478 containing_view->GetWindow()->GetNonClientView()->SchedulePaint(); |
| 446 } | 479 } |
| 447 } | 480 } |
| OLD | NEW |