OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 /** | 5 /** |
6 * Provides drive search results to chrome launcher. | 6 * Provides drive search results to chrome launcher. |
7 * @constructor | 7 * @constructor |
8 * @struct | 8 * @struct |
9 */ | 9 */ |
10 function LauncherSearch() { | 10 function LauncherSearch() { |
(...skipping 16 matching lines...) Expand all Loading... |
27 /** | 27 /** |
28 * @private {function(string, string, number)} | 28 * @private {function(string, string, number)} |
29 */ | 29 */ |
30 this.onQueryStartedBound_ = this.onQueryStarted_.bind(this); | 30 this.onQueryStartedBound_ = this.onQueryStarted_.bind(this); |
31 | 31 |
32 /** | 32 /** |
33 * @private {function(string)} | 33 * @private {function(string)} |
34 */ | 34 */ |
35 this.onQueryEndedBound_ = this.onQueryEnded_.bind(this); | 35 this.onQueryEndedBound_ = this.onQueryEnded_.bind(this); |
36 | 36 |
| 37 /** |
| 38 * @private {function(string)} |
| 39 */ |
| 40 this.onOpenResultBound_ = this.onOpenResult_.bind(this); |
| 41 |
37 // This feature is disabled when drive is disabled. | 42 // This feature is disabled when drive is disabled. |
38 chrome.fileManagerPrivate.onPreferencesChanged.addListener( | 43 chrome.fileManagerPrivate.onPreferencesChanged.addListener( |
39 this.onPreferencesChanged_.bind(this)); | 44 this.onPreferencesChanged_.bind(this)); |
40 this.onPreferencesChanged_(); | 45 this.onPreferencesChanged_(); |
41 } | 46 } |
42 | 47 |
43 /** | 48 /** |
44 * Handles onPreferencesChanged event. | 49 * Handles onPreferencesChanged event. |
45 */ | 50 */ |
46 LauncherSearch.prototype.onPreferencesChanged_ = function() { | 51 LauncherSearch.prototype.onPreferencesChanged_ = function() { |
(...skipping 15 matching lines...) Expand all Loading... |
62 // If this.enabled_ === isDriveEnabled, we don't need to change anything here. | 67 // If this.enabled_ === isDriveEnabled, we don't need to change anything here. |
63 if (this.enabled_ === isDriveEnabled) | 68 if (this.enabled_ === isDriveEnabled) |
64 return; | 69 return; |
65 | 70 |
66 // Remove event listeners if it's already enabled. | 71 // Remove event listeners if it's already enabled. |
67 if (this.enabled_) { | 72 if (this.enabled_) { |
68 chrome.launcherSearchProvider.onQueryStarted.removeListener( | 73 chrome.launcherSearchProvider.onQueryStarted.removeListener( |
69 this.onQueryStartedBound_); | 74 this.onQueryStartedBound_); |
70 chrome.launcherSearchProvider.onQueryEnded.removeListener( | 75 chrome.launcherSearchProvider.onQueryEnded.removeListener( |
71 this.onQueryEndedBound_); | 76 this.onQueryEndedBound_); |
| 77 chrome.launcherSearchProvider.onOpenResult.removeListener( |
| 78 this.onOpenResultBound_); |
72 } | 79 } |
73 | 80 |
74 // Set queryId_ to null to prevent that on-going search returns search | 81 // Set queryId_ to null to prevent that on-going search returns search |
75 // results. | 82 // results. |
76 this.queryId_ = null; | 83 this.queryId_ = null; |
77 | 84 |
78 // Add event listeners when drive is enabled. | 85 // Add event listeners when drive is enabled. |
79 if (isDriveEnabled) { | 86 if (isDriveEnabled) { |
80 this.enabled_ = true; | 87 this.enabled_ = true; |
81 chrome.launcherSearchProvider.onQueryStarted.addListener( | 88 chrome.launcherSearchProvider.onQueryStarted.addListener( |
82 this.onQueryStartedBound_); | 89 this.onQueryStartedBound_); |
83 chrome.launcherSearchProvider.onQueryEnded.addListener( | 90 chrome.launcherSearchProvider.onQueryEnded.addListener( |
84 this.onQueryEndedBound_); | 91 this.onQueryEndedBound_); |
85 // TODO(yawano): Adds listener to onOpenResult when it becomes available. | 92 chrome.launcherSearchProvider.onOpenResult.addListener( |
| 93 this.onOpenResultBound_); |
86 } else { | 94 } else { |
87 this.enabled_ = false; | 95 this.enabled_ = false; |
88 } | 96 } |
89 }; | 97 }; |
90 | 98 |
91 /** | 99 /** |
92 * Handles onQueryStarted event. | 100 * Handles onQueryStarted event. |
93 * @param {string} queryId | 101 * @param {string} queryId |
94 * @param {string} query | 102 * @param {string} query |
95 * @param {number} limit | 103 * @param {number} limit |
(...skipping 28 matching lines...) Expand all Loading... |
124 }.bind(this)); | 132 }.bind(this)); |
125 }; | 133 }; |
126 | 134 |
127 /** | 135 /** |
128 * Handles onQueryEnded event. | 136 * Handles onQueryEnded event. |
129 * @param {string} queryId | 137 * @param {string} queryId |
130 */ | 138 */ |
131 LauncherSearch.prototype.onQueryEnded_ = function(queryId) { | 139 LauncherSearch.prototype.onQueryEnded_ = function(queryId) { |
132 this.queryId_ = null; | 140 this.queryId_ = null; |
133 }; | 141 }; |
| 142 |
| 143 /** |
| 144 * Handles onOpenResult event. |
| 145 * @param {string} itemId |
| 146 */ |
| 147 LauncherSearch.prototype.onOpenResult_ = function(itemId) { |
| 148 util.urlToEntry(itemId).then(function(entry) { |
| 149 if (entry.isDirectory) { |
| 150 // If it's directory, open the directory with file manager. |
| 151 launchFileManager( |
| 152 { currentDirectoryURL: entry.toURL() }, |
| 153 undefined, /* App ID */ |
| 154 LaunchType.FOCUS_SAME_OR_CREATE); |
| 155 } else { |
| 156 // If the file is not directory, try to execute default task. |
| 157 chrome.fileManagerPrivate.getFileTasks([entry.toURL()], function(tasks) { |
| 158 // Select default task. |
| 159 var defaultTask = null; |
| 160 for (var i = 0; i < tasks.length; i++) { |
| 161 var task = tasks[i]; |
| 162 if (task.isDefault) { |
| 163 defaultTask = task; |
| 164 break; |
| 165 } |
| 166 } |
| 167 |
| 168 if (defaultTask) { |
| 169 // Execute default task. |
| 170 chrome.fileManagerPrivate.executeTask( |
| 171 defaultTask.taskId, |
| 172 [entry.toURL()], |
| 173 function(result) { |
| 174 if (result === 'opened' || result === 'message_sent') |
| 175 return; |
| 176 this.openFileManagerWithSelectionURL_(entry.toURL()); |
| 177 }.bind(this)); |
| 178 } else { |
| 179 // If there is no default task for the url, open a file manager with |
| 180 // selecting it. |
| 181 // TODO(yawano): Add fallback to view-in-browser as file_tasks.js do. |
| 182 this.openFileManagerWithSelectionURL_(entry.toURL()); |
| 183 } |
| 184 }.bind(this)); |
| 185 } |
| 186 }.bind(this)); |
| 187 }; |
| 188 |
| 189 /** |
| 190 * Opens file manager with selecting a specified url. |
| 191 * @param {string} selectionURL A url to be selected. |
| 192 * @private |
| 193 */ |
| 194 LauncherSearch.prototype.openFileManagerWithSelectionURL_ = function( |
| 195 selectionURL) { |
| 196 launchFileManager( |
| 197 { selectionURL: selectionURL }, |
| 198 undefined, /* App ID */ |
| 199 LaunchType.FOCUS_SAME_OR_CREATE); |
| 200 }; |
OLD | NEW |