summaryrefslogtreecommitdiffstats
path: root/chrome/browser/media_galleries/media_galleries_dialog_controller.cc
blob: 27b876587edfe9787f6f72678daa47548355ec76 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
// 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/media_galleries/media_galleries_dialog_controller.h"

#include "base/i18n/time_formatting.h"
#include "base/path_service.h"
#include "base/stl_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/media_galleries/media_file_system_registry.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/storage_monitor/media_storage_util.h"
#include "chrome/browser/storage_monitor/storage_info.h"
#include "chrome/browser/ui/chrome_select_file_policy.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/extensions/extension.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_view.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

using extensions::Extension;

namespace chrome {

namespace {

bool IsAttachedDevice(const std::string& device_id) {
  if (!MediaStorageUtil::IsRemovableDevice(device_id))
    return false;

  std::vector<StorageInfo> removable_storages =
      StorageMonitor::GetInstance()->GetAttachedStorage();
  for (size_t i = 0; i < removable_storages.size(); ++i) {
    if (removable_storages[i].device_id == device_id)
      return true;
  }
  return false;
}

}  // namespace

MediaGalleriesDialogController::MediaGalleriesDialogController(
    content::WebContents* web_contents,
    const Extension& extension,
    const base::Closure& on_finish)
      : web_contents_(web_contents),
        extension_(&extension),
        on_finish_(on_finish) {
  MediaFileSystemRegistry* registry =
      g_browser_process->media_file_system_registry();
  preferences_ = registry->GetPreferences(
      Profile::FromBrowserContext(web_contents->GetBrowserContext()));
  InitializePermissions();

  dialog_.reset(MediaGalleriesDialog::Create(this));

  StorageMonitor* monitor = StorageMonitor::GetInstance();
  if (monitor)
    monitor->AddObserver(this);

  preferences_->AddGalleryChangeObserver(this);
}

MediaGalleriesDialogController::MediaGalleriesDialogController()
    : web_contents_(NULL),
      extension_(NULL),
      preferences_(NULL) {}

MediaGalleriesDialogController::~MediaGalleriesDialogController() {
  StorageMonitor* monitor = StorageMonitor::GetInstance();
  if (monitor)
    monitor->RemoveObserver(this);

  if (select_folder_dialog_.get())
    select_folder_dialog_->ListenerDestroyed();
}

// static
string16 MediaGalleriesDialogController::GetGalleryDisplayName(
    const MediaGalleryPrefInfo& gallery) {
  string16 name = gallery.display_name;
  if (IsAttachedDevice(gallery.device_id)) {
    name += ASCIIToUTF16(" ");
    name +=
        l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED);
  }
  return name;
}

// static
string16 MediaGalleriesDialogController::GetGalleryDisplayNameNoAttachment(
    const MediaGalleryPrefInfo& gallery) {
  string16 name = gallery.display_name;
  if (name.empty())
    name = gallery.volume_label;
  if (name.empty()) {
    name = MediaStorageUtil::GetDisplayNameForDevice(
      gallery.total_size_in_bytes,
      MediaStorageUtil::GetFullProductName(UTF16ToUTF8(gallery.vendor_name),
                                           UTF16ToUTF8(gallery.model_name)));
  }

  return name;
}

// static
string16 MediaGalleriesDialogController::GetGalleryTooltip(
    const MediaGalleryPrefInfo& gallery) {
  return gallery.AbsolutePath().LossyDisplayName();
}

// static
bool MediaGalleriesDialogController::GetGalleryAttached(
    const MediaGalleryPrefInfo& gallery) {
  return !MediaStorageUtil::IsRemovableDevice(gallery.device_id) ||
         IsAttachedDevice(gallery.device_id);
}

// static
string16 MediaGalleriesDialogController::GetGalleryAdditionalDetails(
    const MediaGalleryPrefInfo& gallery) {
  string16 attached;
  if (MediaStorageUtil::IsRemovableDevice(gallery.device_id)) {
    if (IsAttachedDevice(gallery.device_id)) {
      attached = l10n_util::GetStringUTF16(
          IDS_MEDIA_GALLERIES_DIALOG_DEVICE_ATTACHED);
    } else if (!gallery.last_attach_time.is_null()) {
      attached = l10n_util::GetStringFUTF16(
          IDS_MEDIA_GALLERIES_LAST_ATTACHED,
          base::TimeFormatShortDateNumeric(gallery.last_attach_time));
    } else {
      attached = l10n_util::GetStringUTF16(
          IDS_MEDIA_GALLERIES_DIALOG_DEVICE_NOT_ATTACHED);
    }
  }

  // TODO(gbillock): This is a placeholder. Need to actually get the
  // right text here.
  return attached;
}

string16 MediaGalleriesDialogController::GetHeader() const {
  return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_HEADER);
}

string16 MediaGalleriesDialogController::GetSubtext() const {
  std::string extension_name(extension_ ? extension_->name() : std::string());
  return l10n_util::GetStringFUTF16(IDS_MEDIA_GALLERIES_DIALOG_SUBTEXT,
                                    UTF8ToUTF16(extension_name));
}

