summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/drive/file_task_executor.cc
blob: e109011e9d4c8761de62b176231fac774ded45fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/drive/file_task_executor.h"

#include <stddef.h>
#include <string>
#include <utility>
#include <vector>

#include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/multi_user/multi_user_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/scoped_tabbed_browser_displayer.h"
#include "chrome/common/extensions/api/file_manager_private.h"
#include "components/drive/drive.pb.h"
#include "components/drive/file_system_interface.h"
#include "components/drive/service/drive_service_interface.h"
#include "content/public/browser/browser_thread.h"
#include "storage/browser/fileapi/file_system_url.h"

using storage::FileSystemURL;

namespace drive {

namespace {

class FileTaskExecutorDelegateImpl : public FileTaskExecutorDelegate {
 public:
  explicit FileTaskExecutorDelegateImpl(Profile* profile) : profile_(profile) {
  }

  FileSystemInterface* GetFileSystem() override {
    return util::GetFileSystemByProfile(profile_);
  }

  DriveServiceInterface* GetDriveService() override {
    return util::GetDriveServiceByProfile(profile_);
  }

  void OpenBrowserWindow(const GURL& open_link) override {
    chrome::ScopedTabbedBrowserDisplayer displayer(profile_);
    chrome::AddSelectedTabWithURL(displayer.browser(), open_link,
                                  ui::PAGE_TRANSITION_LINK);
    // Since the ScopedTabbedBrowserDisplayer does not guarantee that the
    // browser will be shown on the active desktop, we ensure the visibility.
    multi_user_util::MoveWindowToCurrentDesktop(
        displayer.browser()->window()->GetNativeWindow());
  }

 private:
  Profile* const profile_;
};

}  // namespace

FileTaskExecutor::FileTaskExecutor(Profile* profile, const std::string& app_id)
  : delegate_(new FileTaskExecutorDelegateImpl(profile)),
    app_id_(app_id),
    current_index_(0),
    weak_ptr_factory_(this) {
}

FileTaskExecutor::FileTaskExecutor(
    scoped_ptr<FileTaskExecutorDelegate> delegate,
    const std::string& app_id)
    : delegate_(std::move(delegate)),
      app_id_(app_id),
      current_index_(0),
      weak_ptr_factory_(this) {}

FileTaskExecutor::~FileTaskExecutor() {
}

void FileTaskExecutor::Execute(
    const std::vector<FileSystemURL>& file_urls,
    const file_manager::file_tasks::FileTaskFinishedCallback& done) {
  DCHECK(!file_urls.empty());

  done_ = done;

  std::vector<base::FilePath> paths;
  for (size_t i = 0; i < file_urls.size(); ++i) {
    base::FilePath path = util::ExtractDrivePathFromFileSystemUrl(file_urls[i]);
    if (path.empty()) {
      Done(false);
      return;
    }
    paths.push_back(path);
  }

  FileSystemInterface* const file_system = delegate_->GetFileSystem();
  if (!file_system) {
    Done(false);
    return;
  }

  // Reset the index, so we know when we're done.
  DCHECK_EQ(current_index_, 0);
  current_index_ = paths.size();

  for (size_t i = 0; i < paths.size(); ++i) {
    file_system->GetResourceEntry(
        paths[i],
        base::Bind(&FileTaskExecutor::OnFileEntryFetched,
                   weak_ptr_factory_.GetWeakPtr()));
  }
}

void FileTaskExecutor::OnFileEntryFetched(FileError error,
                                          scoped_ptr<ResourceEntry> entry) {
  // Here, we are only interested in files.
  if (entry.get() && !entry->has_file_specific_info())
    error = FILE_ERROR_NOT_FOUND;

  DriveServiceInterface* const drive_service = delegate_->GetDriveService();
  if (!drive_service || error != FILE_ERROR_OK) {
    Done(false);
    return;
  }

  // Send off a request for the drive service to authorize the apps for the
  // current document entry for this document so we can get the
  // open-with-<app_id> urls from the document entry.
  drive_service->AuthorizeApp(entry->resource_id(),
                              app_id_,
                              base::Bind(&FileTaskExecutor::OnAppAuthorized,
                                         weak_ptr_factory_.GetWeakPtr(),
                                         entry->resource_id()));
}

void FileTaskExecutor::OnAppAuthorized(const std::string& resource_id,
                                       google_apis::DriveApiErrorCode error,
                                       const GURL& open_link) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

  if (error != google_apis::HTTP_SUCCESS || open_link.is_empty()) {
    Done(false);
    return;
  }

  delegate_->OpenBrowserWindow(open_link);

  // We're done with this file.  If this is the last one, then we're done.
  current_index_--;
  DCHECK_GE(current_index_, 0);
  if (current_index_ == 0)
    Done(true);
}

void FileTaskExecutor::Done(bool success) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  if (!done_.is_null())
    done_.Run(success
                  ? extensions::api::file_manager_private::TASK_RESULT_OPENED
                  : extensions::api::file_manager_private::TASK_RESULT_FAILED);
  delete this;
}

}  // namespace drive