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/drive/file_task_executor.h"
6
7 #include <string>
8 #include <vector>
9
10 #include "chrome/browser/chromeos/drive/drive.pb.h"
11 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
12 #include "chrome/browser/chromeos/drive/file_system_interface.h"
13 #include "chrome/browser/drive/drive_service_interface.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
16 #include "chrome/browser/ui/browser.h"
17 #include "chrome/browser/ui/browser_tabstrip.h"
18 #include "chrome/browser/ui/browser_window.h"
19 #include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
20 #include "chrome/common/extensions/api/file_browser_private.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "webkit/browser/fileapi/file_system_url.h"
23
24 using fileapi::FileSystemURL;
25
26 namespace drive {
27
FileTaskExecutor(Profile * profile,const std::string & app_id)28 FileTaskExecutor::FileTaskExecutor(Profile* profile,
29 const std::string& app_id)
30 : profile_(profile),
31 app_id_(app_id),
32 current_index_(0),
33 weak_ptr_factory_(this) {
34 }
35
~FileTaskExecutor()36 FileTaskExecutor::~FileTaskExecutor() {
37 }
38
Execute(const std::vector<FileSystemURL> & file_urls,const file_manager::file_tasks::FileTaskFinishedCallback & done)39 void FileTaskExecutor::Execute(
40 const std::vector<FileSystemURL>& file_urls,
41 const file_manager::file_tasks::FileTaskFinishedCallback& done) {
42 done_ = done;
43
44 std::vector<base::FilePath> paths;
45 for (size_t i = 0; i < file_urls.size(); ++i) {
46 base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]);
47 if (path.empty()) {
48 Done(false);
49 return;
50 }
51 paths.push_back(path);
52 }
53
54 FileSystemInterface* file_system = util::GetFileSystemByProfile(profile_);
55 if (!file_system) {
56 Done(false);
57 return;
58 }
59
60 // Reset the index, so we know when we're done.
61 DCHECK_EQ(current_index_, 0);
62 current_index_ = paths.size();
63
64 for (size_t i = 0; i < paths.size(); ++i) {
65 file_system->GetResourceEntry(
66 paths[i],
67 base::Bind(&FileTaskExecutor::OnFileEntryFetched,
68 weak_ptr_factory_.GetWeakPtr()));
69 }
70 }
71
OnFileEntryFetched(FileError error,scoped_ptr<ResourceEntry> entry)72 void FileTaskExecutor::OnFileEntryFetched(FileError error,
73 scoped_ptr<ResourceEntry> entry) {
74 // Here, we are only interested in files.
75 if (entry.get() && !entry->has_file_specific_info())
76 error = FILE_ERROR_NOT_FOUND;
77
78 DriveServiceInterface* drive_service =
79 util::GetDriveServiceByProfile(profile_);
80
81 if (!drive_service || error != FILE_ERROR_OK) {
82 Done(false);
83 return;
84 }
85
86 // 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
88 // open-with-<app_id> urls from the document entry.
89 drive_service->AuthorizeApp(entry->resource_id(),
90 app_id_,
91 base::Bind(&FileTaskExecutor::OnAppAuthorized,
92 weak_ptr_factory_.GetWeakPtr(),
93 entry->resource_id()));
94 }
95
OnAppAuthorized(const std::string & resource_id,google_apis::GDataErrorCode error,const GURL & open_link)96 void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id,
97 google_apis::GDataErrorCode error,
98 const GURL& open_link) {
99 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
100
101 DriveIntegrationService* service =
102 DriveIntegrationServiceFactory::FindForProfile(profile_);
103 if (!service || !service->IsMounted() ||
104 error != google_apis::HTTP_SUCCESS || open_link.is_empty()) {
105 Done(false);
106 return;
107 }
108
109 {
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
120 // We're done with this file. If this is the last one, then we're done.
121 current_index_--;
122 DCHECK_GE(current_index_, 0);
123 if (current_index_ == 0)
124 Done(true);
125 }
126
Done(bool success)127 void FileTaskExecutor::Done(bool success) {
128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
129 if (!done_.is_null())
130 done_.Run(success
131 ? extensions::api::file_browser_private::TASK_RESULT_OPENED
132 : extensions::api::file_browser_private::TASK_RESULT_FAILED);
133 delete this;
134 }
135
136 } // namespace drive
137