string16 MediaGalleriesDialogController::GetUnattachedLocationsHeader() const {
  return l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_UNATTACHED_LOCATIONS);
}

bool MediaGalleriesDialogController::HasPermittedGalleries() const {
  for (KnownGalleryPermissions::const_iterator iter = permissions().begin();
       iter != permissions().end(); ++iter) {
    if (iter->second.allowed)
      return true;
  }
  return false;
}

const MediaGalleriesDialogController::KnownGalleryPermissions&
    MediaGalleriesDialogController::permissions() const {
  return known_galleries_;
}

const MediaGalleriesDialogController::NewGalleryPermissions&
    MediaGalleriesDialogController::new_permissions() const {
  return new_galleries_;
}

void MediaGalleriesDialogController::OnAddFolderClicked() {
  base::FilePath user_data_dir;
  PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
  select_folder_dialog_ =
      ui::SelectFileDialog::Create(this, new ChromeSelectFilePolicy(NULL));
  select_folder_dialog_->SelectFile(
      ui::SelectFileDialog::SELECT_FOLDER,
      l10n_util::GetStringUTF16(IDS_MEDIA_GALLERIES_DIALOG_ADD_GALLERY_TITLE),
      user_data_dir,
      NULL,
      0,
      FILE_PATH_LITERAL(std::string()),
      web_contents_->GetView()->GetTopLevelNativeWindow(),
      NULL);
}

void MediaGalleriesDialogController::DidToggleGallery(
    const MediaGalleryPrefInfo* gallery,
    bool enabled) {
  // Check known galleries.
  KnownGalleryPermissions::iterator iter =
      known_galleries_.find(gallery->pref_id);
  if (iter != known_galleries_.end()) {
    if (iter->second.allowed == enabled)
      return;

    iter->second.allowed = enabled;
    if (ContainsKey(toggled_galleries_, gallery->pref_id))
      toggled_galleries_.erase(gallery->pref_id);
    else
      toggled_galleries_.insert(gallery->pref_id);
    return;
  }

  // Check new galleries.
  for (NewGalleryPermissions::iterator iter = new_galleries_.begin();
       iter != new_galleries_.end(); ++iter) {
    if (&iter->pref_info == gallery) {
      iter->allowed = enabled;
      return;
    }
  }
}

void MediaGalleriesDialogController::DialogFinished(bool accepted) {
  // The dialog has finished, so there is no need to watch for more updates
  // from |preferences_|. Do this here and not in the dtor since this is the
  // only non-test code path that deletes |this|. The test ctor never adds
  // this observer in the first place.
  preferences_->RemoveGalleryChangeObserver(this);

  if (accepted)
    SavePermissions();

  on_finish_.Run();
  delete this;
}

content::WebContents* MediaGalleriesDialogController::web_contents() {
  return web_contents_;
}

void MediaGalleriesDialogController::FileSelected(const base::FilePath& path,
                                                  int /*index*/,
                                                  void* /*params*/) {
  // Try to find it in the prefs.
  MediaGalleryPrefInfo gallery;
  bool gallery_exists = preferences_->LookUpGalleryByPath(path, &gallery);
  if (gallery_exists && gallery.type != MediaGalleryPrefInfo::kBlackListed) {
    // The prefs are in sync with |known_galleries_|, so it should exist in
    // |known_galleries_| as well. User selecting a known gallery effectively
    // just sets the gallery to permitted.
    KnownGalleryPermissions::const_iterator iter =
        known_galleries_.find(gallery.pref_id);
    DCHECK(iter != known_galleries_.end());
    dialog_->UpdateGallery(&iter->second.pref_info, true);
    return;
  }

  // Try to find it in |new_galleries_| (user added same folder twice).
  for (NewGalleryPermissions::iterator iter = new_galleries_.begin();
       iter != new_galleries_.end(); ++iter) {
    if (iter->pref_info.path == gallery.path &&
        iter->pref_info.device_id == gallery.device_id) {
      iter->allowed = true;
      dialog_->UpdateGallery(&iter->pref_info, true);
      return;
    }
  }

  // Lastly, add a new gallery to |new_galleries_|.
  new_galleries_.push_back(GalleryPermission(gallery, true));
  dialog_->UpdateGallery(&new_galleries_.back().pref_info, true);
}

void MediaGalleriesDialogController::OnRemovableStorageAttached(
    const StorageInfo& info) {
  UpdateGalleriesOnDeviceEvent(info.device_id);
}

void MediaGalleriesDialogController::OnRemovableStorageDetached(
    const StorageInfo& info) {
  UpdateGalleriesOnDeviceEvent(info.device_id);
}

void MediaGalleriesDialogController::OnGalleryChanged(
    MediaGalleriesPreferences* pref, const std::string& extension_id) {
  DCHECK_EQ(preferences_, pref);
  if (extension_id.empty() || extension_id == extension_->id())
    UpdateGalleriesOnPreferencesEvent();
}

