OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/chromeos/drive/file_task_executor.h" | 5 #include "chrome/browser/chromeos/drive/file_task_executor.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "chrome/browser/chromeos/drive/drive.pb.h" | 10 #include "chrome/browser/chromeos/drive/drive.pb.h" |
11 #include "chrome/browser/chromeos/drive/drive_integration_service.h" | 11 #include "chrome/browser/chromeos/drive/drive_integration_service.h" |
12 #include "chrome/browser/chromeos/drive/file_system_interface.h" | 12 #include "chrome/browser/chromeos/drive/file_system_interface.h" |
13 #include "chrome/browser/drive/drive_service_interface.h" | 13 #include "chrome/browser/drive/drive_service_interface.h" |
14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" | 15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h" |
16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
17 #include "chrome/browser/ui/browser_tabstrip.h" | 17 #include "chrome/browser/ui/browser_tabstrip.h" |
18 #include "chrome/browser/ui/browser_window.h" | 18 #include "chrome/browser/ui/browser_window.h" |
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" | 19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h" |
20 #include "chrome/common/extensions/api/file_browser_private.h" | 20 #include "chrome/common/extensions/api/file_browser_private.h" |
21 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
22 #include "webkit/browser/fileapi/file_system_url.h" | 22 #include "webkit/browser/fileapi/file_system_url.h" |
23 | 23 |
24 using fileapi::FileSystemURL; | 24 using fileapi::FileSystemURL; |
25 | 25 |
26 namespace drive { | 26 namespace drive { |
27 | 27 |
28 FileTaskExecutor::FileTaskExecutor(Profile* profile, | 28 namespace { |
29 const std::string& app_id) | 29 |
30 : profile_(profile), | 30 class FileTaskExecutorDelegateImpl : public FileTaskExecutorDelegate { |
| 31 public: |
| 32 explicit FileTaskExecutorDelegateImpl(Profile* profile) : profile_(profile) { |
| 33 } |
| 34 |
| 35 virtual FileSystemInterface* GetFileSystem() OVERRIDE { |
| 36 return util::GetFileSystemByProfile(profile_); |
| 37 } |
| 38 |
| 39 virtual DriveServiceInterface* GetDriveService() OVERRIDE { |
| 40 return util::GetDriveServiceByProfile(profile_); |
| 41 } |
| 42 |
| 43 virtual void OpenBrowserWindow(const GURL& open_link) OVERRIDE { |
| 44 chrome::ScopedTabbedBrowserDisplayer displayer( |
| 45 profile_, chrome::HOST_DESKTOP_TYPE_ASH); |
| 46 chrome::AddSelectedTabWithURL(displayer.browser(), open_link, |
| 47 content::PAGE_TRANSITION_LINK); |
| 48 // Since the ScopedTabbedBrowserDisplayer does not guarantee that the |
| 49 // browser will be shown on the active desktop, we ensure the visibility. |
| 50 multi_user_util::MoveWindowToCurrentDesktop( |
| 51 displayer.browser()->window()->GetNativeWindow()); |
| 52 } |
| 53 |
| 54 private: |
| 55 Profile* const profile_; |
| 56 }; |
| 57 |
| 58 } // namespace |
| 59 |
| 60 FileTaskExecutor::FileTaskExecutor(Profile* profile, const std::string& app_id) |
| 61 : delegate_(new FileTaskExecutorDelegateImpl(profile)), |
31 app_id_(app_id), | 62 app_id_(app_id), |
32 current_index_(0), | 63 current_index_(0), |
33 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
| 65 } |
| 66 |
| 67 FileTaskExecutor::FileTaskExecutor( |
| 68 scoped_ptr<FileTaskExecutorDelegate> delegate, |
| 69 const std::string& app_id) |
| 70 : delegate_(delegate.Pass()), |
| 71 app_id_(app_id), |
| 72 current_index_(0), |
| 73 weak_ptr_factory_(this) { |
34 } | 74 } |
35 | 75 |
36 FileTaskExecutor::~FileTaskExecutor() { | 76 FileTaskExecutor::~FileTaskExecutor() { |
37 } | 77 } |
38 | 78 |
39 void FileTaskExecutor::Execute( | 79 void FileTaskExecutor::Execute( |
40 const std::vector<FileSystemURL>& file_urls, | 80 const std::vector<FileSystemURL>& file_urls, |
41 const file_manager::file_tasks::FileTaskFinishedCallback& done) { | 81 const file_manager::file_tasks::FileTaskFinishedCallback& done) { |
| 82 DCHECK(!file_urls.empty()); |
| 83 |
42 done_ = done; | 84 done_ = done; |
43 | 85 |
44 std::vector<base::FilePath> paths; | 86 std::vector<base::FilePath> paths; |
45 for (size_t i = 0; i < file_urls.size(); ++i) { | 87 for (size_t i = 0; i < file_urls.size(); ++i) { |
46 base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]); | 88 base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]); |
47 if (path.empty()) { | 89 if (path.empty()) { |
48 Done(false); | 90 Done(false); |
49 return; | 91 return; |
50 } | 92 } |
51 paths.push_back(path); | 93 paths.push_back(path); |
52 } | 94 } |
53 | 95 |
54 FileSystemInterface* file_system = util::GetFileSystemByProfile(profile_); | 96 FileSystemInterface* const file_system = delegate_->GetFileSystem(); |
55 if (!file_system) { | 97 if (!file_system) { |
56 Done(false); | 98 Done(false); |
57 return; | 99 return; |
58 } | 100 } |
59 | 101 |
60 // Reset the index, so we know when we're done. | 102 // Reset the index, so we know when we're done. |
61 DCHECK_EQ(current_index_, 0); | 103 DCHECK_EQ(current_index_, 0); |
62 current_index_ = paths.size(); | 104 current_index_ = paths.size(); |
63 | 105 |
64 for (size_t i = 0; i < paths.size(); ++i) { | 106 for (size_t i = 0; i < paths.size(); ++i) { |
65 file_system->GetResourceEntry( | 107 file_system->GetResourceEntry( |
66 paths[i], | 108 paths[i], |
67 base::Bind(&FileTaskExecutor::OnFileEntryFetched, | 109 base::Bind(&FileTaskExecutor::OnFileEntryFetched, |
68 weak_ptr_factory_.GetWeakPtr())); | 110 weak_ptr_factory_.GetWeakPtr())); |
69 } | 111 } |
70 } | 112 } |
71 | 113 |
72 void FileTaskExecutor::OnFileEntryFetched(FileError error, | 114 void FileTaskExecutor::OnFileEntryFetched(FileError error, |
73 scoped_ptr<ResourceEntry> entry) { | 115 scoped_ptr<ResourceEntry> entry) { |
74 // Here, we are only interested in files. | 116 // Here, we are only interested in files. |
75 if (entry.get() && !entry->has_file_specific_info()) | 117 if (entry.get() && !entry->has_file_specific_info()) |
76 error = FILE_ERROR_NOT_FOUND; | 118 error = FILE_ERROR_NOT_FOUND; |
77 | 119 |
78 DriveServiceInterface* drive_service = | 120 DriveServiceInterface* const drive_service = delegate_->GetDriveService(); |
79 util::GetDriveServiceByProfile(profile_); | |
80 | |
81 if (!drive_service || error != FILE_ERROR_OK) { | 121 if (!drive_service || error != FILE_ERROR_OK) { |
82 Done(false); | 122 Done(false); |
83 return; | 123 return; |
84 } | 124 } |
85 | 125 |
86 // Send off a request for the drive service to authorize the apps for the | 126 // Send off a request for the drive service to authorize the apps for the |
87 // current document entry for this document so we can get the | 127 // current document entry for this document so we can get the |
88 // open-with-<app_id> urls from the document entry. | 128 // open-with-<app_id> urls from the document entry. |
89 drive_service->AuthorizeApp(entry->resource_id(), | 129 drive_service->AuthorizeApp(entry->resource_id(), |
90 app_id_, | 130 app_id_, |
91 base::Bind(&FileTaskExecutor::OnAppAuthorized, | 131 base::Bind(&FileTaskExecutor::OnAppAuthorized, |
92 weak_ptr_factory_.GetWeakPtr(), | 132 weak_ptr_factory_.GetWeakPtr(), |
93 entry->resource_id())); | 133 entry->resource_id())); |
94 } | 134 } |
95 | 135 |
96 void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id, | 136 void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id, |
97 google_apis::GDataErrorCode error, | 137 google_apis::GDataErrorCode error, |
98 const GURL& open_link) { | 138 const GURL& open_link) { |
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 139 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
100 | 140 |
101 DriveIntegrationService* service = | 141 if (error != google_apis::HTTP_SUCCESS || open_link.is_empty()) { |
102 DriveIntegrationServiceFactory::FindForProfile(profile_); | |
103 if (!service || !service->IsMounted() || | |
104 error != google_apis::HTTP_SUCCESS || open_link.is_empty()) { | |
105 Done(false); | 142 Done(false); |
106 return; | 143 return; |
107 } | 144 } |
108 | 145 |
109 { | 146 delegate_->OpenBrowserWindow(open_link); |
110 chrome::ScopedTabbedBrowserDisplayer displayer( | |
111 profile_, chrome::HOST_DESKTOP_TYPE_ASH); | |
112 chrome::AddSelectedTabWithURL(displayer.browser(), open_link, | |
113 content::PAGE_TRANSITION_LINK); | |
114 // Since the ScopedTabbedBrowserDisplayer does not guarantee that the | |
115 // browser will be shown on the active desktop, we ensure the visibility. | |
116 multi_user_util::MoveWindowToCurrentDesktop( | |
117 displayer.browser()->window()->GetNativeWindow()); | |
118 } | |
119 | 147 |
120 // We're done with this file. If this is the last one, then we're done. | 148 // We're done with this file. If this is the last one, then we're done. |
121 current_index_--; | 149 current_index_--; |
122 DCHECK_GE(current_index_, 0); | 150 DCHECK_GE(current_index_, 0); |
123 if (current_index_ == 0) | 151 if (current_index_ == 0) |
124 Done(true); | 152 Done(true); |
125 } | 153 } |
126 | 154 |
127 void FileTaskExecutor::Done(bool success) { | 155 void FileTaskExecutor::Done(bool success) { |
128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 156 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
129 if (!done_.is_null()) | 157 if (!done_.is_null()) |
130 done_.Run(success | 158 done_.Run(success |
131 ? extensions::api::file_browser_private::TASK_RESULT_OPENED | 159 ? extensions::api::file_browser_private::TASK_RESULT_OPENED |
132 : extensions::api::file_browser_private::TASK_RESULT_FAILED); | 160 : extensions::api::file_browser_private::TASK_RESULT_FAILED); |
133 delete this; | 161 delete this; |
134 } | 162 } |
135 | 163 |
136 } // namespace drive | 164 } // namespace drive |
OLD | NEW |