summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/ui/screensaver_extension_dialog.cc
blob: 8eb28794d927ea8c5762384c386d4895ceb945af (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
// 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/ui/screensaver_extension_dialog.h"

#include "base/bind.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/sessions/restore_tab_helper.h"
#include "chrome/browser/ui/views/extensions/extension_dialog.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_file_util.h"

using content::BrowserThread;

namespace {

ScreensaverExtensionDialog* g_instance = NULL;

}  // namespace

namespace browser {

void ShowScreensaverDialog() {
  ScreensaverExtensionDialog::ShowScreensaverDialog();
}

void CloseScreensaverDialog() {
  ScreensaverExtensionDialog::CloseScreensaverDialog();
}

}  // namespace browser

// static
void ScreensaverExtensionDialog::ShowScreensaverDialog() {
  if (!g_instance)
    g_instance = new ScreensaverExtensionDialog();
  g_instance->Show();
}

// static
void ScreensaverExtensionDialog::CloseScreensaverDialog() {
  if (g_instance)
    g_instance->Close();
}

ScreensaverExtensionDialog::ScreensaverExtensionDialog()
    : screensaver_extension_(NULL),
      loading_extension_(false) {
}

void ScreensaverExtensionDialog::LoadExtension() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  std::string error;
  std::string extension_path = CommandLine::ForCurrentProcess()->
      GetSwitchValueASCII(switches::kKioskModeScreensaverPath);

  scoped_refptr<Extension> screensaver_extension =
      extension_file_util::LoadExtension(
          FilePath(extension_path),
          Extension::COMPONENT,
          Extension::NO_FLAGS,
          &error);

  if (!screensaver_extension) {
    LOG(ERROR) << "Could not load screensaver extension from: " <<
        extension_path;
    return;
  }

  BrowserThread::PostTask(BrowserThread::UI,
                          FROM_HERE,
                          base::Bind(
                              &ScreensaverExtensionDialog::SetExtensionAndShow,
                              base::Unretained(this),
                              screensaver_extension));
}

void ScreensaverExtensionDialog::SetExtensionAndShow(
    scoped_refptr<Extension> extension) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  screensaver_extension_ = extension;
  loading_extension_ = false;
  Show();
}

void ScreensaverExtensionDialog::Show() {
  // Whenever we're loading the extension, Show() will
  // be called after the load finishes, so return.
  if (loading_extension_)
    return;

  if (!screensaver_extension_) {
    loading_extension_ = true;
    BrowserThread::PostTask(BrowserThread::FILE,
                            FROM_HERE,
                            base::Bind(
                                &ScreensaverExtensionDialog::LoadExtension,
                                base::Unretained(this)));
    return;
  }

  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  Profile* default_profile = ProfileManager::GetDefaultProfile();
  if (default_profile->GetExtensionService()->AddExtension(
          screensaver_extension_)) {
    extension_dialog_ = ExtensionDialog::ShowFullscreen(
        screensaver_extension_->GetFullLaunchURL(),
        default_profile,
        string16(),
        this);
  } else {
    LOG(ERROR) << "Couldn't add screensaver extension to profile.";
  }
}

void ScreensaverExtensionDialog::Close() {
  if (extension_dialog_) {
    extension_dialog_->Close();
    extension_dialog_ = NULL;
  }
}

ScreensaverExtensionDialog::~ScreensaverExtensionDialog() {
  if (extension_dialog_)
    extension_dialog_->ObserverDestroyed();
}

void ScreensaverExtensionDialog::ExtensionDialogClosing(
    ExtensionDialog* dialog) {
  // Release our reference to the dialog to allow it to close.
  extension_dialog_ = NULL;
}

void ScreensaverExtensionDialog::ExtensionTerminated(
    ExtensionDialog* dialog) {
  // This needs to be run 'slightly' delayed. When we get the extension
  // terminated notification, the extension isn't fully unloaded yet. There
  // is no good way to get around this. The correct solution will be to
  // not need to reload the extension at all - but the current wiring in
  // ExtensionViewsHost makes that not possible.
  MessageLoop::current()->PostTask(FROM_HERE,
      base::Bind(&ScreensaverExtensionDialog::ReloadAndShow,
          base::Unretained(this)));
  dialog->Close();
}

void ScreensaverExtensionDialog::ReloadAndShow() {
  ProfileManager::GetDefaultProfile()->GetExtensionService()->ReloadExtension(
      screensaver_extension_->id());

  Show();
}