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" |
(...skipping 19 matching lines...) Expand all Loading... |
30 current_index_(0), | 30 current_index_(0), |
31 weak_ptr_factory_(this) { | 31 weak_ptr_factory_(this) { |
32 } | 32 } |
33 | 33 |
34 FileTaskExecutor::~FileTaskExecutor() { | 34 FileTaskExecutor::~FileTaskExecutor() { |
35 } | 35 } |
36 | 36 |
37 void FileTaskExecutor::Execute( | 37 void FileTaskExecutor::Execute( |
38 const std::vector<FileSystemURL>& file_urls, | 38 const std::vector<FileSystemURL>& file_urls, |
39 const file_manager::file_tasks::FileTaskFinishedCallback& done) { | 39 const file_manager::file_tasks::FileTaskFinishedCallback& done) { |
| 40 done_ = done; |
| 41 |
40 std::vector<base::FilePath> paths; | 42 std::vector<base::FilePath> paths; |
41 for (size_t i = 0; i < file_urls.size(); ++i) { | 43 for (size_t i = 0; i < file_urls.size(); ++i) { |
42 base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]); | 44 base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]); |
43 if (path.empty()) { | 45 if (path.empty()) { |
44 Done(false); | 46 Done(false); |
45 return; | 47 return; |
46 } | 48 } |
47 paths.push_back(path); | 49 paths.push_back(path); |
48 } | 50 } |
49 | 51 |
50 DriveIntegrationService* integration_service = | 52 FileSystemInterface* file_system = util::GetFileSystemByProfile(profile_); |
51 DriveIntegrationServiceFactory::GetForProfile(profile_); | 53 if (!file_system) { |
52 DCHECK_EQ(current_index_, 0); | |
53 if (!integration_service || !integration_service->file_system()) { | |
54 Done(false); | 54 Done(false); |
55 return; | 55 return; |
56 } | 56 } |
57 FileSystemInterface* file_system = integration_service->file_system(); | |
58 | 57 |
59 done_ = done; | |
60 // Reset the index, so we know when we're done. | 58 // Reset the index, so we know when we're done. |
| 59 DCHECK_EQ(current_index_, 0); |
61 current_index_ = paths.size(); | 60 current_index_ = paths.size(); |
62 | 61 |
63 for (size_t i = 0; i < paths.size(); ++i) { | 62 for (size_t i = 0; i < paths.size(); ++i) { |
64 file_system->GetResourceEntryByPath( | 63 file_system->GetResourceEntryByPath( |
65 paths[i], | 64 paths[i], |
66 base::Bind(&FileTaskExecutor::OnFileEntryFetched, | 65 base::Bind(&FileTaskExecutor::OnFileEntryFetched, |
67 weak_ptr_factory_.GetWeakPtr())); | 66 weak_ptr_factory_.GetWeakPtr())); |
68 } | 67 } |
69 } | 68 } |
70 | 69 |
71 void FileTaskExecutor::OnFileEntryFetched(FileError error, | 70 void FileTaskExecutor::OnFileEntryFetched(FileError error, |
72 scoped_ptr<ResourceEntry> entry) { | 71 scoped_ptr<ResourceEntry> entry) { |
73 DriveIntegrationService* integration_service = | |
74 DriveIntegrationServiceFactory::GetForProfile(profile_); | |
75 | |
76 // Here, we are only interested in files. | 72 // Here, we are only interested in files. |
77 if (entry.get() && !entry->has_file_specific_info()) | 73 if (entry.get() && !entry->has_file_specific_info()) |
78 error = FILE_ERROR_NOT_FOUND; | 74 error = FILE_ERROR_NOT_FOUND; |
79 | 75 |
80 if (!integration_service || error != FILE_ERROR_OK) { | 76 DriveServiceInterface* drive_service = |
| 77 util::GetDriveServiceByProfile(profile_); |
| 78 |
| 79 if (!drive_service || error != FILE_ERROR_OK) { |
81 Done(false); | 80 Done(false); |
82 return; | 81 return; |
83 } | 82 } |
84 | 83 |
85 DriveServiceInterface* drive_service = | |
86 integration_service->drive_service(); | |
87 | |
88 // Send off a request for the drive service to authorize the apps for the | 84 // Send off a request for the drive service to authorize the apps for the |
89 // current document entry for this document so we can get the | 85 // current document entry for this document so we can get the |
90 // open-with-<app_id> urls from the document entry. | 86 // open-with-<app_id> urls from the document entry. |
91 drive_service->AuthorizeApp(entry->resource_id(), | 87 drive_service->AuthorizeApp(entry->resource_id(), |
92 app_id_, | 88 app_id_, |
93 base::Bind(&FileTaskExecutor::OnAppAuthorized, | 89 base::Bind(&FileTaskExecutor::OnAppAuthorized, |
94 weak_ptr_factory_.GetWeakPtr(), | 90 weak_ptr_factory_.GetWeakPtr(), |
95 entry->resource_id())); | 91 entry->resource_id())); |
96 } | 92 } |
97 | 93 |
98 void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id, | 94 void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id, |
99 google_apis::GDataErrorCode error, | 95 google_apis::GDataErrorCode error, |
100 const GURL& open_link) { | 96 const GURL& open_link) { |
101 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
102 | 98 |
| 99 // TODO(hidehiko): GetForProfile will return the instance always even if |
| 100 // Drive is disabled. Need to check the mounting state then. |
| 101 // crbug.com/284972 |
103 DriveIntegrationService* integration_service = | 102 DriveIntegrationService* integration_service = |
104 DriveIntegrationServiceFactory::GetForProfile(profile_); | 103 DriveIntegrationServiceFactory::GetForProfile(profile_); |
105 | 104 |
106 if (!integration_service || error != google_apis::HTTP_SUCCESS) { | 105 if (!integration_service || error != google_apis::HTTP_SUCCESS || |
| 106 open_link.is_empty()) { |
107 Done(false); | 107 Done(false); |
108 return; | 108 return; |
109 } | 109 } |
110 | |
111 if (open_link.is_empty()) { | |
112 Done(false); | |
113 return; | |
114 } | |
115 | 110 |
116 Browser* browser = chrome::FindOrCreateTabbedBrowser( | 111 Browser* browser = chrome::FindOrCreateTabbedBrowser( |
117 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord(), | 112 profile_ ? profile_ : ProfileManager::GetDefaultProfileOrOffTheRecord(), |
118 chrome::HOST_DESKTOP_TYPE_ASH); | 113 chrome::HOST_DESKTOP_TYPE_ASH); |
119 | 114 |
120 chrome::AddSelectedTabWithURL(browser, open_link, | 115 chrome::AddSelectedTabWithURL(browser, open_link, |
121 content::PAGE_TRANSITION_LINK); | 116 content::PAGE_TRANSITION_LINK); |
122 // If the current browser is not tabbed then the new tab will be created | 117 // If the current browser is not tabbed then the new tab will be created |
123 // in a different browser. Make sure it is visible. | 118 // in a different browser. Make sure it is visible. |
124 browser->window()->Show(); | 119 browser->window()->Show(); |
125 | 120 |
126 // We're done with this file. If this is the last one, then we're done. | 121 // We're done with this file. If this is the last one, then we're done. |
127 current_index_--; | 122 current_index_--; |
128 DCHECK_GE(current_index_, 0); | 123 DCHECK_GE(current_index_, 0); |
129 if (current_index_ == 0) | 124 if (current_index_ == 0) |
130 Done(true); | 125 Done(true); |
131 } | 126 } |
132 | 127 |
133 void FileTaskExecutor::Done(bool success) { | 128 void FileTaskExecutor::Done(bool success) { |
134 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 129 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
135 if (!done_.is_null()) | 130 if (!done_.is_null()) |
136 done_.Run(success); | 131 done_.Run(success); |
137 delete this; | 132 delete this; |
138 } | 133 } |
139 | 134 |
140 } // namespace drive | 135 } // namespace drive |
OLD | NEW |