- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / app_runtime / app_runtime_api.cc
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.
4
5 #include "chrome/browser/extensions/api/app_runtime/app_runtime_api.h"
6
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"
22 #include "url/gurl.h"
23
24 #if defined(OS_CHROMEOS)
25 #include "chrome/browser/chromeos/login/user_manager.h"
26 #endif
27
28 namespace extensions {
29
30 namespace app_runtime = api::app_runtime;
31
32 namespace {
33
34 void DispatchOnLaunchedEventImpl(const std::string& extension_id,
35                                  scoped_ptr<base::DictionaryValue> launch_data,
36                                  Profile* profile) {
37 #if defined(OS_CHROMEOS)
38   launch_data->SetBoolean("isKioskSession",
39                           chromeos::UserManager::Get()->IsLoggedInAsKioskApp());
40 #else
41   launch_data->SetBoolean("isKioskSession", false);
42 #endif
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,
48                                     args.Pass()));
49   event->restrict_to_profile = profile;
50   system->event_router()->DispatchEventWithLazyListener(extension_id,
51                                                         event.Pass());
52   system->extension_service()->extension_prefs()->SetLastLaunchTime(
53       extension_id, base::Time::Now());
54 }
55
56 }  // anonymous namespace
57
58 // static.
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);
63 }
64
65 // static.
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,
70                                     arguments.Pass()));
71   event->restrict_to_profile = profile;
72   extensions::ExtensionSystem::Get(profile)->event_router()->
73       DispatchEventToExtension(extension->id(), event.Pass());
74 }
75
76 // static.
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);
94 }
95
96 // static.
97 void AppEventRouter::DispatchOnLaunchedEventWithUrl(
98     Profile* profile,
99     const Extension* extension,
100     const std::string& handler_id,
101     const GURL& url,
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);
109 }
110
111 }  // namespace extensions