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/drive_app_registry.h" | 5 #include "chrome/browser/chromeos/drive/drive_app_registry.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <set> |
9 #include <utility> | 9 #include <utility> |
10 #include <vector> | |
11 | 10 |
12 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
13 #include "base/strings/string_util.h" | 12 #include "chrome/browser/drive/drive_service_interface.h" |
14 #include "chrome/browser/chromeos/drive/file_system_util.h" | |
15 #include "chrome/browser/chromeos/drive/job_scheduler.h" | |
16 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
17 #include "google_apis/drive/drive_api_parser.h" | 14 #include "google_apis/drive/drive_api_parser.h" |
18 | 15 |
19 using content::BrowserThread; | 16 using content::BrowserThread; |
20 | 17 |
21 namespace { | 18 namespace { |
22 | 19 |
23 // Add {selector -> app_id} mapping to |map|. | 20 // Add {selector -> app_id} mapping to |map|. |
24 void AddAppSelectorList(const ScopedVector<std::string>& selectors, | 21 void AddAppSelectorList(const ScopedVector<std::string>& selectors, |
25 const std::string& app_id, | 22 const std::string& app_id, |
(...skipping 28 matching lines...) Expand all Loading... |
54 : app_id(app_id), | 51 : app_id(app_id), |
55 app_icons(app_icons), | 52 app_icons(app_icons), |
56 document_icons(document_icons), | 53 document_icons(document_icons), |
57 app_name(app_name), | 54 app_name(app_name), |
58 create_url(create_url) { | 55 create_url(create_url) { |
59 } | 56 } |
60 | 57 |
61 DriveAppInfo::~DriveAppInfo() { | 58 DriveAppInfo::~DriveAppInfo() { |
62 } | 59 } |
63 | 60 |
64 DriveAppRegistry::DriveAppRegistry(JobScheduler* scheduler) | 61 DriveAppRegistry::DriveAppRegistry(DriveServiceInterface* drive_service) |
65 : scheduler_(scheduler), | 62 : drive_service_(drive_service), |
66 is_updating_(false), | 63 is_updating_(false), |
67 weak_ptr_factory_(this) { | 64 weak_ptr_factory_(this) { |
68 } | 65 } |
69 | 66 |
70 DriveAppRegistry::~DriveAppRegistry() { | 67 DriveAppRegistry::~DriveAppRegistry() { |
71 } | 68 } |
72 | 69 |
73 void DriveAppRegistry::GetAppsForFile( | 70 void DriveAppRegistry::GetAppsForFile( |
74 const base::FilePath::StringType& file_extension, | 71 const base::FilePath::StringType& file_extension, |
75 const std::string& mime_type, | 72 const std::string& mime_type, |
76 ScopedVector<DriveAppInfo>* apps) const { | 73 std::vector<DriveAppInfo>* apps) const { |
77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
78 | 75 |
79 std::vector<std::string> matched_apps; | 76 std::vector<std::string> matched_apps; |
80 if (!file_extension.empty()) { | 77 if (!file_extension.empty()) { |
81 const base::FilePath::StringType without_dot = file_extension.substr(1); | 78 const base::FilePath::StringType without_dot = file_extension.substr(1); |
82 FindAppsForSelector(without_dot, extension_map_, &matched_apps); | 79 FindAppsForSelector(without_dot, extension_map_, &matched_apps); |
83 } | 80 } |
84 if (!mime_type.empty()) | 81 if (!mime_type.empty()) |
85 FindAppsForSelector(mime_type, mimetype_map_, &matched_apps); | 82 FindAppsForSelector(mime_type, mimetype_map_, &matched_apps); |
86 | 83 |
87 // Insert found Drive apps into |apps|, but skip duplicate results. | 84 // Insert found Drive apps into |apps|, but skip duplicate results. |
88 std::set<std::string> inserted_app_ids; | 85 std::set<std::string> inserted_app_ids; |
89 for (size_t i = 0; i < matched_apps.size(); ++i) { | 86 for (size_t i = 0; i < matched_apps.size(); ++i) { |
90 if (inserted_app_ids.count(matched_apps[i]) == 0) { | 87 if (inserted_app_ids.count(matched_apps[i]) == 0) { |
91 inserted_app_ids.insert(matched_apps[i]); | 88 inserted_app_ids.insert(matched_apps[i]); |
92 std::map<std::string, DriveAppInfo>::const_iterator it = | 89 std::map<std::string, DriveAppInfo>::const_iterator it = |
93 all_apps_.find(matched_apps[i]); | 90 all_apps_.find(matched_apps[i]); |
94 DCHECK(it != all_apps_.end()); | 91 DCHECK(it != all_apps_.end()); |
95 apps->push_back(new DriveAppInfo(it->second)); | 92 apps->push_back(it->second); |
96 } | 93 } |
97 } | 94 } |
98 } | 95 } |
99 | 96 |
100 void DriveAppRegistry::Update() { | 97 void DriveAppRegistry::Update() { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
102 | 99 |
103 if (is_updating_) // There is already an update in progress. | 100 if (is_updating_) // There is already an update in progress. |
104 return; | 101 return; |
105 is_updating_ = true; | 102 is_updating_ = true; |
106 | 103 |
107 scheduler_->GetAppList(base::Bind(&DriveAppRegistry::UpdateAfterGetAppList, | 104 drive_service_->GetAppList( |
108 weak_ptr_factory_.GetWeakPtr())); | 105 base::Bind(&DriveAppRegistry::UpdateAfterGetAppList, |
| 106 weak_ptr_factory_.GetWeakPtr())); |
109 } | 107 } |
110 | 108 |
111 void DriveAppRegistry::UpdateAfterGetAppList( | 109 void DriveAppRegistry::UpdateAfterGetAppList( |
112 google_apis::GDataErrorCode gdata_error, | 110 google_apis::GDataErrorCode gdata_error, |
113 scoped_ptr<google_apis::AppList> app_list) { | 111 scoped_ptr<google_apis::AppList> app_list) { |
114 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
115 | 113 |
116 DCHECK(is_updating_); | 114 DCHECK(is_updating_); |
117 is_updating_ = false; | 115 is_updating_ = false; |
118 | 116 |
119 FileError error = GDataToFileError(gdata_error); | 117 // Failed to fetch the data from the server. We can do nothing here. |
120 if (error != FILE_ERROR_OK) { | 118 if (gdata_error != google_apis::HTTP_SUCCESS) |
121 // Failed to fetch the data from the server. We can do nothing here. | |
122 return; | 119 return; |
123 } | |
124 | 120 |
125 DCHECK(app_list); | 121 DCHECK(app_list); |
126 UpdateFromAppList(*app_list); | 122 UpdateFromAppList(*app_list); |
127 } | 123 } |
128 | 124 |
129 void DriveAppRegistry::UpdateFromAppList(const google_apis::AppList& app_list) { | 125 void DriveAppRegistry::UpdateFromAppList(const google_apis::AppList& app_list) { |
130 all_apps_.clear(); | 126 all_apps_.clear(); |
131 extension_map_.clear(); | 127 extension_map_.clear(); |
132 mimetype_map_.clear(); | 128 mimetype_map_.clear(); |
133 | 129 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 | 171 |
176 // Go forward while the size is larger or equal to preferred_size. | 172 // Go forward while the size is larger or equal to preferred_size. |
177 size_t i = 1; | 173 size_t i = 1; |
178 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size) | 174 while (i < sorted_icons.size() && sorted_icons[i].first >= preferred_size) |
179 ++i; | 175 ++i; |
180 return sorted_icons[i - 1].second; | 176 return sorted_icons[i - 1].second; |
181 } | 177 } |
182 | 178 |
183 } // namespace util | 179 } // namespace util |
184 } // namespace drive | 180 } // namespace drive |
OLD | NEW |