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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
7 #include "base/json/json_writer.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/time/time.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/api/file_handlers/app_file_handler_util.h"
14 #include "chrome/browser/extensions/event_router.h"
15 #include "chrome/browser/extensions/extension_prefs.h"
16 #include "chrome/browser/extensions/extension_service.h"
17 #include "chrome/browser/extensions/extension_system.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/common/extensions/api/app_runtime.h"
20 #include "chrome/common/extensions/extension.h"
21 #include "content/public/browser/web_contents.h"
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/user_manager.h"
28 namespace extensions {
30 namespace app_runtime = api::app_runtime;
34 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
35 scoped_ptr<base::DictionaryValue> launch_data,
37 #if defined(OS_CHROMEOS)
38 launch_data->SetBoolean("isKioskSession",
39 chromeos::UserManager::Get()->IsLoggedInAsKioskApp());
41 launch_data->SetBoolean("isKioskSession", false);
43 scoped_ptr<base::ListValue> args(new base::ListValue());
44 args->Append(launch_data.release());
45 extensions::ExtensionSystem* system =
46 extensions::ExtensionSystem::Get(profile);
47 scoped_ptr<Event> event(new Event(app_runtime::OnLaunched::kEventName,
49 event->restrict_to_profile = profile;
50 system->event_router()->DispatchEventWithLazyListener(extension_id,
52 system->extension_service()->extension_prefs()->SetLastLaunchTime(
53 extension_id, base::Time::Now());
56 } // anonymous namespace
59 void AppEventRouter::DispatchOnLaunchedEvent(
60 Profile* profile, const Extension* extension) {
61 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue());
62 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), profile);
66 void AppEventRouter::DispatchOnRestartedEvent(Profile* profile,
67 const Extension* extension) {
68 scoped_ptr<base::ListValue> arguments(new base::ListValue());
69 scoped_ptr<Event> event(new Event(app_runtime::OnRestarted::kEventName,
71 event->restrict_to_profile = profile;
72 extensions::ExtensionSystem::Get(profile)->event_router()->
73 DispatchEventToExtension(extension->id(), event.Pass());
77 void AppEventRouter::DispatchOnLaunchedEventWithFileEntry(
78 Profile* profile, const Extension* extension,
79 const std::string& handler_id, const std::string& mime_type,
80 const extensions::app_file_handler_util::GrantedFileEntry& file_entry) {
81 // TODO(sergeygs): Use the same way of creating an event (using the generated
82 // boilerplate) as below in DispatchOnLaunchedEventWithUrl.
83 scoped_ptr<base::DictionaryValue> launch_data(new base::DictionaryValue);
84 launch_data->SetString("id", handler_id);
85 scoped_ptr<base::DictionaryValue> launch_item(new base::DictionaryValue);
86 launch_item->SetString("fileSystemId", file_entry.filesystem_id);
87 launch_item->SetString("baseName", file_entry.registered_name);
88 launch_item->SetString("mimeType", mime_type);
89 launch_item->SetString("entryId", file_entry.id);
90 scoped_ptr<base::ListValue> items(new base::ListValue);
91 items->Append(launch_item.release());
92 launch_data->Set("items", items.release());
93 DispatchOnLaunchedEventImpl(extension->id(), launch_data.Pass(), profile);
97 void AppEventRouter::DispatchOnLaunchedEventWithUrl(
99 const Extension* extension,
100 const std::string& handler_id,
102 const GURL& referrer_url) {
103 api::app_runtime::LaunchData launch_data;
104 launch_data.id.reset(new std::string(handler_id));
105 launch_data.url.reset(new std::string(url.spec()));
106 launch_data.referrer_url.reset(new std::string(referrer_url.spec()));
107 DispatchOnLaunchedEventImpl(
108 extension->id(), launch_data.ToValue().Pass(), profile);
111 } // namespace extensions