void MediaGalleriesDialogController::InitializePermissions() {
  const MediaGalleriesPrefInfoMap& galleries = preferences_->known_galleries();
  for (MediaGalleriesPrefInfoMap::const_iterator iter = galleries.begin();
       iter != galleries.end();
       ++iter) {
    const MediaGalleryPrefInfo& gallery = iter->second;
    if (gallery.type == MediaGalleryPrefInfo::kBlackListed)
      continue;

    known_galleries_[iter->first] = GalleryPermission(gallery, false);
  }

  MediaGalleryPrefIdSet permitted =
      preferences_->GalleriesForExtension(*extension_);

  for (MediaGalleryPrefIdSet::iterator iter = permitted.begin();
       iter != permitted.end(); ++iter) {
    if (ContainsKey(toggled_galleries_, *iter))
      continue;
    DCHECK(ContainsKey(known_galleries_, *iter));
    known_galleries_[*iter].allowed = true;
  }
}

void MediaGalleriesDialogController::SavePermissions() {
  for (KnownGalleryPermissions::const_iterator iter = known_galleries_.begin();
       iter != known_galleries_.end(); ++iter) {
    preferences_->SetGalleryPermissionForExtension(
        *extension_, iter->first, iter->second.allowed);
  }

  for (NewGalleryPermissions::const_iterator iter = new_galleries_.begin();
       iter != new_galleries_.end(); ++iter) {
    // If the user added a gallery then unchecked it, forget about it.
    if (!iter->allowed)
      continue;

    // TODO(gbillock): Should be adding volume metadata during FileSelected.
    const MediaGalleryPrefInfo& gallery = iter->pref_info;
    MediaGalleryPrefId id = preferences_->AddGalleryWithName(
        gallery.device_id, gallery.display_name, gallery.path, true);
    preferences_->SetGalleryPermissionForExtension(*extension_, id, true);
  }
}

void MediaGalleriesDialogController::UpdateGalleriesOnPreferencesEvent() {
  // Merge in the permissions from |preferences_|. Afterwards,
  // |known_galleries_| may contain galleries that no longer belong there,
  // but the code below will put |known_galleries_| back in a consistent state.
  InitializePermissions();

  // If a gallery no longer belongs in |known_galleries_|, forget it in the
  // model/view.
  // If a gallery still belong in |known_galleries_|, check for a duplicate
  // entry in |new_galleries_|, merge its permission and remove it. Then update
  // the view.
  const MediaGalleriesPrefInfoMap& pref_galleries =
      preferences_->known_galleries();
  MediaGalleryPrefIdSet galleries_to_forget;
  for (KnownGalleryPermissions::iterator it = known_galleries_.begin();
       it != known_galleries_.end();
       ++it) {
    const MediaGalleryPrefId& gallery_id = it->first;
    GalleryPermission& gallery = it->second;
    MediaGalleriesPrefInfoMap::const_iterator pref_it =
        pref_galleries.find(gallery_id);
    // Check for lingering entry that should be removed.
    if (pref_it == pref_galleries.end() ||
        pref_it->second.type == MediaGalleryPrefInfo::kBlackListed) {
      galleries_to_forget.insert(gallery_id);
      dialog_->ForgetGallery(&gallery.pref_info);
      continue;
    }

    // Look for duplicate entries in |new_galleries_|.
    for (NewGalleryPermissions::iterator new_it = new_galleries_.begin();
         new_it != new_galleries_.end();
         ++new_it) {
      if (new_it->pref_info.path == gallery.pref_info.path &&
          new_it->pref_info.device_id == gallery.pref_info.device_id) {
        // Found duplicate entry. Get the existing permission from it and then
        // remove it.
        gallery.allowed = new_it->allowed;
        dialog_->ForgetGallery(&new_it->pref_info);
        new_galleries_.erase(new_it);
        break;
      }
    }
    dialog_->UpdateGallery(&gallery.pref_info, gallery.allowed);
  }

  // Remove the galleries to forget from |known_galleries_|. Doing it in the
  // above loop would invalidate the iterator there.
  for (MediaGalleryPrefIdSet::const_iterator it = galleries_to_forget.begin();
       it != galleries_to_forget.end();
       ++it) {
    known_galleries_.erase(*it);
  }
}

void MediaGalleriesDialogController::UpdateGalleriesOnDeviceEvent(
    const std::string& device_id) {
  for (KnownGalleryPermissions::iterator iter = known_galleries_.begin();
       iter != known_galleries_.end(); ++iter) {
    if (iter->second.pref_info.device_id == device_id)
      dialog_->UpdateGallery(&iter->second.pref_info, iter->second.allowed);
  }

  for (NewGalleryPermissions::iterator iter = new_galleries_.begin();
       iter != new_galleries_.end(); ++iter) {
    if (iter->pref_info.device_id == device_id)
      dialog_->UpdateGallery(&iter->pref_info, iter->allowed);
  }
}

// MediaGalleries dialog -------------------------------------------------------

MediaGalleriesDialog::~MediaGalleriesDialog() {}

}  // namespace chrome